g++ 编译错误:`.rodata' 不能在制作共享对象时使用;使用 -fPIC 重新编译 [英] g++ compile error: `.rodata' can not be used when making a shared object; recompile with -fPIC

查看:36
本文介绍了g++ 编译错误:`.rodata' 不能在制作共享对象时使用;使用 -fPIC 重新编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用命令:
g++ --std=c++11 -fPIC -Iincludes parser.cpp lib/main-parser.o lib/lib.a

在 Debian 9 上编译 C++ 程序.但我收到以下错误消息:<代码>/usr/bin/ld: lib/lib.a(csdocument.o): 重定位 R_X86_64_32 反对 '.rodata' 制作共享对象时不能使用;使用 -fPIC 重新编译/usr/bin/ld:最终链接失败:输出中不可表示的部分collect2:错误:ld 返回 1 个退出状态

我已经看到了线程:编译失败并出现 "relocation R_X86_64_32 against `.rodata.str1.8' 在制作共享对象时不能使用"

但是,我尝试添加 -fPIC 参数,但奇怪的是它给出了相同的错误消息,以及使用 -fPIC 重新编译"

However, I have tried adding the -fPIC argument however it strangely gives the same error message, along with "recompile with -fPIC"

任何想法将不胜感激.我试过在我大学的 RedHat 系统上编译它,它在那里工作得很好.我认为这可能是缺少依赖项,但我一直找不到任何答案.

Any ideas would be appreciated. I have tried compiling this on my University's RedHat systems and it works fine there. I'm thinking it could be a missing dependency, but I've been unable to find any answers.

提前致谢

推荐答案

看起来 gcc 正在尝试生成一个与位置无关的可执行文件(共享对象"是提示),告诉它不要:

As it seems gcc is trying to produce a position-independent executable ("shared object" is the hint), tell it not to:

g++ --std=c++11 -no-pie -Iincludes parser.cpp lib/main-parser.o lib/lib.a

似乎 g++ 默认情况下会在您的系统上生成与位置无关的可执行文件.其他系统需要 -pie 来执行此操作.使用 -no-pie 应该创建一个常规的";(取决于位置)可执行文件.

It seems that g++ produces position-independent executables by default on your system. Other systems would require -pie to do so. Using -no-pie should create a "regular" (position dependent) executable.

(该错误是由于尝试将编译为非位置无关的目标文件链接到应该与位置无关的可执行文件的结果).

(The error is a result of trying to link an object file that was compiled as non-position-independent into an executable that is supposed to be position-independent).

这篇关于g++ 编译错误:`.rodata' 不能在制作共享对象时使用;使用 -fPIC 重新编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆