使用 -fPIC 选项重新编译,但该选项已在 makefile 中

发布于 2024-07-09 19:08:57 字数 372 浏览 8 评论 0原文

当我执行 make 时,出现此错误:

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

它表示我应该使用 -fPIC 选项重新编译。 我这样做了,添加 CFLAGSCXXFLAGS-fPIC 选项,但我仍然遇到相同的错误。 有什么办法可以解决这个问题吗? 我看到这个问题与使用64位机器有关,确实我正在使用一台。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

时光是把杀猪刀 2024-07-16 19:08:57

我很久以前就遇到过这个问题,如果我没记错的话,修复方法是将 -fPIC 的位置移到命令行中 gcc 之后。 完全没有意义,现在更没有意义,但据我记得,这解决了它。

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.

滿滿的愛 2024-07-16 19:08:57

我遇到了同样的问题,但它有一个额外的转折。 @clintm 的答案解决了这个问题,但我想我应该在这里描述我的问题变体以供将来参考...

32 位机器上的 Makefile:

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

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

编译正确。 但是当我在 64 位机器上尝试时,相同的 Makefile 失败了。 我将“-fpic”更改为“-fPIC”,但仍然失败。 我将对象规则更改为:

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

仍然失败。

最后,我将“-fPIC”放入实际的编译器变量中(以便现在“-fPIC”出现在每个对象的规则共享库的规则中):

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

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

它起作用了!

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

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

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 $@    
lt;   

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

And it worked!

皇甫轩 2024-07-16 19:08:57

假设您有一些 makefile,例如:

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

TARGET = libmyawesomelib.a

all: $(TARGET) main

只需添加 -fPIC 标志,如下所示:

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

对于 makefile 的其余部分依此类推。

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.

不打扰别人 2024-07-16 19:08:57

我在与 android-ndk 工具链交叉编译时遇到了这个问题。 我最终不得不使用

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

-fPIC-fPIE 在这种情况下都不适合我。

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.

送舟行 2024-07-16 19:08:57

我在 CentOS 7 机器上交叉编译shadowsocks-libev,同样的问题发生在我身上,它在我的笔记本电脑上完美运行,

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

但在 travis ci 上,它不起作用,我必须将 -fPIC 添加到 CC 和 CXX 才能让它发挥作用

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 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 
记忆消瘦 2024-07-16 19:08:57

升级gcc后我遇到了这个问题。 我有一个 .o 文件(sqlite)尚未被 Makefile 清理,因此我遇到了这个问题(假设因为它是用旧版本的 gcc 编译的)。 删除该文件并重建后,此错误消失了。

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.

弱骨蛰伏 2024-07-16 19:08:57

如果您要编译的项目有一个正确的配置脚本,请使用如下所示:

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

因此该标志将是所有 Makefile 的规则...

几天前我需要一个较旧的版本。 VLC 在 x64 机器上编译,它有一个很好的配置脚本;-)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文