110

I'm trying to compile this source code from the makefile in a VPS, but its not working. The VPS is a 64 Cent OS

Here's the full error

# make
gcc -c -O3 -w -DLINUX -I../SDK/amx/ ../SDK/amx/*.c
g++ -c -O3 -w -DLINUX -I../SDK/amx/ ../SDK/*.cpp
g++ -c -O3 -w -DLINUX -I../SDK/amx/ *.cpp
g++ -O2 -fshort-wchar -shared -o "TCP_V1.so" *.o
/usr/bin/ld: TCP-LINUX_V1.o: relocation R_X86_64_32 against `.rodata.str1.8' can not be     used when making a shared object; recompile with -fPIC
TCP-LINUX_V1.o: could not read symbols: Bad value
collect2: ld returned 1 exit status
make: *** [all] Error 1

Here's my makefile:

GPP=g++
GCC=gcc
OUTFILE="TCP_V1.so"

COMPILE_FLAGS=-c -O3 -w -DLINUX -I../SDK/amx/

all:
    $(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?

6
  • 9
    Did you try recompile with -fPIC? Commented Oct 14, 2013 at 16:44
  • Sorry but I'm not sure how to do this. Can't find anything about "-fPIC" on google. Commented Oct 14, 2013 at 16:49
  • 5
    Try something like COMPILE_FLAGS=-c -O3 -w -DLINUX -fPIC -I../SDK/amx/ Commented Oct 14, 2013 at 17:05
  • Related: stackoverflow.com/questions/6093547/… Commented Oct 22, 2015 at 20:16
  • 20
    if you search google for -fPIC you will certainly find nothing. Remove the minus or use quotation marks "-fPIC" otherwise you omit all results containing fPIC.
    – d00d
    Commented Jul 7, 2017 at 7:49

9 Answers 9

135

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.

3
  • 5
    Could you explain how to recompile with -fPIC? Commented Sep 10, 2014 at 18:10
  • 16
    @Beni Bogosel: This is very simple. You just add the -fPIC to all the compiler invocations for all the source files (translation units, e.g. *.cpp files) of the library. The concrete way of doing that depends on the build system that you use. For instance, in CMake you could issue set_target_properties(${LIBRARY_NAME} PROPERTIES POSITION_INDEPENDENT_CODE ON). In case of this guy (using plain old Make), he would have to do COMPILE_FLAGS+=-fPIC since he is using this variable to denote the set of compilation flags for all the source files of his library. Commented Sep 11, 2014 at 6:46
  • 1
    to enable -fPIC using configure: configure --enable-shared, see stackoverflow.com/a/850464/440403
    – camino
    Commented Jan 26, 2019 at 3:05
58

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.

5
  • 2
    Perfect, "it's been a file-not-found error in disguise." In my case the dependency was not installed yet.
    – Litty
    Commented Apr 9, 2017 at 8:51
  • +1 In my case, a newer copy of openssl was built manually and installed without shared libraries. The library I was trying to build was already compiled with -fPIC. Is there anyway that the compiler could recognize this error to give a less obscure error message, for example "Expected to find shared library libssl.so, but only found incompatible static library /usr/local/ssl/lib/libssl.a." ?
    – Rohan Mahy
    Commented Jul 11, 2017 at 3:36
  • 1
    Thanks. I had a make -j and parallel execution was not allowed for this software package. Commented Jun 13, 2018 at 8:39
  • In my case, I have this line "find_library(NGHTTP2_LIB NAMES libnghttp2.a libnghttp2.so libnghttp2.dylib)". and it was only picking up the .a one, I later changed it to find_library(NGHTTP2_LIB NAMES libnghttp2.so libnghttp2.a libnghttp2.dylib)" and it started working. My concern is, what it use to work before for me?
    – Nabz C
    Commented Apr 29, 2020 at 6:49
  • 1
    @NabaChinde I am afraid I do not have an answer for your question also because I lack context information on your outcomes. I would certainly encourage to post a separate question in which you explain your working situation and the unexpected behaviour. Commented Apr 29, 2020 at 8:48
22

Fixed it with -no-pie option in linker stage:

g++-8 -L"/home/pedro/workspace/project/lib" -no-pie ...
1
  • This was all I needed to get successful compilation as well
    – KhanKhuu
    Commented Jun 20, 2022 at 19:22
11

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

4

Simply cleaning the project solved it for me.

My project is a C++ application (not a shared library). I randomly got this error after a lot of successful builds.

2

I had the same problem. Try recompiling using -fPIC flag.

0
0

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.

0

We had the same problem. It turned out to be a mix-up in a Makefile. The error occurred when the linker was gcc, but the C++ compiler clang++. Changing the linker to clang++ fixed it.

0

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

all:
    @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:

all:
      @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

Not the answer you're looking for? Browse other questions tagged or ask your own question.