26

I am using the command:
g++ --std=c++11 -fPIC -Iincludes parser.cpp lib/main-parser.o lib/lib.a

To compile a C++ program on Debian 9. But I am getting the below error message: /usr/bin/ld: lib/lib.a(csdocument.o): relocation R_X86_64_32 against '.rodata' can not be used when making a shared object; recompile with -fPIC /usr/bin/ld: final link failed: Nonrepresentable section on output collect2: error: ld returned 1 exit status

I have already seen the thread: Compilation fails with "relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a shared object"

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

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.

Thanks in advance

Polymer
  • 1,108
  • 1
  • 9
  • 17
  • 1
    How did you compile main-parser.o ? – Daniel Trugman Oct 19 '17 at 10:11
  • Unfortunately, I wouldn't be able to provide information on that, as it was pre-compiled and we don't receive information on how it was compiled – Polymer Oct 19 '17 at 10:16
  • If the haven't used `-fPIC` it could explain the error... – Daniel Trugman Oct 19 '17 at 10:27
  • 1
    I get the same error message without using `-fPIC`, too – Polymer Oct 19 '17 at 10:27
  • @Polymer that you get the same error with and without `-fPIC` implies that it is one of the already-compiled objects that has the problematic relocation. You won't be able to link such an object into a shared library. – davmac Oct 19 '17 at 10:28
  • It doesn't matter, because shared objects (SO) are supposed to use position independent code only(!) as they should have an ability to dynamically change their location in RAM – Daniel Trugman Oct 19 '17 at 10:29
  • 1
    Are you trying to build a shared object? There seems to be nothing shared object related in your command line. – n. m. could be an AI Oct 19 '17 at 10:40
  • In fact the problematic object is given in the error message: `lib/lib.a(csdocument.o)`. How is that built? Also as n.m. notes your command line doesn't specify to build a shared object. Is that really the _complete_ command line you are using? – davmac Oct 19 '17 at 10:58
  • I really wouldn't be able to tell you how it is compiled as we don't get that information, the sources are already pre-compiled for us. And yes, `g++ --std=c++11 -fPIC -Iincludes -o jparser parser.cpp lib/main-parser.o lib/lib.a` is the entire input. I am currently in the process of trying the same code on a Debian 8 fresh install (never know) – Polymer Oct 20 '17 at 09:34

3 Answers3

36

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

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).

davmac
  • 20,150
  • 1
  • 40
  • 68
10
/usr/bin/ld: lib/lib.a(csdocument.o): relocation R_X86_64_32 against '.rodata' \
can not be used when making a shared object; recompile with -fPIC

This linker error is telling you that the object file csdocument.o in the static library lib/lib.a is not Position Independent Code and hence cannot be linked with your PIE program. So you need to recompile the source files of lib/lib.a with -fPIC, then rebuild the static library, then link it with your PIE program. If you don't have control of the libary sources then request a PIC build from its supplier.

(Others have questioned why you should need to build a PIE target at all since it's not a shared library. In Debian 9, GCC produces PIE executables by default, whether programs or shared libraries. The same goes for Ubuntu as of 17.04. )

Mike Kinghan
  • 55,740
  • 12
  • 153
  • 182
  • Hi, I am having this problem and I don't know how to get the PIC build and use it. Could you please help in my question? https://stackoverflow.com/questions/52499568/relocation-r-x86-64-32-against-xxx-can-not-be-used-when-making-a-shared-object – Felipe Sep 25 '18 at 14:50
  • Quoting that Debian doc: "Note that even though the error message says `-fPIC`, it is sufficient to recompile with `-fPIE` (which is the default in the GCC 6 packages that are part of stretch)." – Beni Cherniavsky-Paskin Sep 19 '19 at 11:01
1

Adding this worked for me.

g++ --std=c++11 -no-pie

I also added the -fPIC to compile flag.