开发者

"Undefined Reference" Error From ld

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 fl开发者_Python百科ags or is it something else?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜