Shared library linked with static library: relocation error
Asked Answered
H

1

3

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!

Haruspicy answered 20/10, 2012 at 14:20 Comment(0)
P
3

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.

Plectron answered 20/10, 2012 at 14:23 Comment(4)
That was my guess, but how to get a version of libgfortran compiled with -fPIC?Haruspicy
You might consider compiling GCC from its source code, and tweaking manually some flags (perhaps with ../gcc-4.7/configure CFLAGS='-fPIC' ....) but I really believe it is not worth the effort.Plectron
More details on my problem: I am sharing this library on a server where a very old version of GCC is installed, which is too old to compile my code. I just installed a more recent version of GCC to solve this problem, but then my colleagues would also have to have access to the shared version of libgfortran. I thought it would be easier to have a self-contained shared library. Would you rather not do that? What's the alternative? Sharing libgfortran with them as well?Haruspicy
Share the entire GCC 4.7 compiler suite with your colleagues. Ask help from, and tell about this issue, your sysadmin.Plectron

© 2022 - 2024 — McMap. All rights reserved.