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

查看:655
本文介绍了在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的目录有我想链接到一个可执行的目标文件。在命令我使用,我认为是需要在Linux中连接所有的-L,-l和-rpath选项。看来我错了,因为我得到这样的错误:

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'

我怎么联系起来的工作?

How do I get the linking to work?

版本的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天全站免登陆