Gerhard
Gerhard

Reputation: 7069

Linking Libraries that contain circular references in GCC

I am trying to link an application with multiple static libraries in GCC.

There are two libraries that cause problems. Libsupport provides a terminal for the application. It relies on libcpu to provide a serial link, timing and syncronisation. Libcpu relies on libsupport to provide queueing for serial data and more.

If I specify libsupport first when linking libcpu cannot be linked with the queue functions. Is I specify libcpu first lib support can not link the serial link (and more) functions.

It looks like GCC parses a library only once and discard any unused objects.

Can I ask gcc to parse libraries multiple times or to include all objects?

Upvotes: 9

Views: 7939

Answers (4)

Dmitry Grigoryev
Dmitry Grigoryev

Reputation: 3203

Note that --start-group /  --end-group are linker options which are unknown to the compiler, so if you use gcc / g++ for linking, you should specify them as:

gcc ... -Wl,--start-group -lsupport -lcpu -Wl,--end-group

Otherwise you will get:

gcc.exe: error: unrecognized command-line option '--start-group'

gcc.exe: error: unrecognized command-line option '--end-group'

Upvotes: 2

Employed Russian
Employed Russian

Reputation: 213829

Here is detailed explanation of why either repeating libraries or using --start/--end-group is required in this situation.

Upvotes: 7

user562374
user562374

Reputation: 3917

gcc ... -lsupport -lcpu -lsupport -lcpu

-> Each mention of a library will cause resolution of libraries that came before it (but not necessarily ones specified afterwards), which is why you may need to specify more "-lsupport -lcpu" in future.

Alternatively, try --start-group -lsupport -lcpu --end-group once.

Upvotes: 12

Paul R
Paul R

Reputation: 213080

You can normally specify a library more than once to get around this kind of problem, e.g.

$ gcc ... -lsupport -lcpu -lsupport ...

Upvotes: 3

Related Questions