1
votes

This isn't as naive as the title may lead you to think.

I receive an "Undefined Reference" Error from the linker/ld for a couple of function symbols in third party shared library, that I'm trying to link with my executable.

The strange part is, that library itself is supposed to contain the definition for the offending symbols in question.

The stranger part is that there are several executables in my project, with some facing this problem and some not.

Have I messed up my gcc/ld flags or is it something else?

2
"Have I messed up my gcc/ld flags or is it something else?" The answer on this question is "yes". Please, provide more details.P Shved

2 Answers

1
votes

Have you checked your link order? This has gotten stricter in recent versions of GCC.

For example a common problem is caused by doing this:

g++ -lX11 -lSuperLibrary awesomeApp.cpp

instead of this:

g++ awesomeApp.cpp -lX11 -lSuperLibrary

It also matters which order the library flags are, if they are interdependent.

There are flags for doing a library multi-pass which might help you solve your problem. To to the same as above but with the linker 'grouping' libraries and doing a recursive link on them (at a linker performance cost) you could do something like the following:

g++ awesomeApp.cpp -Wl,--start-group -lX11 -lSuperLibrary -Wl,--end-group

Where -Wl,<option> passes an option to the linker... in this case --start-group and --end-group .

A great analogy of why link order is important is here

I hope that helps.

0
votes

Impossible to say from your description, which contains approximately zero useful information. One possibility is that you are not including the correct header file in some source files.