13

Typing man strptime it sais that this function needs to have declared _XOPEN_SOURCE and included time.h header. I did it. But, when I try to compile my code I get:

./check.c:56: warning: implicit declaration of function ‘strptime’

Look at my code:

int lockExpired(const char *date, const char *format, time_t current) {
        struct tm *tmp = malloc(sizeof(struct tm *));
        time_t lt;
        int et;

        strptime(date, format, tmp);
        lt = mktime(tmp);
        et = difftime(current, lt);

        if (et < 3600)
                return -et;

        return 1;
}

Also the function declaration is:

char *strptime(const char *s, const char *format, struct tm *tm);

Can anyone tell me where my problem come from?

artaxerxe
  • 6,281
  • 21
  • 68
  • 106

2 Answers2

29

I've found that I needed to define __USE_XOPEN and also _GNU_SOURCE to get it to be happy.

Joe
  • 7,378
  • 4
  • 37
  • 54
  • 3
    That was it, thanks! It is strange that `man strptime` is wrong on my Linux machine; it writes `_XOPEN_SOURCE` which does not work for me. That being said, I am not sure about `_GNU_SOURCE` though. `__USE_XOPEN` seems to work on its own for me. – László Papp May 30 '14 at 09:43
  • 5
    make sure you use `#define __USE_XOPEN` before you use `#include ` – Itay Sela Sep 09 '15 at 13:42
1

The manual page provided by Debian says:

#define _XOPEN_SOURCE /* See feature_test_macros(7) */
#include <time.h>

The comment shouldn’t be disregarded. Indeed, by running man 7 feature_test_macros you will learn that:

In order to be effective, a feature test macro must be defined before including any header files.

So just move the #define line to the very top of your source file and it will work just fine.

kirjosieppo
  • 617
  • 3
  • 16