"relocation R_X86_64_32 against `.bss' can not be used when making a shared..." error
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
"relocation R_X86_64_32 against `.bss' can not be used when making a shared..." error
Hi All,
I am getting "relocation R_X86_64_32 against `.bss' can not be used when making a shared object; recompile with -fPIC" error when compiling a shared object with optimization option -O2 enabled using GNU GCC:
First, I compile a static library, which is used by the shared object, as follows:
Code:
g++ -g -O2 -c common/src/packet-class-registry.cc
ar -cvq libclassreg.a common/src/packet-class-registry.o
Below is how I compile the shared object, which uses libclassreg.a:
Code:
g++ -g -O2 -fPIC -c packet-v4.cc -o packet-v4.o
gcc -shared -Wl,-soname,libv4.so.1 -o libv4.so.1.0 packet-v4.o -Lcommon/lib -Lconf/configLib/lib -lclassreg -lNsConf
/usr/bin/ld: common/lib/libclassreg.a(packet-class-registry.o): relocation R_X86_64_32 against `.bss' can not be used when making a shared object; recompile with -fPIC
common/lib/libclassreg.a: could not read symbols: Bad value
collect2: ld returned 1 exit status
make: *** [libv4.so] Error 1
Is this a problem regarding the order of compiler options ? (-O2 appears before -fPIC)
In that situation, you need -fPIC when compiling the code that goes into the .a file, not just when compiling the main code of the .so file.
So the first g++ command you quoted above also needs -fPIC
Is the side-effect of not using -fPIC when compiling a static library which goes into a shared object platform-dependent ?
I am asking this since the way of compilation denoted in my first thread works "without" any problem on two different machines. That is, -fPIC is not used to compile .a file, which is linked into a shared object. However, on another machine, the same compilation fails.
Sorry. I am also talking about x86. By "different platforms", I meant different machines.
By different platforms, I meant things like x86 vs. x86_64.
Quote:
So how come does the lack of -fPIC usage cause an error on one x86 machine, but not on another x86 ?
It failed on x86_64. I don't know what would happen on x86 without that -fPIC option when building the .a.
The fact that x86_64 hardware can run your choice of x86 or x86_64 architecture, does not make x86 and x86_64 the same "platform".
Quote:
The one where the compilation fails is a 64-bit machine. I am not sure about the other one.
32 bit is x86 architecture.
Maybe there is a machine specific difference in the way g++ is installed to make -fPIC the default on one system, so you don't need to specify it. That is unlikely, but not impossible.
More likely, the relevant difference is x86 (32 bit) vs. x86_64.
Quote:
And, does the usage of g++ and gcc together like above work all the times ?
g++ and gcc are very similar front ends to the same compile and link tools. When compiling the choice of command g++ vs. gcc determines whether the compiler is expecting C++ or C code. But when linking (as you seem to be in your use of GCC) I don't know of anything different that the gcc command would do compared to a g++ command.
More likely, the relevant difference is x86 (32 bit) vs. x86_64.
I agree.
I found out that the machine where the compilation without -fPIC option succeeds has an 32-bit OS. On the other hand, the other one where it fails is x86_64 with an 64-bit OS.
As a general principle, is it OK if I make -fPIC part of all of my CFLAGS settings ?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.