In Solaris, gcc gives me
implicit declaration of function `getopt'
when compiling
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
    getopt(1,argv,"");
    return 0;
} 
The man page for getopt says something about including unistd.h or stdio.h, however even though I'm inluding both I still get this warning. Is this normal? Is using functions that aren't explicitly declared common in Unix development?
You're compiling with -ansi, and in that mode getopt might not be available, since -ansi implies C89 conformant mode.  Try removing that switch, or #define _GNU_SOURCE before #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 the functionality with defining preprocessor macros such that this is true:
#if _POSIX_C_SOURCE >= 2 || _XOPEN_SOURCE || _POSIX_SOURCE
See this for more information on the feature test macros.
The man page says to include stdio.h, not stdlib.h. Does including stdio.h fix the problem?
Using gnu99 solved it for me:
gcc -std=gnu99 file.c
This is with unistd.h.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With