C++ 针对“.rodata”重定位 R_X86_64_32S ... 在 64 位平台上编译时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9637551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Relocation R_X86_64_32S against `.rodata' ... While compiling on 64-bit platform
提问by Mare
So I've been coding something on 32-bit and yesterday I needed to build a dll and I had a couple of problems with that. Anyway I solved them here.
所以我一直在 32 位上编码一些东西,昨天我需要构建一个 dll,我遇到了一些问题。无论如何,我在这里解决了它们。
Unfortunately even if I thought that everything was working after all I found that wasn't the case when I moved my program and makefile on other computer what runs on 64bit, as you can guess what happened...
不幸的是,即使我认为一切都正常工作,但我发现当我将我的程序和 makefile 移动到其他运行在 64 位的计算机上时,情况并非如此,因为您可以猜到发生了什么......
So my problem is related to relocation because of 64bit
所以我的问题与 64 位有关
/usr/bin/ld: MyClass.o: relocation R_X86_64_32S against `.rodata' can not be used when making a shared object; recompile with -fPIC
MyClass.o: could not read symbols: Bad value
and here is my makefile
这是我的makefile
MyProgram: main.o chkopts
-${CLINKER} -o $@ $< ${MYLIB} ${PETSC_MAT_LIB}
${RM} main.o
export LD_LIBRARY_PATH=${LIBADD}:$LD_LIBRARY_PATH
LibMyProgram.so: MyClass.o chkopts
-${CLINKER} -shared -Wl,-soname,${SONAME} -o ${VERS} *.o ${PETSC_MAT_LIB}
mv ${VERS} ${LIBADD}
ln -sf ${LIBADD}${VERS} ${LIBADD}${SOWOV}
ln -sf ${LIBADD}${VERS} ${LIBADD}${SONAME}
I've tried to add -fPIC in CFLAGS, CPPFLAGS and even LDFLAGS. I've also tried add -fPIC before and after -shared flag.
我尝试在 CFLAGS、CPPFLAGS 甚至 LDFLAGS 中添加 -fPIC。我也试过在 -shared 标志之前和之后添加 -fPIC。
-${CLINKER} -shared -fPIC -Wl,-soname,${SONAME} -o ${VERS} *.o ${PETSC_MAT_LIB}
But I'll just get a same error as previously.
但我只会得到与以前相同的错误。
If I use CFLAGS = -fPIC I'll get a bit same kind of error which is:
如果我使用 CFLAGS = -fPIC 我会得到一些类似的错误,即:
.../petsc/petsc-3.2-p6/arch-linux2-cxx-debug/lib/libpetsc.a(err.o): relocation R_X86_64_32 against `ompi_mpi_comm_self' can not be used when making a shared object; recompile with -fPIC.
I've read about all the topics what are even remotely similar with my problem but I've been unable to figure this out.
我已经阅读了所有与我的问题非常相似的主题,但我一直无法弄清楚这一点。
采纳答案by Mare
The solution was to compile everything with -fPIC
, and link shared objects with -shared
.
解决方案是使用 编译所有内容-fPIC
,并将共享对象与-shared
.
Add -fPIC
to CFLAGS
or CXXFLAGS
for make-based projects.
添加-fPIC
到CFLAGS
或CXXFLAGS
用于基于 make 的项目。
回答by user2391685
I encountered the same problem when I try to create a shared library which need to link a static library.
当我尝试创建需要链接静态库的共享库时,我遇到了同样的问题。
I solved the problem by adding -fPIC to CXXFLAGS to compile .o files which are archived in the static library.
我通过将 -fPIC 添加到 CXXFLAGS 来编译存档在静态库中的 .o 文件解决了这个问题。
回答by Saeed
Trying to compile xmlrpc-c-1.06.41 in CentOS 6.5, I have encountered same linking problem, which was solved by the following: In ./src/cpp, I have modified Makefile: line 142 to
尝试在 CentOS 6.5 中编译 xmlrpc-c-1.06.41,我遇到了同样的链接问题,解决方法如下:在 ./src/cpp 中,我将 Makefile: line 142 修改为
CXXFLAGS = $(CXXFLAGS_COMMON) $(CFLAGS_PERSONAL) $(CADD) -shared -fPIC
More info regarding the flags can be found link
可以找到有关标志的更多信息链接
回答by Xin
If this problem still exist after adding "-fPIC",try clean all the .o files,and run again
如果添加“-fPIC”后仍然存在此问题,请尝试清理所有.o文件,然后再次运行
回答by Strong
I also meet this problem. As I try using @Mare and @user2391685 said, it can work well :
我也遇到这个问题。正如我尝试使用 @Mare 和 @user2391685 所说,它可以很好地工作:
Using -fPIC
when comepile to .o
file :
For example:
-fPIC
在编译到.o
文件时使用:例如:
gcc -Wall -fPIC -c hello.c -I./ -I/usr/lib/jvm/java/include/ -I/usr/lib/jvm/java/include/linux/
Then you can build a .so
file :
然后你可以构建一个.so
文件:
gcc -Wall -rdynamic -shared -o libhello.so hello.o Main.h -I/usr/lib/jvm/java/include/ -I/usr/lib/jvm/java/include/linux/
回答by Nur1Labs
this work as a charm. for who not know yet this easy used
这项工作作为一种魅力。谁还不知道这个好用
an open file called Makefile.amor Makefile. Just up to your config.
一个名为Makefile.am或Makefile的打开文件。就看你的配置了。
look the code at this _a_CXXFLAGS =or just CXXFLAGS =
看看这个_a_CXXFLAGS =或CXXFLAGS =的代码
add after that files -shared -fPIC
在该文件之后添加-shared -fPIC
this example
这个例子
before
前
crypto_libmubdi_crypto_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIC_FLAGS) $(CXXFLAGS_COMMON) $(CFLAGS_PERSONAL) $(CADD)
crypto_libmubdi_crypto_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIC_FLAGS) $(CXXFLAGS_COMMON) $(CFLAGS_PERSONAL) $(CADD)
after
后
crypto_libmubdi_crypto_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIC_FLAGS) $(CXXFLAGS_COMMON) $(CFLAGS_PERSONAL) $(CADD) -shared -fPIC
crypto_libmubdi_crypto_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIC_FLAGS) $(CXXFLAGS_COMMON) $(CFLAGS_PERSONAL) $(CADD) -shared -fPIC
these bugs cause we not put shared for the files or need -fPIC strings/tags.
这些错误导致我们没有为文件设置共享或需要 -fPIC 字符串/标签。
Note: I experience on to build my blockchain. and this cause added this crypto/sph_sha2big.c
注意:我有构建区块链的经验。这个原因添加了这个crypto/sph_sha2big.c