Linking Libraries that contain circular references in GCC
Asked Answered
P

4

9

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?

Paco answered 26/1, 2011 at 8:42 Comment(0)
S
12
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.

Squiffy answered 26/1, 2011 at 9:25 Comment(1)
The group specifier is interesting.. never seen it before.Ligure
O
7

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

Opportunist answered 27/1, 2011 at 6:53 Comment(0)
P
3

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

$ gcc ... -lsupport -lcpu -lsupport ...
Peracid answered 26/1, 2011 at 8:49 Comment(1)
I tried it but it did not work. Then I tried "-lsupport -lcpu -lsupport -lcpu" and it worked. Not sure what is happing but the application is linking.Paco
R
2

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'

Ragsdale answered 15/9, 2022 at 13:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.