This simple .c file:
#include <unistd.h>
void test() {
char string[40];
gethostname(string,40);
}
… when compiled normally, works fine:
$ cc -Wall -c -o tmp.o tmp.c
$
… but when compiled in C99 mode, gives a warning:
$ cc -Wall -std=c99 -c -o tmp.o tmp.c
tmp.c: In function `test':
tmp.c:5: warning: implicit declaration of function `gethostname'
$
The resultant .o file is fine, and linking works. I’d just like to get rid of the warning. I can achieve this in a hacky way, by putting declarations in my own .h file.
What is it about C99 that means the declarations in unistd.h don’t get included?
Can this be overcome, without giving up the niceness of C99?
I see the same problem for other standard libs.
You may need to define some macros in a particluar way to get the prototype for
gethostname()From
man gethostname:So:
The gory details:
If you don’t specify the
-std-c99option, thenfeatures.h(which is implicitly included byunistd.h) will default to setting_BSD_SOURCEin such a way that the prototype forgethostname()gets included. However, specifying-std=c99causes the compiler to automatically define__STRICT_ANSI__, which in turn causesfeatures.hto not define_BSD_SOURCE, unless you force it with your own feature macro definition (as above).