g linux 未定义的引用_在Linux上链接错误:"未定义的引用"

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

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 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 version: gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3

On 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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值