为什么 DevkitARM GBA 链接器会检测到多个定义?

Why does DevkitARM GBA linker detect multiple definitions?

运行 make on this code 使用 DevkitARM 为 GBA 交叉编译结果如下:

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 保护。任何信息都有帮助。

显然您在多个来源中包含了 header。 header 守卫只是防止 同一个翻译单元 中的多个 definitions/declarations。每个生成的 object 文件都将包含提到的变量,因此 linker 给你的错误是正确的。

不要在header 文件中定义 变量。在关联的源文件中定义它们,在您的例子中是“tiles25.c”。将该源添加到要编译的源列表中,然后 link.

header文件应该声明变量,为此使用关键字extern

/* tiles25.h */
/* ... */
extern const uint16_t TILES25_PALETTE[];
/* ... */
/* tiles25.c */
/* ... */
const uint16_t TILES25_PALETTE[] = {
};
/* ... */