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

两盒软妹~` 提交于 2019-12-01 02:46:49

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.

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!

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

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.

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.

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 

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.

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