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

xinde 2013-04-07 08:48:58
有一静态库文件为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 出错原因
...全文
199 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
xinde 2013-04-08
  • 打赏
  • 举报
回复
编译libtest.a时也带上fPIC了,先前已经试过了
mayer 2013-04-08
  • 打赏
  • 举报
回复
直接用libtest.a,而不用-ltest试试 另外,libtest.a这个是c写的,还是C++写的啊
Linux-Torvalds 2013-04-08
  • 打赏
  • 举报
回复
-Wl,-Bstatic -ltest -Wl,-Bdynamic -ldl
qq120848369 2013-04-07
  • 打赏
  • 举报
回复
应该只能重新编译libtest.a带上fPIC了。
xinde 2013-04-07
  • 打赏
  • 举报
回复
更正: $g++ -fPIC -c calltest.cpp (编译calltest.cpp 得到 .o )
qq120848369 2013-04-07
  • 打赏
  • 举报
回复
-c才能生成.o吧。

23,118

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧