Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getopt implicit declaration in Solaris?

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?

like image 403
Steven Avatar asked Dec 13 '09 22:12

Steven


3 Answers

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.

like image 170
Alok Singhal Avatar answered Nov 17 '22 22:11

Alok Singhal


The man page says to include stdio.h, not stdlib.h. Does including stdio.h fix the problem?

like image 1
sth Avatar answered Nov 17 '22 22:11

sth


Using gnu99 solved it for me:

gcc -std=gnu99 file.c

This is with unistd.h.

like image 1
KendallV Avatar answered Nov 18 '22 00:11

KendallV