Question

I am trying to run the following code snippet taken from this simple example of a timer:

#include <sys/time.h>
#include <stdio.h>

int SetTimer(struct timeval &tv, time_t sec) {
    gettimeofday(&tv, NULL);
    tv.tv_sec += sec;

    return 1;
}

int CheckTimer(struct timeval &tv, time_t sec) {
    struct timeval ctv;
    gettimeofday(&ctv, NULL);

    if ((ctv.tv_sec > tv.tv_sec)) {
        gettimeofday(&tv, NULL);
        tv.tv_sec += sec;
        return 1;
    } else {
        return 0;
    }
}

int main() {
    struct timeval tv;
    SetTimer(tv, 5); //set up a delay timer
    printf("start counting.\n");
    while (1)
        if (CheckTimer(tv, 5) == 1)
            printf("Welcome to cc.byexamples.com\n");
    return 0;
}

I am obtaining the following error: field tv_sec could not be resolved

I have searched for it on the Web but no one seems to give a concrete answer.

I tried my chance looking into the libraries sys/time.h and time.h but in none of them seems to be defined this structure but is anyway used.

Am I missing any library? Since this example is rather old, did something changed that needs it to be done in a different way? I would appreciate any sight.

PS: I am using Eclipse CDT Indigo under Ubuntu 11.10 and g++ 4.6.1.

Was it helpful?

Solution 2

In the end it was an Eclipse problem since it was unable to index the time.h library. I solved it by following the most upvoted answer of this other SF question:

Adding manually time.h to the C/C++ indexer.

Currently I am using Eclipse CDT Juno and the problem doesn't seems not to be happening anymore. As a side comment in Eclipse CDT Juno I couldn't find the location where to manually edit the C/C++ indexer settings.

OTHER TIPS

try to put this in your file : #define __USE_GNU

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top