Getopt implicit declaration in Solaris?

In Solaris gcc gives me

implicit declaration of the getopt function

at compilation

#include <unistd.h> #include <stdlib.h> int main(int argc, char *argv[]) { getopt(1,argv,""); return 0; } 

the getopt man page says something about including unistd.h or stdio.h, however, although I enter both I still get this warning. This is normal? Is using functions that are not explicitly declared common in Unix development?

+7
c gcc unix posix solaris
source share
3 answers

You compile with -ansi , and getopt may not be available in this mode, since -ansi implies C89 compliance mode. Try removing this switch or #define _GNU_SOURCE to #include <unistd.h> . getopt() is POSIX, not ANSI.

Edit: you probably don't need _GNU_SOURCE . According to this , you should be able to get functionality with macro definition of the preprocessor so that this is true:

 #if _POSIX_C_SOURCE >= 2 || _XOPEN_SOURCE || _POSIX_SOURCE 

For more information on function verification macros, see.

+9
source share

The man page says stdio.h , not stdlib.h . Does stdio.h problem?

+1
source share

Using gnu99 solved this for me:

 gcc -std=gnu99 file.c 

This is from unistd.h .

+1
source share

All Articles