g++ linking order dependency when linking c code to c++ code

25,066

Solution 1

You can use --start-group archives --end-group and write the 2 dependent libraries instead of archives:

gcc main.o -L. -Wl,--start-group -lobj_A -lobj_b -Wl,--end-group

Solution 2

The library order pass to gcc/g++ does actually matter. If A depends on B, A must be listed first. The reason is that it optimizes out symbols that aren't referenced, so if it sees library B first, and no one has referenced it at that point then it won't link in anything from it at all.

Solution 3

A static library is a collection of object files grouped into an archive. When linking against it, the linker only chooses the objects it needs to resolve any currently undefined symbols. Since the objects are linked in order given on the command line, objects from the library will only be included if the library comes after all the objects that depend on it.

So the link order is very important; if you're going to use static libraries, then you need to be careful to keep track of dependencies, and don't introduce cyclic dependencies between libraries.

Share:
25,066
Gearoid Murphy
Author by

Gearoid Murphy

https://www.linkedin.com/in/gearoid-murphy-a6003983/

Updated on August 15, 2020

Comments

  • Gearoid Murphy
    Gearoid Murphy over 3 years

    Prior to today I had always believed that the order that objects and libraries were passed to g++ during the linking stage was unimportant. Then, today, I tried to link from c++ code to c code. I wrapped all the C headers in an extern "C" block but the linker still had difficulties finding symbols which I knew were in the C object archives.

    Perplexed, I created a relatively simple example to isolate the linking error but much to my surprise, the simpler example linked without any problems.

    After a little trial and error, I found that by emulating the linking pattern used in the simple example, I could get the main code to link OK. The pattern was object code first, object archives second eg:

    g++ -o serverCpp serverCpp.o algoC.o libcrypto.a

    Can anyone shed some light on why this might be so?. I've never seen this problem when linking ordinary c++ code.

  • caf
    caf almost 14 years
    Cyclic dependencies are OK - it just means you need to have a link line like "a.o b.o a.o".
  • Mike Seymour
    Mike Seymour almost 14 years
    @caf: if the extra objects from the second inclusion of a depend on objects in b that haven't been included yet, then that's not good enough; you'll need to add b again at the end. Which in turn might introduce more dependencies on a. So cyclic dependencies aren't really "OK", it's just rare for them to be pathological enough to need more than one repetition.