This discussion is archived
2 Replies Latest reply: Dec 17, 2009 1:51 AM by paul_floyd RSS

getopt implicit declaration in Solaris?

807559 Newbie
Currently Being Moderated
When compiling this program with
gcc -ansi -pedantic -Wall
I get the warning "implicit declaration of function `getopt'"
#include <unistd.h>
#include <stdio.h>
  
int main(int argc, char *argv[]) {
    getopt(1,argv,"");
    return 0;
}
The [man page|http://docs.sun.com/app/docs/doc/816-5168/getopt-3c?l=en&a=view&q=getopt] 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?
  • 1. Re: getopt implicit declaration in Solaris?
    user4994457 Newbie
    Currently Being Moderated
    getopt() isn't part of ANSI, so I think it's being stripped when you give '-ansi'.

    --
    Darren
  • 2. Re: getopt implicit declaration in Solaris?
    paul_floyd Newbie
    Currently Being Moderated
    Hi

    man -s3c getopt says
    SYNOPSIS
      SVID3, XPG3
         #include <stdio.h>
    
         int getopt(int argc, char * const argv[], const char *optstring
    
         extern char *optarg;
    
         extern int optind, opterr, optopt;
    
      POSIX.2, XPG4, SUS, SUSv2, SUSv3
         #include <unistd.h>
    
         int getopt(int argc, char * const argv[], const char *optstring
    
         extern char *optarg;
    [here's a little bug in the manpage it seems, the function prototypes don't have the closing ");"]

    You have to decide which standard you want to code to (see "man standards"). For instance these two should work

    gcc -ansi -pedantic -Wall -D_XOPEN_SOURCE=500

    or

    gcc -std=c99 -pedantic -Wall -D_XOPEN_SOURCE=600

    (Xopen 600, or SUSv3, requires C99).

    You may also find that you need -D__EXTENSIONS__

    Paul