gcc and linking problems?

Hugh Sasse hgs@dmu.ac.uk
Thu Sep 14 10:17:00 GMT 2006

On Fri, 8 Sep 2006, Hugh Sasse wrote:

> On Fri, 8 Sep 2006, Rupert Wood wrote:
> > Hugh Sasse wrote:
> > > The second one seems to be a linker problem, since it is complaining
> > > about the library file itself.  Other programs seem to be working
> > > with PNG images correctly, however.  
> > 
> > It's complaining that it can't find deflate, which is defined in libz. Link
> > order is significant: you must link libz *after* you link libpng since
> > libpng depends on it.
> Oh, I thought it would have to be first for precisely that reason :-)
> Is there something that will help me sort these directives correctly?

I have a solution to the oringinal prolem now thanks to Nobu Nakada
in the Ruby community.  The -l options must all occur after the .o
files.  The GCC manual does say

-l library
    Search the library named library when linking. (The second
alternative with the library as a separate argument is only for
POSIX compliance and is not recommended.)

    It makes a difference where in the command you write this
option; the linker searches and processes libraries and object files
in the order they are specified. Thus, `foo.o -lz bar.o' searches
library `z' after file foo.o but before bar.o. If bar.o refers to
functions in `z', those functions may not be loaded.


    Normally the files found this way are library files.archive
files whose members are object files. The linker handles an archive
file by scanning through it for members which define symbols that
have so far been referenced but not defined. But if the file that is
found is an ordinary object file, it is linked in the usual fashion.
The only difference between using an -l option and specifying a file
name is that -l surrounds library with `lib' and `.a' and searches
several directories. 

But this is way after earlier advice that:

You can mix options and other arguments. For the most part, the
order you use doesn't matter. Order does matter when you use several
options of the same kind; for example, if you specify -L more than
once, the directories are searched in the order specified.

at http://gcc.gnu.org/onlinedocs/gcc-4.1.1/gcc/Invoking-GCC.html#Invoking-GCC

It might be useful to change that by adding links to the specific cases
where this is false, maybe in footnote style "[1], [2]". Given the way 
-l works, I can't imagine a way to improve the error diagnostics to 
show this mistake up.

so the only outstanding question for me is whether there is a tool
to sort of list of libraris into the correct order for addition to a
GCC command line?

        Thank you

More information about the Gcc-help mailing list