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

Re: gcc-10: options order important?



On Fri, Sep 03, 2021 at 02:12:46PM +0100, Tixy wrote:
> > A man page a found online [1] says linking happens as Greg described, 
> > and this is true looking at a 6 year old copy of that page on
> > archive.org. So seems strange that for many years my Makefiles have
> > worked with Libraries specified before inputs that use them.
> 
> Correction... 'for many years my Makefiles have worked with Libraries
> specified _after_ inputs that use them'

You had it right the first time.  It's strange that it worked (in the
past) with the library argument in front.  It's supposed to be behind.

The linker's argument processing must have been changed a few times.
GNU utilities in general have a tendency to be overly lenient with
command-line options.  Commands that *should* fail according to POSIX
sometimes work in the GNU variants.

For example:

unicorn:~$ ls .bashrc -l
-rwxr-xr-x 1 greg greg 2741 Sep  1 22:32 .bashrc*

That should *not* have worked, but GNU permits the option (-l) to be
handled even when it's in the wrong place.  (In standard POSIX ls,
that command should have tried to list a file named "-l", because
the non-option ".bashrc" terminates option processing.  All remaining
arguments are to be treated as files or directories.)

All I can speculate is that GNU ld (the linker that gcc uses) must have
had some tweaks done to it over the years, and now has settled back into
its original behavior.  An expert in GNU binutils (or even a changelog)
might have more details.

The way a gcc (or cc) command is supposed to be written is:

gcc [options] source/object files [-libraries]

The up-front [options] can include things like "-o foo" to specify an
output file other than a.out to the linker.  So, for example, the OP's
command should have been something like:

gcc -o foo foo.c -lm

That's the standard format, and is what you should be using in Makefiles
and similar files which call gcc.


Reply to: