Linux 上的链接器错误:“未定义的引用" [英] Linker error on Linux: "undefined reference to"

查看:30
本文介绍了Linux 上的链接器错误:“未定义的引用"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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

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

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:

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

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

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

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

如何使链接正常工作?

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

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

在 Linux Mint 13 上

On Linux Mint 13

谢谢.

推荐答案

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

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.

这篇关于Linux 上的链接器错误:“未定义的引用"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆