Compilation fails with relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a shared object
Do what the compiler tells you to do, i.e. recompile with -fPIC
. To learn what does this flag do and why you need it in this case, see Code Generation Options of the GCC manual.
In brief, the term position independent code (PIC) refers to the generated machine code which is memory address agnostic, i.e. does not make any assumptions about where it was loaded into RAM. Only position independent code is supposed to be included into shared objects (SO) as they should have an ability to dynamically change their location in RAM.
Finally, you can read about it on Wikipedia too.
g++ compile error: `.rodata' can not be used when making a shared object; recompile with -fPIC
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).
How to compile file using scheme2c
PIE is enabled by default in Ubuntu's GCC (Ubuntu compiles GCC using the --enable-default-pie
option).
The problem can be solved by using GCC's -no-pie
option:
$ scc -cc 'gcc -no-pie' hello.sc
hello.sc:
$ ./a.out
Hello!
Relocation R_X86_64_32S against `.rodata' ... While compiling on 64-bit platform
The solution was to compile everything with -fPIC
, and link shared objects with -shared
.
Add -fPIC
to CFLAGS
or CXXFLAGS
for make-based projects.
Related Topics
How to Avoid the Diamond of Death When Using Multiple Inheritance
About Binding a Const Reference to a Sub-Object of a Temporary
Stack Overflow Caused by Recursive Function
Why Are Memcpy() and Memmove() Faster Than Pointer Increments
C++11: How to Alias a Function
How to Remove Constness of Const_Iterator
Implementing a No-Op Std::Ostream
Why Does Calling Std::String.C_Str() on a Function That Returns a String Not Work
Are Child Processes Created with Fork() Automatically Killed When the Parent Is Killed
Hide User Input on Password Prompt
Opencv Error: Assertion Failed (Size.Width>0 && Size.Height>0) Simple Code
Difference Between Shared Objects (.So), Static Libraries (.A), and Dll's (.So)
Is There a Function to Copy an Array in C/C++
Performance of Qsort VS Std::Sort
Why Can't Static_Cast Be Used to Down-Cast When Virtual Inheritance Is Involved