Search code examples
cgccposixstrptime

strptime error on Ubuntu 17.10 with GCC 7.2.0


When compiling a program that uses strptime with the following:

gcc http_server.c -g -std=c11 -o http_server

I run into the this warning:

warning: implicit declaration of function 'strptime'; did you mean 'strftime'? [-Wimplicit-function-declaration]

When I run the program I get a segmentation fault. Upon further debugging I come to find out it fails at the strptime() line. I have time.h included in the file. I am also using gcc 7.2.0 as stated in the title. Any help would be appreciated as I'm at a loss.

Here is the line in my code:

const char TIME_FORMAT[] = "%a, %d %b %Y %H:%M:%S GMT\r\n";
char date[255];
strcpy(date, token + 19);
strptime(date, TIME_FORMAT, request->if_modified_since);

Solution

  • Use -D_XOPEN_SOURCE=700 on the compiler command line. Just -D_XOPEN_SOURCE is equivalent to -D_XOPEN_SOURCE=1 and that won't get strptime() declared.

    You could use 500 or 600 instead of 700; you shouldn't need to.

    You could also use -std=gnu11 instead of -std=c11 and then strptime() would be exposed, with or without the -D_XOPEN_SOURCE=700.

    You could also think about using a header to ensure the correct POSIX defines are in use; that's what I do. See posixver.h, which is available on GitHub in my SOQ (Stack Overflow Questions) repository as file posixver.h in the src/libsoq sub-directory.