Recompile with -fPIC option, but the option is already in the makefile

偶尔善良 提交于 2019-11-30 21:43:25

问题


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.


回答1:


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.




回答2:


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!




回答3:


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




回答4:


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.




回答5:


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.




回答6:


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 



回答7:


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.



来源:https://stackoverflow.com/questions/332767/recompile-with-fpic-option-but-the-option-is-already-in-the-makefile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!