Subject: RE: B19: Link error between libs linked with -l
From: [email-masked] (Colin Peters)

29 Sep 1998 15:36:33 -0700
Message-ID: <[email-masked]>
Mime-Version: 1.0
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
To: "GNU-win32" <[email-masked]>
Cc: "Adam Chlipala" <[email-masked]>, "Mumit Khan" <[email-masked]>

Mumit Khan <[email-masked]> wrote:

>On Sun, 27 Sep 1998, Adam Chlipala wrote:
>
>> When I do something like the following:
>> gcc file.c -l1 -l2
>> I get linker errors involving functions found in 1 and used in 2.  When
>> I move a lib to the cwd and do:
>> gcc file.c lib1.a -l2
>> all is well.  Am I doing something incorrectly here or is this a bug?
>
>When you use -l<libname>, you must also supply a -L<dirname> to tell
>the linker where to find it (unless it's in one of the standard or
>default directories).

[snip]

Actually this looks like a simple link order error to me. If I'm not
mistaken, in gcc if an object uses a symbol in a library that library must
appear *after* the object that requires it on the command line. The
exception to this rule is if the object in the library that contains the
required symbol has already been linked in, either by another reference in
earlier code or, as happens in the second case above, all the objects in the
library are linked in explicitly (when you include a library on the command
line directly instead of using -l then all the objects in the library are
linked in, whether your code references them or not). The solution, if I'm
correct, is:

gcc file.c -l2 -l1

If the two librarys are interdependent then you may have to list them twice
(or more times!):

gcc file.c -l2 -l1 -l2

which is just one fine argument for getting rid of cycles in your dependency
graphs.

Hope I'm right :)

Colin.

PS. For even more fun, if you have a symbol defined in two libraries the one
in the first library on the command line (after the referencing object) will
be used and there will be no error. You get errors when two objects are
forced to be linked in with duplicate symbols. It's wonderfully complicated.

- Colin Peters - colin at fu.is.saga-u.ac.jp
- http://www.geocities.com/Tokyo/Towers/6162/index.html
- Go not to usenet for counsel, for it will say both
- 'yes' and 'no' and 'try another newsgroup'.

-
For help on using this list (especially unsubscribing), send a message to
"[email-masked]" with one line of text: "help".