Linux 上的链接器错误:“未定义的引用”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12272864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:15:12  来源:igfitidea点击:

Linker error on Linux: "undefined reference to"

linuxgccbuildlinker

提问by Matthew Mitchell

I am able to make a shared library without problems. I create libcbitcoin.so (with no errors) and attempt to link against it with an executable as well as OpenSSL libraries. I use this command:

我能够毫无问题地制作一个共享库。我创建了 libcbitcoin.so(没有错误)并尝试使用可执行文件和 OpenSSL 库链接它。我使用这个命令:

gcc -L/media/sf_BitEagle_Projects/cbitcoin/build/bin -lcbitcoin \
-Wl-rpath,/media/sf_BitEagle_Projects/cbitcoin/build/bin -lssl -lcrypto \
-L/usr/local/ssl/lib/ -o /media/sf_BitEagle_Projects/cbitcoin/build/bin/testCBAddress \
/media/sf_BitEagle_Projects/cbitcoin/build/obj/testCBAddress.o \
/media/sf_BitEagle_Projects/cbitcoin/build/obj/CBOpenSSLCrypto.o

The bin directory is the location of the library. The obj directory has the object files I wish to link into an executable. In the command I use the -L, -l and -rpath options which I thought was all that is needed for linking in linux. It seems I am wrong since I get errors like:

bin 目录是库的位置。obj 目录包含我希望链接到可执行文件的目标文件。在命令中,我使用了 -L、-l 和 -rpath 选项,我认为这是在 linux 中进行链接所需的全部内容。似乎我错了,因为我收到以下错误:

/media/sf_BitEagle_Projects/cbitcoin/test/testCBAddress.c:40:
undefined reference to `CBNewByteArrayFromString'

CBNewByteArrayFromString is found in the library. For some reason it is not being linked. OpenSSL too:

CBNewByteArrayFromString 可以在库中找到。由于某种原因,它没有被链接。OpenSSL 也是:

/media/sf_BitEagle_Projects/cbitcoin/dependencies/crypto/CBOpenSSLCrypto.c:37:
undefined reference to `SHA1'

How do I get the linking to work?

我如何让链接工作?

GCC version: gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3

GCC 版本:gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3

On Linux Mint 13

在 Linux Mint 13 上

Thank you.

谢谢你。

采纳答案by Jonathan Leffler

Put the libraries after the object files on the link command line:

将库放在链接命令行上的目标文件之后:

gcc /media/sf_BitEagle_Projects/cbitcoin/build/obj/testCBAddress.o \
    /media/sf_BitEagle_Projects/cbitcoin/build/obj/CBOpenSSLCrypto.o \
    -L/media/sf_BitEagle_Projects/cbitcoin/build/bin \
    -lcbitcoin -Wl-rpath,/media/sf_BitEagle_Projects/cbitcoin/build/bin \
    -L/usr/local/ssl/lib/ -lssl -lcrypto \
    -o /media/sf_BitEagle_Projects/cbitcoin/build/bin/testCBAddress

If you don't do that, the linker may decide that it needs nothing from a particular library at the stage of the link where it scans the library, and then it won't rescan the library later after it finds some undefined symbols in the object files. If you put the object files first, you don't run into this problem.

如果你不这样做,链接器可能会在它扫描库的链接阶段决定它不需要来自特定库的任何东西,然后在它在库中找到一些未定义的符号后它不会重新扫描库目标文件。如果将目标文件放在首位,则不会遇到此问题。

回答by How Chen

I think it caused by can NOT find symbol, gcc will first go through from left, try to put the lib file at the end

我认为是找不到符号造成的,gcc会先从左边通过,尝试把lib文件放在最后