B19: Link error between libs linked with -l

Colin Peters colin@fu.is.saga-u.ac.jp
Tue Sep 29 02:36:00 GMT 1998

Mumit Khan <khan@xraylith.wisc.edu> 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).


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

Hope I'm right :)


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
"gnu-win32-request@cygnus.com" with one line of text: "help".

More information about the Cygwin mailing list