[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: gcc oddities : why would this compile?



On Sat, Jan 05, 2002 at 06:52:04PM -0800, Eric G . Miller wrote:
> On Fri, 4 Jan 2002 21:41:13 -0500, dman <dsh8290@rit.edu> wrote:
> > I have both gcc 2.95.4 and gcc 3.0.2 installed.  Both of them compiled
> > this C program (this is the entire thing) :
> > 
> > -----------
> > int
> > main()
> > {
> >     puts( uname() ) ;
> >     return 0 ;
> > }
> > -----------
> > 
> > Why does this compile without any errors?  (no options were given to
> > gcc)  Why don't I need to include stdio.h or sys/utsname.h?  After I
> > looked at the docs (I just wanted to see what the function would
> > return) I saw that it returns an int to indicate success/failure and
> > takes a pointer to a struct utsname as an out-argument.  (the program
> > above naturally seg faults)  I get the same sort of effect with
> > gethostbyname() (I should include netdb.h, it takes a string argument
> > and returns a pointer to a struct hostent).
> 
> By the looks of it, I would contend you have found a bug.  I think my
> contention is supported by the fact the using -Wall causes the compilation
> to fail at the linker.  So why would -Wall cause it to fail at the
> linker when not using it doesn't?

Not here it doesn't. Both 'gcc -o foo foo.c' and 'gcc -Wall -o foo
foo.c' succeed, although of course -Wall produces two warnings.

> Using -pedantic-errors, does catch and stop the compilation before the
> linker is called.  So, I'm not hundred percent sure it'd be a bug.
> GCC does try to support K&R, so will relax checking if it thinks it is
> supposed to "int main()" ??

It is quite correct for gcc to generate a non-fatal warning during
compilation for this. C specifies that unprototyped functions are
assumed to take a non-specified number of arguments and return int (see
K&Rv2 p. 201). Even the linker can't spot the argument mismatch, since C
symbols, unlike those of C++, don't contain argument information.

> I find GCC is often too lax and wish it'd generate more compilation
> errors out of the box.  I suppose, I could change the defaults somewhere.

Always use -Wall anyway. It'll make your life much easier.

-- 
Colin Watson                                  [cjwatson@flatline.org.uk]



Reply to: