c - 制作程序时出现奇怪的链接器错误:'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
解决方案
正如链接器试图建议的那样,您似乎在所有,和文件中定义了变量fnames
和变量。gBoard
util.c
possiblemoves.c
main.c
定义必须仅保留在一个源文件中,而如果您需要从那里访问这些变量,则可以在其他文件中声明此类变量。
推荐阅读
- json - Bash:如何将数组值添加到 JSON?
- angular - 角度形式“确认密码”显示错误
- regex - 如何使用带有 end_with 的正则表达式?Ruby中的方法?
- r - 如何在 bnlearn 4.6 版中使用自定义评分函数?
- c - 4 个字符快速完成,但添加第 5 个循环会减慢整个程序
- javascript - es6中使用map过滤并返回一个新对象
- r - 如何将字符串列表传递给用户定义的函数?
- python-3.x - 可视化在 SpaCy 中使用预训练词嵌入计算的余弦相似度分数
- apache - 突然无法从外部访问 HTTP(AWS EC2 Linux)
- c++ - 制作一个将用户输入与“数据库”相匹配的程序。不断收到此错误