问题
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!
回答1:
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.
来源:https://stackoverflow.com/questions/12989334/shared-library-linked-with-static-library-relocation-error