首页 > 解决方案 > 制作程序时出现奇怪的链接器错误:'fnames'的多个定义;src/main.o:(.data.rel.local+0x0): 首先定义在这里'

问题描述

所以我正在编写一个增长如此之多的程序,以至于我决定为这个项目创建许多文件(就像我以前做过的那样)。创建 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

标签: cgccmakefilelinkergnu-make

解决方案


正如链接器试图建议的那样,您似乎在所有,和文件中定义了变量fnames和变量。gBoardutil.cpossiblemoves.cmain.c

定义必须仅保留在一个源文件中,而如果您需要从那里访问这些变量,则可以在其他文件中声明此类变量。


推荐阅读