GPP=g++
GCC=gcc
OUTFILE="TCP_V1.so"
COMPILE_FLAGS=-c -O3 -w -DLINUX -I../SDK/amx/
$(GCC) $(COMPILE_FLAGS) ../SDK/amx/*.c
$(GPP) $(COMPILE_FLAGS) ../SDK/*.cpp
$(GPP) $(COMPILE_FLAGS) *.cpp
$(GPP) -O2 -fshort-wchar -shared -o $(OUTFILE) *.o
Anyone know what's wrong?
–
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.
–
–
In my case this error occurred because a make
command was expecting to fetch shared libraries (*.so
files) from a remote directory indicated by a LDFLAGS
environment variable. In a mistake, only static libraries were available there (*.la
or *.a
files).
Hence, my problem did not reside with the program I was compiling but with the remote libraries it was trying to fetch.
So, I did not need to add any flag (say, -fPIC
) to the compilation interrupted by the relocation error.
Rather, I recompiled the remote library so that the shared objects were available.
Basically, it's been a file-not-found error in disguise.
In my case I had to remove a misplaced --disable-shared
switch in the configure
invocation for the requisite program, since shared and static libraries were both built as default.
I noticed that most programs build both types of libraries at the same time, so mine is probably a corner case. In general, it may be the case that you rather have to enable shared libraries, depending on defaults.
To inspect your particular situation with compile switches and defaults, I would read out the summary that shows up with ./configure --help | less
, typically in the section Optional Features. I often found that this reading is more reliable than installation guides that are not updated while dependency programs evolve.
–
–
–
–
–
It is not always about the compilation flags, I have the same error on gentoo when using distcc.
The reason is that on distcc server is using a not-hardened profile and on client the profile is hardened. Check this discussion:
https://forums.gentoo.org/viewtopic-p-7463994.html
I'm getting the same solution as @camino's comment on https://stackoverflow.com/a/19365454/10593190 and XavierStuvw's reply.
I got it to work (for installing ffmpeg) by simply reinstalling the whole thing from the beginning with all instances of $ ./configure
replaced by $ ./configure --enable-shared
(first make sure to delete all the folders and files including the .so files from the previous attempt).
Apparently this works because https://stackoverflow.com/a/13812368/10593190.
I know that this query was asked a year ago.
I tried the given error at my system.
I tried simulating the given error using Make file providing this error.
Error Make file(each line begins using a tab instead of multiple spaces):
@echo "/usr/bin/gcc -c -O3 -w -DLINUX -I../SDK/amx/ ../SDK/amx/*.c";\
/usr/bin/gcc -c -O3 -w -DLINUX -I../SDK/amx/ ../SDK/amx/*.c;\
echo "/usr/bin/g++ -c -O3 -w -DLINUX -I../SDK/amx/ ../SDK/*.cpp";\
/usr/bin/g++ -c -O3 -w -DLINUX -I../SDK/amx/ ../SDK/*.cpp;\
echo "/usr/bin/g++ -c -O3 -w -DLINUX -I../SDK/amx/ *.cpp";\
/usr/bin/g++ -c -O3 -w -DLINUX -I../SDK/amx/ *.cpp;\
echo "/usr/bin/g++ -O2 -shared -o "TCP_V1.so" *.o";\
/usr/bin/g++ -O2 -shared -o "TCP_V1.so" *.o
Error:
/usr/bin/gcc -c -O3 -w -DLINUX -I../SDK/amx/ ../SDK/amx/*.c
/usr/bin/g++ -c -O3 -w -DLINUX -I../SDK/amx/ ../SDK/*.cpp
/usr/bin/g++ -c -O3 -w -DLINUX -I../SDK/amx/ *.cpp
/usr/bin/g++ -O2 -shared -o TCP_V1.so *.o
/usr/bin/ld: one.o: relocation R_X86_64_32 against `.rodata.str1.1'
can not be used when making a shared object; recompile with -fPIC
one.o: error adding symbols: Bad value
collect2: error: ld returned 1 exit status
Updated Make file:
@echo "/usr/bin/gcc -fPIC -c -O3 -w -DLINUX -I../SDK/amx/ ../SDK/amx/*.c";\
/usr/bin/gcc -fPIC -c -O3 -w -DLINUX -I../SDK/amx/ ../SDK/amx/*.c;\
echo "/usr/bin/g++ -fPIC -c -O3 -w -DLINUX -I../SDK/amx/ ../SDK/*.cpp";\
/usr/bin/g++ -fPIC -c -O3 -w -DLINUX -I../SDK/amx/ ../SDK/*.cpp;\
echo "/usr/bin/g++ -fPIC -c -O3 -w -DLINUX -I../SDK/amx/ *.cpp";\
/usr/bin/g++ -fPIC -c -O3 -w -DLINUX -I../SDK/amx/ *.cpp;\
echo "/usr/bin/g++ -O2 -shared -o "TCP_V1.so" *.o";\
/usr/bin/g++ -O2 -shared -o "TCP_V1.so" *.o
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.