首页 > 解决方案 > 使用 -fPIC 重新编译

问题描述

我写了这个命令:

gcc drr.c -o drr -pthread -lcdsl -L./../synch_implementations -I./../synch_implementations

我不明白为什么我会得到这个回复:

usr/bin/ld: ./../synch_implementations/libcdsl.a(cdsl_queue_list.o): relocation R_X86_64_32 against `.rodata' can not be used when making a PIE object; recompile with -fPIC
usr/bin/ld: ./../synch_implementations/libcdsl.a(cdsl_deque_list.o): relocation R_X86_64_32S against symbol `cdsl_deque_list_lock_based_pthread_push_tail' can not be used when making a PIE object; recompile with -fPIC
usr/bin/ld: ./../synch_implementations/libcdsl.a(cdsl_lock.o): relocation R_X86_64_32S against symbol `cdsl_pthread_lock_request' can not be used when making a PIE object; recompile with -fPIC
usr/bin/ld: final link failed: Nonrepresentable section on output
ollect2: error: ld returned 1 exit status

谁能帮我理解什么是错的?

笔记:

1)我使用ubuntu(通过VirtualBox)。

2)我非常缺乏经验,所以这可能不是一个很聪明的问题。

标签: fpic

解决方案


解决方案:当您编译 ./../synch_implementations/libcdsl.a 时,您需要添加一个额外的“-fPIC”标志来生成与位置无关的代码(参见上面的注释)。

注意:生成库对象代码时通常需要 PIC 标志,因为它创建相对于全局偏移表 (GOT) 的对象,随后允许将库对象与您的代码链接。


推荐阅读