软件平台及版本
操作系统:Ubuntu14.04.6
编辑器:英文版Codeblocks16.01
编译器:gcc4.8.4
问题描述
我在创建动态库,初次编译报如下错误(碰到好几次):
/usr/bin/ld: obj/Debug/EPack/source/EAes.o: relocation R_X86_64_32S against `_ZTV4EAes’ can not be used when making a shared object; recompile with -fPIC
obj/Debug/EPack/source/EAes.o: error adding symbols: Bad value
collect2: error: ld returned 1 exit status
问题分析
根据错误提示明显得知,我们需要添加编译选项-fPIC
问题描述中只是列出来最后的报错,前面还有若干条编译语句,如:
g++ -Wall -fexceptions -g -IEPack/include -c /work2/PyPro/WebDemo/pci_verify/EPack/source/EAes.cpp -o obj/Debug/EPack/source/EAes.o
g++ -Wall -fexceptions -g -IEPack/include -c /work2/PyPro/WebDemo/pci_verify/EPack/source/ECryptoSha1.cpp -o obj/Debug/EPack/source/ECryptoSha1.o
问题在于怎么在codeblocks修改,而且要在每一条编译语句中都要添加
解决方法
选择这个项目,
菜单,
Project -> Build options…->Compiler settings->Other compiler options
在出现的很大的提示框中写-fPIC
如下图:
注意区分Debug版本和Release版本,最好都配上。点击OK即完成配置。
再次全编即可成功生成so动态库文件。