Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

972
Views
relocation against xxx in read-only section '.text' - wrong compiler or linux setup in SUSE?

I'm not a frequent user of Linux and I think I did something wrong.

This is the code for a test dynamic library ".so" I'm generating.

class InternalClass
{
public:
    int Function(){ return 10; }
};

extern "C"
{
    int WrapperFunctionSimple() { return 10; }
    void WrapperCreateInstance() {InternalClass* item = new InternalClass(); delete item; }
}

The compilation fails with the following error:

g++ -Wall -fexceptions -O2  -c /home/lidia/compartida/TestLibrary/TestLibrary/main.cpp -o obj/Release/main.o
g++ -shared  obj/Release/main.o  -o bin/Release/libTestLibrary.so -s  
/usr/lib64/gcc/x86_64-suse-linux/7/../../../../x86_64-suse-linux/bin/ld: obj/Release/main.o: warning: relocation against `_Znwm@@GLIBCXX_3.4' in read-only section `.text'
/usr/lib64/gcc/x86_64-suse-linux/7/../../../../x86_64-suse-linux/bin/ld: obj/Release/main.o: relocation R_X86_64_PC32 against symbol `_Znwm@@GLIBCXX_3.4' can not be used when making a shared object; recompile with -fPIC
/usr/lib64/gcc/x86_64-suse-linux/7/../../../../x86_64-suse-linux/bin/ld: final link failed: bad value
collect2: error: ld returned 1 exit status

I tried with -fPIC as suggested and it compiles. But when using this library, It cannot be loaded when I add that last function:

void WrapperCreateInstance() {InternalClass* item = new InternalClass(); delete item; }

The problem is using the InternalClass, without this function everything works.


I'm using VirtualBox. I installed OpenSUSE 64bit, the app that uses the library is also 64bit. In another linux distribution (Mint), with exactly the same project and settings (without fPIC), it can be compiled. When I use that library (.so) it works in SUSE.

I'm also using :

  • gcc (SUSE Linux) 7.5.0
  • g++ (SUSE Linux) 7.5.0
  • My IDE is Code::Blocks 20 (last version). Settings are empty except for the -m64 flag.

What am I doing wrong? This seems like something advanced Linux users could help me understand.

EDIT: To add more information, this can compile in Ubuntu with the same settings. Not in SUSE

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

This usually means that you don't have all functions defined. A better error message would be nice, but this is what we have for now. Define all used functions, and you should be good to go.

over 4 years ago · Santiago Trujillo Report

0

It happened to me when I used GCC to compile a CPP file. So for C++ files use g++ only, and obviously not GCC, which is meant for c files.

over 4 years ago · Santiago Trujillo Report

0

To me it happened, if one library (A) depends on another one (B) and library A was linked before library B. The solution is to link library B first and then A.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!