关于linux中so链接a

这段由于工作需要,做成链接库文件给别人调用,但是只想提供一个so文件,于是实现在so文件中链接.a文件,写了一个测试程序,编译脚本如下:


g++ -fPIC hello.cpp -c -o hello.o
ar rcs libhello.a hello.o
g++ -fPIC -shared -o libhello_s.so hello_s.cpp -L. -lhello
g++ -o main -g main.cpp -L. -lhello_s


刚刚开始在编译静态库的时候,没有加入-fPIC参数,编译libhello_s.so文件时,出现如下错误:

/usr/bin/ld: ./libhello.a(hello.o): relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
./libhello.a: could not read symbols: Bad value


百度查找资料-fPIC这个参数,是代码段重新定位,猜想链接库的地址只能够是相对地址,so链接.a文件也使用的相对地址,所以.a需要加上-fPIC编译

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章