Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
434 views
in Technique[技术] by (71.8m points)

c++ - g++ compile error: `.rodata' can not be used when making a shared object; recompile with -fPIC

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...