在make上运行this code,以便将GBA与DevkitARM交叉编译,结果如下:
main.c
tilemap.c
linking cartridge
/opt/devkitpro/devkitARM/bin/../lib/gcc/arm-none-eabi/11.1.0/../../../../arm-none-eabi/bin/ld: tilemap.o:/home/shuffles/repos/_gba_dev/catsvsrats/include/raw/images/gui/map/tiles25.h:14: multiple definition of `TILES25_IMAGE'; main.o:/home/shuffles/repos/_gba_dev/catsvsrats/include/raw/images/gui/map/tiles25.h:14: first defined here
/opt/devkitpro/devkitARM/bin/../lib/gcc/arm-none-eabi/11.1.0/../../../../arm-none-eabi/bin/ld: tilemap.o:/home/shuffles/repos/_gba_dev/catsvsrats/include/raw/tilemap_data.h:11: multiple definition of `TILEMAP'; main.o:/home/shuffles/repos/_gba_dev/catsvsrats/include/raw/tilemap_data.h:11: first defined here
/opt/devkitpro/devkitARM/bin/../lib/gcc/arm-none-eabi/11.1.0/../../../../arm-none-eabi/bin/ld: tilemap.o:/home/shuffles/repos/_gba_dev/catsvsrats/include/raw/images/gui/map/tiles25.h:337: multiple definition of `TILES25_PALETTE'; main.o:/home/shuffles/repos/_gba_dev/catsvsrats/include/raw/images/gui/map/tiles25.h:337: first defined here
collect2: error: ld returned 1 exit status
make[1]: *** [/opt/devkitpro/devkitARM/gba_rules:25: /home/shuffles/repos/_gba_dev/catsvsrats/catsvsrats.elf] Error 1
make: *** [Makefile:121: build] Error 2尽管链接器抱怨定义重复,但我没有在代码中找到任何定义,而且所有的头文件都有#include保护。任何信息都有帮助。
发布于 2021-12-08 07:36:28
显然,您将标题包含在多个源中。标题保护只会防止同一翻译单元中的多个定义/声明。每个生成的对象文件都将包含上述变量,因此链接器通过给出错误信息是正确的。
请不要在头文件中定义变量。在相关源文件中定义它们,在您的示例中是"tiles25.c“。将该源添加到要编译和链接的源列表中。
头文件应该声明变量,并为此使用关键字extern。
/* tiles25.h */
/* ... */
extern const uint16_t TILES25_PALETTE[];
/* ... *//* tiles25.c */
/* ... */
const uint16_t TILES25_PALETTE[] = {
};
/* ... */https://stackoverflow.com/questions/70269490
复制相似问题