交叉编译 C++ 项目,通用 ELF 中的重定位(EM:3)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8168950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cross compiling C++ project, Relocations in generic ELF (EM: 3)
提问by Matt
I've been working on a c++ project for a while now, but would like to port it over to my arm processor. I already have all of my cross-compile tools (I'm using CodeSourcery) and thought I could just change my makefile to point to that compiler. It compiles fine using the default g++, but When try a make pointing to the cross-compiler I get relocation errors:
我已经在 C++ 项目上工作了一段时间,但想将它移植到我的 arm 处理器上。我已经拥有了所有的交叉编译工具(我正在使用 CodeSourcery),并认为我可以更改我的 makefile 以指向该编译器。它使用默认的 g++ 编译得很好,但是当尝试指向交叉编译器的 make 时,我得到重定位错误:
/home/oryan/CodeSourcery/bin/../lib/gcc/arm-none-linux-gnueabi/4.5.2/../../../../arm-none-linux-gnueabi/bin/ld: ServerSocket.o: Relocations in generic ELF (EM: 3)
ServerSocket.o: could not read symbols: File in wrong format
collect2: ld returned 1 exit status
make: *** [simple_server] Error 1
/home/oryan/CodeSourcery/bin/../lib/gcc/arm-none-linux-gnueabi/4.5.2/../../../../arm-none-linux-gnueabi/bin/ ld:ServerSocket.o:通用 ELF 中的重定位(EM:3)
ServerSocket.o:无法读取符号:文件格式错误
collect2:ld 返回 1 退出状态
make:*** [simple_server] 错误 1
It seems like I don't have a proper link set up or it's pointing to a wrong location. I'm not that familiar with makefiles and am probably missing something obvious. The makefile I've been using is from http://tldp.org/LDP/LG/issue74/tougher.htmlwith the client side removed:
似乎我没有设置正确的链接,或者它指向错误的位置。我对 makefile 不太熟悉,可能遗漏了一些明显的东西。我一直在使用的 makefile 来自http://tldp.org/LDP/LG/issue74/tougher.html删除了客户端:
# Makefile for the socket programming example
#
simple_server_objects = ServerSocket.o Socket.o simple_server_main.o
all : simple_server
simple_server: $(simple_server_objects)
/home/matt/CodeSourcery/bin/arm-none-linux-gnueabi-g++ -o simple_server $(simple_server_objects)
Socket: Socket.cpp
ServerSocket: ServerSocket.cpp
simple_server_main: simple_server_main.cpp
clean:
rm -f *.o simple_server
Right now I am manually compiling each file and it works great, but I'd like to further my understanding here.
现在我正在手动编译每个文件并且效果很好,但我想在这里进一步了解。
Thanks!
谢谢!
回答by Rup
The problem is you've set your makefile up to link with the new g++ but you haven't changed the compiler you're using to build the objects in the first place.
问题是您已经将 makefile 设置为与新的 g++ 链接,但您没有更改用于构建对象的编译器。
The easiest way to fix this is to set environment CXX
to the next compiler, i.e.
解决此问题的最简单方法是将环境设置CXX
为下一个编译器,即
export CXX=/home/matt/CodeSourcery/bin/arm-none-linux-gnueabi-g++
or just set it for a given make
by adding CXX=...
to the command line.
或者只是make
通过添加CXX=...
到命令行来为给定设置它。
You'll need to make clean
first but you'll then use the correct compiler for both the compile and link.
您make clean
首先需要这样做,然后您将使用正确的编译器进行编译和链接。
You could also specify a new how-to-compile-C++ files rule in your makefile to specify the new compiler but the environment variable is easier:
您还可以在 makefile 中指定新的 how-to-compile-C++ 文件规则来指定新的编译器,但环境变量更容易:
.cc.o:
/home/.../g++ $(CPPFLAGS) $(CXXFLAGS) -c
回答by Some programmer dude
The problem is because of these three rules:
问题在于这三个规则:
Socket: Socket.cpp
ServerSocket: ServerSocket.cpp
simple_server_main: simple_server_main.cpp
First of all, the left-hand side of the rule should be the object file I guess, so should have the .o
suffix.
首先,规则的左侧应该是我猜的目标文件,所以应该有.o
后缀。
The second problem, and most likely the root of your problem, is that there is no command to compile the source files, which means that make
will use the default compiler and not your cross-compiler.
第二个问题,也很可能是您问题的根源,是没有编译源文件的命令,这意味着make
将使用默认编译器而不是您的交叉编译器。