我的 makefile 给出了以下错误信息:
ma.c:(.text+0x5aa): multiple definition of `dividetoken'
obj/src/ag.o:ag.c:(.text+0x0): first defined here
obj/src/ma.o: In function `main':
ma.c:(.text+0x629): multiple definition of `main'
obj/src/ag.o:ag.c:(.text+0x6ef): first defined here
obj/src/cv.o: In function `readln':
cv.c:(.text+0xb2): multiple definition of `readln'
obj/src/ag.o:ag.c:(.text+0x7f): first defined here
obj/src/cv.o: In function `dividetoken':
cv.c:(.text+0x176): multiple definition of `dividetoken'
obj/src/ag.o:ag.c:(.text+0x0): first defined here
obj/src/cv.o: In function `main':
cv.c:(.text+0x1f5): multiple definition of `main'
obj/src/ag.o:ag.c:(.text+0x6ef): first defined here
obj/src/sv.o: In function `readln':
sv.c:(.text+0x0): multiple definition of `readln'
obj/src/ag.o:ag.c:(.text+0x7f): first defined here
obj/src/sv.o: In function `dividetoken':
sv.c:(.text+0x150): multiple definition of `dividetoken'
obj/src/ag.o:ag.c:(.text+0x0): first defined here
obj/src/sv.o: In function `main':
sv.c:(.text+0xcb5): multiple definition of `main'
obj/src/ag.o:ag.c:(.text+0x6ef): first defined here
collect2: error: ld returned 1 exit status
makefile:17: recipe for target 'program' failed
make: *** [program] Error 1
我该怎么办?能轻易解决吗?我不知道该怎么办
我的 makefile 看起来像这样:
ODIR = obj
IDIR = include
SDIR = src
CC = gcc
EXE = program
DEPS = $(IDIR)/$(wildcard*.h)
SOURCES = $(wildcard $(SDIR)/*.c)
OBJECTS = $(foreach o, $(patsubst %.c,%.o,$(SOURCES)), $(ODIR)/$o)
$(ODIR)/%.o : %.c $(DEPS)
$(CC) -c -o $@ $<
$(EXE): $(OBJECTS)
$(CC) $(OBJECTS) -o $(EXE)
clean:
rm $(ODIR)/$(SDIR)/*
rm $(EXE)
我希望这可以帮助您了解我的错误所在。
最佳答案
你的 Makefile 看起来不错——我看不出有任何问题[*]。问题出在您正在编译的代码上。
查看您遇到的其中一个错误:
obj/src/sv.o: In function `main':
sv.c:(.text+0xcb5): multiple definition of `main'
obj/src/ag.o:ag.c:(.text+0x6ef): first defined here
也就是说,您有一个名为“sv.c”的文件,其中包含一个名为main
的函数。并且您有另一个名为“ag.c”的文件,其中也有一个名为 main
的函数。您正试图将这两个文件编译成同一个程序,但您只能使用一个名为 main
的函数。
[*] 据推测,您的“src”目录中似乎有多个程序,而您的 Makefile 正试图将所有内容构建到一个巨大的程序中。如果是这种情况,您需要将代码分离到它们自己的目录中或更改您的 Makefile,以便 SOURCES
仅列出与您正在编译的程序相关的源文件。
如果你想在同一个 Makefile 中编译多个程序,你需要将它们分开。每个程序都需要自己的构建规则来指定它需要的依赖项。您可以用相同的方式为它们中的每一个生成 OBJECTS
变量 - 但您需要列出每个程序需要的 C 文件。
SOURCES1 = ag.c other.c something.c
SOURCES2 = sv.c morecode.c something.c
OBJECTS1 = $(foreach o, $(patsubst %.c,%.o,$(SOURCES1)), $(ODIR)/$o)
OBJECTS2 = $(foreach o, $(patsubst %.c,%.o,$(SOURCES2)), $(ODIR)/$o)
$(ODIR)/%.o : %.c $(DEPS)
$(CC) -c -o $@ $<
program1: $(OBJECTS1)
$(CC) $^ -o $@
program2: $(OBJECTS2)
$(CC) $^ -o $@
供引用 $^
表示所有依赖。
关于c - makefile 上的 fatal error ,需要了解问题所在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56079827/