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。因此(没有错误)并尝试与可执行文件和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.
谢谢你!
2 个解决方案
#1
26
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.
如果您不这样做,链接器可能会在它扫描库的链接阶段决定它不需要任何特定库,然后在它在对象文件中发现一些未定义的符号之后,它不会重新扫描库。如果将对象文件放在前面,就不会遇到这个问题。
#2
1
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文件放在末尾
#1
26
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.
如果您不这样做,链接器可能会在它扫描库的链接阶段决定它不需要任何特定库,然后在它在对象文件中发现一些未定义的符号之后,它不会重新扫描库。如果将对象文件放在前面,就不会遇到这个问题。
#2
1
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文件放在末尾