通用 ELF 中的重定位 (EM: 40)

18

我正在尝试从Ubuntu交叉编译到Friendly-arm,但是遇到了奇怪的错误:

root@kevin-VirtualBox:/home/kevin/Desktop/makef# make ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi-l
gcc -o main obj/main.o obj/serial.o obj/fb.o obj/menu_main.o obj/timer.o obj/cmdin.o obj/buzzer.o obj/statemachine.o obj/inout.o obj/network.o obj/text_file_input.o obj/text_file_input_oven.o -I./ -lgd -lrt
/usr/bin/ld: obj/main.o: Relocations in generic ELF (EM: 40)
obj/main.o: could not read symbols: File in wrong format
collect2: ld returned 1 exit status
make: *** [main] Error 1

命令行: make ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi-l

Makefile:(已更新)

export ARCH:=arm
export CROSS_COMPILE:=arm-none-linux-gnueabi

IDIR =./
CC=arm-linux-gnueabi-gcc
CFLAGS=-I$(IDIR)

LD= //opt/FriendlyARM/toolschain/4.5.1/arm-none-linux-gnueabi/bin/

ODIR=obj
LDIR =./

LIBS=-lgd -lrt

_DEPS = main.h Makefile
DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS))

_OBJ = main.o serial.o fb.o menu_main.o timer.o cmdin.o buzzer.o statemachine.o inout.o network.o text_file_input.o text_file_input_oven.o
OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))


$(ODIR)/%.o: %.c $(DEPS)
    $(CC) -c -o $@ $< $(CFLAGS)

main: $(OBJ)
    $(CC) -o $@ $^ $(CFLAGS) $(LIBS)

.PHONY: clean

clean:
    rm -f $(ODIR)/*.o *~ core $(INCDIR)/*~ 

更新 Makefile 后出现新的错误:

root@kevin-VirtualBox:/home/kevin/Desktop/makef# make ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi -l
gcc -o main obj/main.o obj/serial.o obj/fb.o obj/menu_main.o obj/timer.o obj/cmdin.o obj/buzzer.o obj/statemachine.o obj/inout.o obj/network.o obj/text_file_input.o obj/text_file_input_oven.o -I./ -lgd -lrt
/usr/bin/ld: obj/main.o: Relocations in generic ELF (EM: 40)
obj/main.o: could not read symbols: File in wrong format
collect2: ld returned 1 exit status
make: [main] Error 1 (ignored)
arm-linux-gnueabi-gcc -o main obj/main.o obj/serial.o obj/fb.o obj/menu_main.o obj/timer.o obj/cmdin.o obj/buzzer.o obj/statemachine.o obj/inout.o obj/network.o obj/text_file_input.o obj/text_file_input_oven.o -I./ -lgd -lrt
/usr/lib/gcc/arm-linux-gnueabi/4.6/../../../../arm-linux-gnueabi/bin/ld: warning: libz.so.1, needed by /usr/lib/gcc/arm-linux-gnueabi/4.6/../../../../arm-linux-gnueabi/lib/../lib/libgd.so, not found (try using -rpath or -rpath-link)
/usr/lib/gcc/arm-linux-gnueabi/4.6/../../../../arm-linux-gnueabi/bin/ld: warning: libexpat.so.1, needed by /usr/arm-linux-gnueabi/lib/libfontconfig.so.1, not found (try using -rpath or -rpath-link)
/usr/arm-linux-gnueabi/lib/libfontconfig.so.1: undefined reference to `XML_GetBuffer'
/usr/arm-linux-gnueabi/lib/libfontconfig.so.1: undefined reference to `XML_GetErrorCode'
/usr/lib/gcc/arm-linux-gnueabi/4.6/../../../../arm-linux-gnueabi/lib/../lib/libgd.so: undefined reference to `compress'
/usr/arm-linux-gnueabi/lib/libfreetype.so.6: undefined reference to `inflate'
/usr/arm-linux-gnueabi/lib/libfontconfig.so.1: undefined reference to `XML_SetDoctypeDeclHandler'
/usr/arm-linux-gnueabi/lib/libfontconfig.so.1: undefined reference to `XML_ParserCreate'
/usr/arm-linux-gnueabi/lib/libpng12.so.0: undefined reference to `crc32'
/usr/arm-linux-gnueabi/lib/libfontconfig.so.1: undefined reference to `XML_GetCurrentLineNumber'
/usr/lib/gcc/arm-linux-gnueabi/4.6/../../../../arm-linux-gnueabi/lib/../lib/libgd.so: undefined reference to `uncompress'
/usr/arm-linux-gnueabi/lib/libpng12.so.0: undefined reference to `deflateReset'
/usr/arm-linux-gnueabi/lib/libpng12.so.0: undefined reference to `deflate'
/usr/arm-linux-gnueabi/lib/libfontconfig.so.1: undefined reference to `XML_SetCharacterDataHandler'
/usr/arm-linux-gnueabi/lib/libfontconfig.so.1: undefined reference to `XML_ParserFree'
/usr/arm-linux-gnueabi/lib/libfontconfig.so.1: undefined reference to `XML_SetUserData'
/usr/arm-linux-gnueabi/lib/libpng12.so.0: undefined reference to `deflateInit2_'
/usr/arm-linux-gnueabi/lib/libfontconfig.so.1: undefined reference to `XML_ParseBuffer'
/usr/arm-linux-gnueabi/lib/libfreetype.so.6: undefined reference to `inflateEnd'
/usr/arm-linux-gnueabi/lib/libpng12.so.0: undefined reference to `deflateEnd'
/usr/arm-linux-gnueabi/lib/libpng12.so.0: undefined reference to `inflateInit_'
/usr/arm-linux-gnueabi/lib/libfontconfig.so.1: undefined reference to `XML_ErrorString'
/usr/arm-linux-gnueabi/lib/libfontconfig.so.1: undefined reference to `XML_SetElementHandler'
/usr/arm-linux-gnueabi/lib/libfreetype.so.6: undefined reference to `inflateInit2_'
/usr/arm-linux-gnueabi/lib/libfreetype.so.6: undefined reference to `inflateReset'
collect2: ld returned 1 exit status
make: *** [main] Error 1
root@kevin-VirtualBox:/home/kevin/Desktop/makef# 

经过你的评论,Makefile已经可以正常工作(谢谢),并生成了一个主文件。但是我收到的唯一错误是:警告:格式不是字符串文字,也没有格式参数[-Wformat-security],但似乎不是关键问题。


你现在发布的输出(截至18:50)与你的Makefile不匹配。 - user149341
输出表明编译器被运行了两次,一次是作为 gcc(或可能是 cc),另一次是作为 arm-linux-gnueabi-gcc。根据您提供的 Makefile 版本,这是不可能的。 - user149341
3个回答

14

看起来你之前尝试使用不同的工具链(例如x86)进行编译,而那次构建生成的目标文件仍然存在。

尝试使用

 make clean

执行 make clean 命令将会返回“rm -f obj/*.o ~ core/~”,并且会移除所有的 .o 文件。当我运行 make 命令时,我得到了相同的错误。 - kevin ver
啊,是的,我注意到@duskwuff发现了另一个问题——这是同样的“错误类型”(混合工具),但是由不同的情况引起的。 - Mats Petersson
更新了问题,包括@duskwuff建议的结果。 - kevin ver
看起来你缺少一些库(某种压缩/解压库 - 不确定是libz还是其他什么 - 还有一些XML库)。 - Mats Petersson

11

按照现有的写法,这个Makefile在交叉编译时是有问题的——它没有一致地使用$(CC)变量,因此无法编译到非本机架构。

要修复它,请在main的部分中将cc更改为$(CC),即:

 main: $(OBJ)
-    gcc -o $@ $^ $(CFLAGS) $(LIBS)
+    $(CC) -o $@ $^ $(CFLAGS) $(LIBS)

更新 makefile 后出现了其他错误。 - kevin ver
“-”和“+”旨在作为补丁指示符号,而不是Makefile中的文字。涉及libz和libexpat的错误似乎表明您在交叉编译系统上未正确安装这些库。 - user149341
@kevinver,你现在发布的错误是链接器错误,看起来你正在使用各种你必须链接到的库,例如libz、libpng和libxml2,可能还有其他的。(而且你可能需要先进行交叉编译) - nos
我遇到了类似的问题,但是通过这里得到了解决方案。我使用$(CXX)编译.o文件为.a文件,这是用于交叉编译的,但是编译主程序的.o文件时使用了gcc。难怪库无法链接到主程序中。它们具有不同的目标环境设置。我在主程序的编译命令和链接命令中将gcc更改为$(CXX),这样就可以正常工作了。 - Stan Huang at Taiwan

0
我遇到了类似的问题,清理并没有帮助。最后我删除了构建文件夹并重新构建,这样就解决了。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接