c - makefile 上的 fatal error ,需要了解问题所在

标签 c makefile

我的 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/

相关文章:

c - start.exe : 0xC0000005: Access violation writing location 0x00000000 中 0x61e1f5cf (msvcr90d.dll) 的未处理异常

linux - 在 debian lenny 上安装 rpcgen

c++ - 编译/链接 C 和 C++ 文件时遇到问题

c - 如何使用 opengl 函数制作用于在 c 中绘制 3D 点的 3D 窗口?

c++ - 从c/cpp程序中的进程ID获取进程名称(我不能使用/proc/<pid>/cmdline)

c - 如何修补 libxml2,以便在使用前缀时使用 ICU 支持进行编译?

c++ - CMake 没有创建可以访问我的共享库的可执行文件

c++ - 未定义对 `vtable for implementation' 错误的引用

c - FIFO channel 读取不正确

c - 密码强度程序不起作用