与静态库链接的共享库:重定位错误 [英] Shared library linked with static library: relocation error
问题描述
出于可移植性的原因,我想使用gfortran创建一个共享库,并将其与libgfortran的静态版本链接.不幸的是,我无法适当地链接不同的对象.我已经找到了一些解决类似问题的帖子,但是我不知道如何解决该问题.
我的源文件都使用-fPIC
标志进行编译.当我尝试用标志-shared
和-static-libgfortran
链接对象时,出现以下错误消息:
gfortran -shared -static-libgfortran file1.o file2.o file3.o -o "mynewlib.so"
/usr/bin/ld: ../lib64/libgfortran.a(error.o): relocation R_X86_64_32 against `a local symbol' can not be used when making a shared object; recompile with -fPIC
../lib64/libgfortran.a: could not read symbols: Bad value
collect2: ld returned 1 exit status
我使用的是gfortran 4.6.3,该服务器是我从头开始编译的.安装运行顺利,我没有更改标准配置.我在旧帖子中发现应该使用--with-pic
配置编译器,但是 GCC手册.
问题是来自编译器还是我尝试链接不同对象的方式?知道我该如何解决吗?
非常感谢您的帮助!
使用静态版本时,libgfortran
不会使用-fPIC
标志进行编译(因此它不包含位置独立代码). /p>
因此,您不能将libfgortran
(静态)链接到共享对象.
我建议您正确打包库(例如,将其作为Debian或Ubuntu的.deb
文件),并将gfortran
添加到软件包的依赖项中.
I would like to create a shared library with gfortran, linking it with the static version of libgfortran for portability reasons. Unfortunately, I don't manage to link the different objects appropriately. I have already found some posts addressing a similar issue, but I could not figure out how to fix the problem.
My source files are all compiled with the -fPIC
flag. When I try to link the objects with the flags -shared
and -static-libgfortran
, I get the following error message:
gfortran -shared -static-libgfortran file1.o file2.o file3.o -o "mynewlib.so"
/usr/bin/ld: ../lib64/libgfortran.a(error.o): relocation R_X86_64_32 against `a local symbol' can not be used when making a shared object; recompile with -fPIC
../lib64/libgfortran.a: could not read symbols: Bad value
collect2: ld returned 1 exit status
I am using gfortran 4.6.3, that I compiled from scratch on my server. The installation ran smoothly and I did not change the standard configuration. I found on old posts that the compiler should be configured with --with-pic
, but this flag is not mentioned in the GCC manual.
Does the problem come from the compiler or from the way I am trying to link the different objects? Any idea how I can fix this?
Thank you very much in advance for your help!
The libgfortran
, when you use the static version, is not compiled with the -fPIC
flag (so it does not contain position indenpendent code).
Hence you cannot link libfgortran
(statically) to your shared object.
I suggest you to package your library properly (e.g. as a .deb
file for Debian or Ubuntu) and to add gfortran
to the dependencies of your package.
这篇关于与静态库链接的共享库:重定位错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!