100分求解: g++编译输出.so连接.a文件出错

xinde xinde 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
有一静态库文件为libtest.a, 现在需要写一个程序来加载该.a库,执行下列编译命令时出错:
$g++ -fPIC calltest.cpp (编译calltest.cpp 得到 .o )

$g++ -shared -o libcalltest.so calltest.o -fPIC -I./ -L./ -ltest -ldl

(出错如下:)
/usr/bin/ld: .//libtest.a(test1.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
.//libtest.a: could not read symbols: Bad value
collect2: ld returned 1 exit status



说明: libtest.a是有几个.o文件(如test1.o) ar得来的. 我写一个程序单独来call libtest.a,编译成可执行文件,编译通过,执行test也正常,编译命令如下.
g++ main.cpp -o test -fPIC -I./ -L./ -ltest -ldl

请各位大侠帮忙解答一下$g++ -shared -o libcalltest.so calltest.o -fPIC -I./ -L./ -ltest -ldl 出错原因
...全文
评论 6
收藏 已收藏
分享
返回顶部
xinde
Linux/Unix社区 23,224 成员 74,535 内容