简体   繁体   English

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

[英]Linker error on Linux: “undefined reference to”

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. 我创建了libcbitcoin.so(没有错误),并尝试通过可执行文件以及OpenSSL库与之链接。 I use this command: 我使用以下命令:

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. bin目录是库的位置。 The obj directory has the object files I wish to link into an executable. obj目录包含我希望链接到可执行文件的目标文件。 In the command I use the -L, -l and -rpath options which I thought was all that is needed for linking in linux. 在命令中,我使用-L,-l和-rpath选项,我认为这是在Linux中进行链接所需的全部。 It seems I am wrong since I get errors like: 由于出现类似以下的错误,看来我错了:

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

CBNewByteArrayFromString is found in the library. 在库中找到CBNewByteArrayFromString。 For some reason it is not being linked. 由于某种原因,它没有被链接。 OpenSSL too: 也是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. 谢谢。

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. 如果将对象文件放在第一位,则不会遇到此问题。

我认为是由于找不到符号引起的,gcc首先会从左边穿过,尝试将lib文件放在末尾

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM