GCC oddity

Kurt Wall kwall
Mon May 17 11:47:15 PDT 2004


An unnamed Administration source, Kevin O'Gorman, wrote:
% I have a short program that provokes an error when compiled on
% some versions of gcc, and I cannot figure out why.  I'm using
% the stock gcc that came with RedHat 7.1 (gcc 2.96-81)
% 
% The program is simple:
% ##################### CUT HERE ###################################
% #include <sys/types.h>
% #include <sys/stat.h>
% #include <unistd.h>
% 
% int
% main(){
%         int r;
%         struct stat buf;
% 
%         r = lstat(".", &buf);
%         return r;
% }
% ##################### CUT HERE ###################################
% 
% And I use a simple makefile:
% 
% ##################### CUT HERE ###################################
% CC =gcc
% CFLAGS = -g -Wall -ansi -pedantic
% 
% foo: foo.c
% 
% bar.c: foo.c
%         gcc -E $< | perl -p -e 's:(#.*):/* \1 */:' >$@
% 
% clean:
%         rm bar* foo.o foo
% ##################### CUT HERE ###################################
% 
% If I use the command 'make foo' I get
% 
% > gcc -g -Wall -ansi -pedantic    foo.c   -o foo
% > foo.c: In function `main':
% > foo.c:10: warning: implicit declaration of function `lstat'
% 
% But I don't get this warning if I use the preprocessor to
% make a source file without includes.  This should be identical
% to the results for foo.c, but if I use the command 'make bar'
% I get:
% 
% > gcc -E foo.c | perl -p -e 's:(#.*):/* \1 */:' >bar.c
% > gcc -g -Wall -ansi -pedantic    bar.c   -o bar
% 
% I had expected the same error with the constructed bar.c, so that
% I could narrow down the cause of the error, but this is quite
% frustrating.
% 
% Any ideas?

Lose the -ansi flag. I believe it is disabling the declaration
of lstat in sys/stat.h.

     The macro `__STRICT_ANSI__' is predefined when the `-ansi' option
     is used.  Some header files may notice this macro and refrain from
     declaring certain functions or defining certain macros that the
     ISO standard doesn't call for; this is to avoid interfering with
     any programs that might use these names for other things.

Kurt
-- 
This novel is not to be tossed lightly aside, but to be hurled with
great force.
		-- Dorothy Parker


More information about the Linux-users mailing list