On Wed, 2008-06-18 at 17:07 +0200, Quentin Mathé wrote:

> The man page example works for me too.

I realize now that this example doesn't help -- it includes getopt.h,
rather unistd.h, because it uses long options.  I have attached a
modified example which uses standard getopt and includes unistd.h.

> After some experiments, I have found the problem comes from passing  
> the flag -std=c99 to GCC. The example fails to compile with it, unless  
> I pass -D_GNU_SOURCE or I use -std=gnu99 instead of c99.
> I'm not sure why I observe this behavior. I suppose getopt() is a  
> posix extension not part of the C99 standard lib. On Linux (at least  
> Ubuntu), posix extensions are enabled by default except for c99 (and  
> probably other strict ISO C versions). Is my reasoning correct?

I agree with you.  "-std=c99" seems to be equivalent to passing
"-std=gnu99 -ansi", where the "-ansi" restricts to ANSI standard (more
or less; according to the GCC info page, to be really strict you must
add "-pedantic").  Same with "-std=c89" and just "-ansi", since
"-std=gnu89" is the default.

> I think David Chisnall wrote this code for FreeBSD initially. So this  
> point doesn't seem to hold for FreeBSD.

Exactly.  To the extent that one is picky about standards compliance,
this is a bug with FreeBSD's libc headers.  Of course, it's also a
pragmatic solution to just declare getopt and be done with it, since it
doesn't conflict with an older identifier.

> Thanks for the help :-)

No problem.  Thanks for writing clean code  -- someone less careful
would just include getopt.h and move on  :)


Sourav
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main (int argc, char **argv) {
	int c;
	
	while(1) {
		c = getopt(argc, argv, "abc:d:");
		if(c == -1) break;

		switch(c) {
	        case 'a':
				printf ("option a\n");
				break;

			case 'b':
				printf ("option b\n");
				break;

			case 'c':
				printf ("option c with value '%s'\n", optarg);
				break;
			
			case 'd':
				printf ("option d with value '%s'\n", optarg);
				break;
		
			case '?':
				break;
			
			default:
				printf ("?? getopt returned character code 0%o ??\n", c);
		}
	}

	exit(0);

}
_______________________________________________
Etoile-discuss mailing list
[email protected]
https://mail.gna.org/listinfo/etoile-discuss

Répondre à