首页 > 解决方案 > 如何让 ld 使用 PIC 版本的 libc.a

问题描述

要将gcc静态链接到共享库中,根据this question中的答案,剩下的问题是如何让链接器使用libc.a的PIC版本而不是非PIC版本。问题与该答案相同:

g++ -fPIC -Wall -O0 -fexceptions -g -c main.cpp -o main.o
ld -shared -static -o test.so main.o -lc
ld: //usr/lib/x86_64-linux-gnu/libc.a(malloc.o): relocation R_X86_64_TPOFF32 against `tcache' can not be used when making a shared object; recompile with -fPIC

这是我尝试过的:

sudo apt-get install libc-pic //then libc6-pic get installed successfully
ld -shared -static -o test.so main.o -lc //same as above, same error
ld -shared -static -o test.so main.o -lc-pic //not working: cannot find -lc-pic
ld -shared -static -o test.so main.o -lc6-pic //not working: cannot find -lc6-pic

标签: clinkerldstatic-linkinglibc

解决方案


查看包中的文件列表有一个libc_pic.a所以正确的选项似乎是-lc_pic.


推荐阅读