制作程序时出现奇怪的链接器错误:'multiple definition of `fnames'; src/main.o:(.data.rel.local+0x0): 首先在这里定义'

odd linker error occuring when making program: 'multiple definition of `fnames'; src/main.o:(.data.rel.local+0x0): first defined here'

因此,我正在编写一个程序,该程序的增长如此之大,以至于我决定为该项目创建许多文件(就像我以前所做的那样)。创建 2 个文件并将适当的函数放入其中后,这些是我在制作时遇到的错误。

/bin/ld: src/util.o:(.data.rel.local+0x0): multiple definition of `fnames'; src/main.o:(.data.rel.local+0x0): first defined here
/bin/ld: src/util.o:(.data+0x0): multiple definition of `gBoard'; src/main.o:(.data+0x0): first defined here
/bin/ld: src/possiblemoves.o:(.data.rel.local+0x0): multiple definition of `fnames'; src/main.o:(.data.rel.local+0x0): first defined here
/bin/ld: src/possiblemoves.o:(.data+0x0): multiple definition of `gBoard'; src/main.o:(.data+0x0): first defined here
collect2: error: ld returned 1 exit status
make: *** [Makefile:22: vgcp] Error 1

这是我的 Makefile:

IDIR=/usr/include/SDL2
CC=gcc
CFLAGS=-I$(IDIR)

ODIR=src

LIBS=-lSDL2 -lSDL2_image

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

_OBJ = main.o util.o possiblemoves.o
OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))

all: vgcp clean


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

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

.PHONY: clean

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

您似乎在所有 util.cpossiblemoves.cmain.c 文件中定义了变量 fnamesgBoard,就像链接器一样尝试建议。

定义必须仅保留在一个源文件中,而如果需要从其他文件中访问它们,则可以在其他文件中放置此类变量的声明。

我只是通过浏览堆栈溢出来解决这个问题... 所以看起来我需要在头文件中为我的变量使用 extern 关键字。我从这里得到了我的解决方案:(.bss+0x0): multiple definition of Proxies