Static library symbols not being found even with -l

倖福魔咒の 提交于 2020-01-03 03:18:12

问题


I have a static library, liborc-0.4.a with no shared library. I have another library, libschroedinger-1.0.a (no shared) that depends on symbols in liborc-0.4.a. If I run nm on liborc-0.4.a, symbols such as orc_init show up as T (meaning they are defined). I built libschroedinger-1.0.a with the command line flag -lorc-0.4 so it saw the symbols and was ok.

However, now I have a small executable that depends on libschroedinger-1.0.a. It compiles fine, but when I run the linker

gcc -lschroedinger-1.0 -lorc-0.4 -o output input.o

It gives errors such as:

/usr/local/lib/libschroedinger-1.0.a(libschroedinger_1.0_la-schro.o):schro.c:(.text+0x21):
undefined reference to `orc_init'

回答1:


gcc is sensitive to the order of libraries. When it's compiling liborc-0.4.a in, there is no need for orc_init, so it's not included. The solution is to put the LDFLAGS at the end of the command:

gcc -o output input.o -lschroedinger-1.0 -lorc-0.4



回答2:


You most probably compiled libschroedinger with shared liborc. Static library is the same as bunch of object files in an archive, so they don't need to see more than headers. Write like the following to be sure (the same apples to liborc).

gcc /path/to/libschroedinger-1.0.a /path/to/liborc-0.4.a -o output input.o


来源:https://stackoverflow.com/questions/5010384/static-library-symbols-not-being-found-even-with-l

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!