0

I'm using utarray (part of the uthash library) for a project. Whenever I include it, I get the following error:

utarray.h:221:3: error: implicit declaration of function ‘strdup’ [-Wimplicit-function-declaration]

Admittedly, I use some pretty aggressive flags when compiling (-Wall -Wpedantic -Wextra -Werror -pedantic-errors -std=c99), but I don't understand why this should be an error at all. strdup is defined in string.h (according to man strdup) which is very clearly included from utarray.h.

What am I doing wrong? Google was no help. (apparently nobody else tries to compile utarray.h with these flags?)

Here's an example file that fails to compile (using gcc -Wall -Wpedantic -Wextra -Werror -pedantic-errors -std=c99 scratch.c).

#include "utarray.h"

int main(int argc, char *argv[]) {
    (void)argc;
    (void)argv;
    return 0;
}

versions: gcc 4.9.2, glibc 2.21, uthash 1.9.9

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97

2 Answers2

2

The problem is that strdup() is not a c standard function it's a POSIX function, you can't use -std=c99 when you use strdup() unless you add the following -D_POSIX_C_SOURCE=200809L to the compilation command

gcc -Wall -Wpedantic -Wextra -Werror -pedantic-errors -std=c99 -D_POSIX_C_SOURCE=200809L
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • Even after including `string.h`? – Duncan Townsend Mar 11 '15 at 18:04
  • 1
    Of course, that's possible. You just have to define the correct feature test macro (`-D_POSIX_C_SOURCE=200809L`). See the [man page](http://man7.org/linux/man-pages/man3/strdup.3.html). – cremno Mar 11 '15 at 18:04
  • 1
    It's not quite correct to say `strdup()` is not a standard function. It's not standardized by C99, but it *is* standardized by POSIX. – John Bollinger Mar 11 '15 at 18:06
  • Note that there are other feature test macros that will also result in `strdup()` being defined by `string.h`. In fact, the Linux/GLIBC manpage doesn't even mention `_POSIX_C_SOURCE`, but rather `_SVID_SOURCE` or `_BSD_SOURCE`, or `_XOPEN_SOURCE` >= 500. – John Bollinger Mar 11 '15 at 18:12
  • @JohnBollinger lately when compiling KDE, i've noticed that they use `_BSD_SOURCE` and **glibc** was complaining about it via `#warning`, and suggesting `_XOPEN_SOURCE`. – Iharob Al Asimi Mar 11 '15 at 18:24
  • Is there one of those that is preferable? It seems like they each enable a somewhat arbitrary collection of functionality. – Duncan Townsend Mar 11 '15 at 18:29
  • @DuncanTownsend actually it seems from it's name like `_POSIX_C_SOURCE` is the best option, atlhough according to the manual it works since **glibc-2.12**, which doesn't matter if you are using another c library. For **glibc-2.10** and before it was `_GNU_SOURCE`, if you are using a build system, let it test which one to use when configuring the source. – Iharob Al Asimi Mar 11 '15 at 18:33
0

I had the same error with strdup), so I added the dummy prototype statement

char * strdup(char *) ; 

I don’t get the error message anymore.

Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39