Compiler flag to reveal functions like strdup

1.4k views Asked by At

I've been given some starter code for a project I have to complete in a class I'm taking. The code compiles fine on the university computers however when I try to compile the code on my own computer I get errors due the function call strdup. From what I can gather this is caused because strdup is not a ISO c99 function (https://bugzilla.redhat.com/show_bug.cgi?id=130815). How should I go about getting the code to compile? I'd imagine I just need to throw in some additional compiler flags but I'm not sure which ones. In case you need the info I ran g++ -v, here is the output:

Using built-in specs.
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.4.3-4ubuntu5' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --program-suffix=-4.4 --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-plugin --enable-objc-gc --disable-werror --with-arch-32=i486 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5) 
2

There are 2 answers

0
jmuc On BEST ANSWER

The answer of Seth is correct. If it doesn't work for you, there is also the possibility to pass the -std=gnu99 compiler option to g++ (it automatically defines all common test macros).

Be sure to add all options at the end of the command line, because later options overwrite newer ones!

Here are the test macro requirements for GNU glibc (from manpage):

       Since glibc 2.12:
           _SVID_SOURCE || _BSD_SOURCE || _XOPEN_SOURCE >= 500 ||
           _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED ||
           _POSIX_C_SOURCE >= 200809L
       Before glibc 2.12:
           _SVID_SOURCE || _BSD_SOURCE || _XOPEN_SOURCE >= 500 ||
           _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED

I don't know why POSIX >= 200809L is required, since the manpage says it conforms to POSIX.1-2001.

2
Seth On

Add -D_BSD_SOURCE or -D_SVID_SOURCE to your compile line and you will expose strdup()