LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-20-2010, 09:04 AM   #1
aryan1
Member
 
Registered: Jul 2009
Posts: 50

Rep: Reputation: 16
Question "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)

Or is it something else ?

Thanks.
 
Old 01-20-2010, 09:19 AM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
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
 
Old 01-20-2010, 01:39 PM   #3
aryan1
Member
 
Registered: Jul 2009
Posts: 50

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by johnsfine View Post
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.
 
Old 01-20-2010, 01:47 PM   #4
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by aryan1 View Post
Is the side-effect of not using -fPIC when compiling a static library which goes into a shared object platform-dependent ?
Yes. I assumed we were just talking about x86_64.

I don't have as good an understanding of this question on any other platform.
 
Old 01-20-2010, 02:03 PM   #5
aryan1
Member
 
Registered: Jul 2009
Posts: 50

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by johnsfine View Post
Yes. I assumed we were just talking about x86_64.
Sorry. I am also talking about x86. By "different platforms", I meant different machines.

Let me clarify my question. The way of compilation above succeeded in one x86 machine, but it failed on another x86 machine.

So how come does the lack of -fPIC usage cause an error on one x86 machine, but not on another x86 ?

The one where the compilation fails is a 64-bit machine. I am not sure about the other one.

And, does the usage of g++ and gcc together like above work all the times ?
 
Old 01-20-2010, 03:13 PM   #6
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by aryan1 View Post
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.
 
Old 01-21-2010, 01:00 AM   #7
aryan1
Member
 
Registered: Jul 2009
Posts: 50

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by johnsfine View Post
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 ?
 
Old 01-21-2010, 08:26 AM   #8
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
I'm pretty sure -fPIC will work OK outside of shared libraries, but it may generate code that is less efficient and also harder to debug.

I would suggest not making -fPIC always part of the CFLAGS.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
relocation R_X86_64_32 against `a local symbol' can not be used when making a shared jimmyjiang Linux - Software 3 07-12-2007 11:12 PM
avgscan "relocation error" "undefined symbol: __dynamic_cast_2" Monster_user Linux - Software 0 02-22-2006 11:57 AM
Knetmapper produces "relocation" error when I try to run it Mel_P Mandriva 2 04-14-2004 05:12 PM
Knetmapper produces "relocation" error when I try to run it Mel_P Linux - Software 0 04-14-2004 03:48 AM
"relocation error" when launching gtkpod on rh9 tektone Linux - Software 1 12-18-2003 02:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration