22

I get this error when I do the make:

relocation R_X86_64_32 against `vtable for Torch::MemoryDataSet' can not be used 
when making a shared object; recompile with -fPIC

It says that I should recompile with the -fPIC option. I did that, adding the -fPIC option to CFLAGS and CXXFLAGS, but I still get the same error. Is there any way to solve this? I have seen that this problem is related with the use of a 64-bit machine, and it is true that I am using one.

senderle
  • 145,869
  • 36
  • 209
  • 233
Eduardo
  • 19,928
  • 23
  • 65
  • 73
  • Is this an error from the linker or the compiler ? Did you clean all the object files before recompiling ? – philant Dec 02 '08 at 06:38
  • 1
    Yes it is an error from the compiler and yes I clean all the objects and I still get the same error. Thank you. – Eduardo Dec 02 '08 at 16:36
  • Related question http://stackoverflow.com/questions/1250608/passing-a-gcc-flag-through-makefile Should have checked yours before posting mine. – Arjun Singri Aug 09 '09 at 06:19
  • If you don't mind, please show the sequence of flags you used (if you can still find the original after 6 years :P) so that we know in future what to avoid... – icedwater Apr 14 '14 at 02:08

7 Answers7

12

I had this problem quite a while back and if I remember correctly, the fix was moving the placement of -fPIC just after gcc in the command line. Made absolutely no sense, and less so now, but as I remember, that fixed it.

clintm
  • 1,259
  • 10
  • 23
  • 1
    this did work for me aswell... adding -fPIC directly after gcc in Makefile i.e CC=gcc -pthread -fPIC – ashishsony Mar 10 '09 at 12:41
  • 1
    I encountered the same problem. moving "-fPIC" to directly after the "gcc" (as you wrote) worked for me as well. Puzzling. – cmo Jan 21 '13 at 15:12
  • What were the other flags you used with `gcc`? @ashishsony your example doesn't quite show `-fPIC` directly after `gcc` though. – icedwater Apr 14 '14 at 02:07
  • I can confirm this strange behaviour with `gcc (SUSE Linux) 4.6.4` – Cimbali Feb 03 '15 at 13:32
  • 1
    I had this issue, and it was caused by a library that I was linking against, which was not compiled with `-fPIC`. After adding `-fPIC` to that library's makefile, and recompiling it, linking worked fine. I know this is a different case, but noting for anyone who bumps into this. – 0 _ Jul 10 '15 at 00:17
5

I encountered the same problem, but it had an extra twist. The answer by @clintm solved it, but I thought I would describe my variation of the problem here for future reference...

Makefile on 32-bit machine:

CXX=g++
CXXFLAGS= -O3 -Wall
...
...   
%.o:  %.c
    $(CXX)  $(CXXFLAGS)  -fpic  -c  $<      

libmylibrary.so: $(OBJECTS)
    $(CXX) -shared -Wl,-soname,$@ -o $@   $(OBJECTS)

This compiled correctly. But the same Makefile failed when I tried it on a 64-bit machine. I changed "-fpic" to "-fPIC" and it still failed. I changed the object rule to:

%.o:  %.c
    $(CXX)  -fPIC  $(CXXFLAGS)  -c  $< 

and it still failed.

Finally, I placed "-fPIC" in the actual compiler variable (so that now "-fPIC" appears in the rule for each object and the rule for the shared library):

CXX=g++  -fPIC
CXXFLAGS= -g -O3 -Wall
...
%.o:  %.c
        $(CXX)    $(CXXFLAGS)   -c      -o $@    $<   

libalglib.so: $(OBJECTS)
        $(CXX) -shared -Wl,-soname,$@  -o $@      $(OBJECTS)

And it worked!

cmo
  • 3,762
  • 4
  • 36
  • 64
0

Let's say you have some makefile like:

CFLAGS = -g -Wall
SOURCES = $(wildcard *.c)
OBJECTS = ...

TARGET = libmyawesomelib.a

all: $(TARGET) main

just add the -fPIC flag like so:

$(TARGET): CFLAGS += -fPIC
$(TARGET): $(OBJECTS)
        .
        .
        .

so on so forth with the rest of the makefile.

mmienko
  • 91
  • 2
  • 4
0

I ran into this problem cross-compiling with the android-ndk toolchain. I ended up having to use

CC="$CROSS/bin/arm-linux-androideabi-gcc -pie --sysroot=$SYSROOT"

Neither -fPIC nor -fPIE worked for me in this situation.

jan
  • 181
  • 2
  • 7
0

I was cross compiling shadowsocks-libev on a CentOS 7 machine, the same problem happened to me, it works perfectly on my laptop with

CC=mipsel-unknown-linux-uclibc-gcc CXX=mipsel-unknown-linux-uclibc-g++ AR=mipsel-unknown-linux-uclibc-ar RANLIB=mipsel-unknown-linux-uclibc-ranlib make SHARED=1 CFLAGS=-fPIC

but on travis ci, it did not work, I have to add -fPIC to CC and CXX in order to get it to work

CC="mipsel-unknown-linux-uclibc-gcc -fPIC" CXX="mipsel-unknown-linux-uclibc-g++ -fPIC" AR=mipsel-unknown-linux-uclibc-ar RANLIB=mipsel-unknown-linux-uclibc-ranlib make SHARED=1 
Shuman
  • 3,914
  • 8
  • 42
  • 65
0

I had this issue after I upgraded gcc. I had one .o file (sqlite) that hadn't been cleaned by the Makefile and as a result I had this issue (assuming because it was compiled with an older version of gcc). After removing that file and rebuilding this error went away.

b.pell
  • 3,873
  • 2
  • 28
  • 39
-1

if the project you'd like to compile has a correct configure script use like this:

$ ./configure 'CFLAGS=-g -O2 -fPIC ....' --enable-some-thing

so the flag will be all the Makefile's rule ...

few days before i've need an elder ver. of VLC to compile on an x64 machine, it has a nice configure script ;-)

SoS
  • 1