【问题标题】:Makefile compliation issue cannot find *.o no such file or directoryMakefile 编译问题找不到 *.o 没有这样的文件或目录
【发布时间】:2021-12-31 14:53:21
【问题描述】:

我正在尝试使用不同子文件夹中的文件制作一个简单的 Makefile。这是我的 Makefile

CFLAGS = -g -Wall
IFLAGS = -Iinclude
OPATH = obj/
CPATH = src/

vpath %.h include
vpath %.c src
vpath %.o obj
vpath main bin



main: main.o grille.o io.o jeu.o
    gcc $(CFLAGS) -o main $(OPATH)main.o $(OPATH)grille.o $(OPATH)io.o $(OPATH)jeu.o 
    mv $@ bin/
main.o: main.c grille.h io.h jeu.h
    


grille.o : grille.c grille.h
    

io.o: io.c io.h
    

jeu.o: jeu.c jeu.h
    

%.o : 
    gcc $(CFLAGS) -c $< $(IFLAGS)
    mv $@ $(OPATH)

clean:
    rm obj/* bin/*

我将这些文件放在名为 src、obj 和 include 的子文件夹中。 我收到此错误

C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find obj/main.o: No such file or directory
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find obj/grille.o: No such file or directory
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find obj/io.o: No such file or directory
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find obj/jeu.o: No such file or directory
collect2.exe: error: ld returned 1 exit status
make: *** [makefile:15: main] Error 1

ERROR

任何想法如何解决这个问题?

【问题讨论】:

  • 如果你写了一个名为foo.o的目标,make期望你构建一个名为foo.o的文件。如果您不构建该文件,而是构建一些其他文件,例如 obj/foo.o,则 make 无法正常工作。如果您想构建obj/foo.o,那么您应该使用obj/foo.o 作为您的目标名称。然后它将起作用。对于编译器,如果您添加选项 -o $@ 它将始终正确(因为 make 将变量 $@ 设置为它希望您的配方构建的文件的名称。
  • @MadScientist 我对 makefile 很陌生,所以如果有的话,我很抱歉;我应该做一些直接的事情,但我确实添加了 obj/begore my grille.o io.o和jeu.o,但这似乎并不能解决问题。

标签: c makefile compiler-errors compilation


【解决方案1】:

将输出文件写入与输入文件不同的目录需要额外的设置。您应该查看 make 运行的编译行。它是否将目标文件放在正确的位置?如果不是,那么这就解释了为什么会出现这些链接错误:目标文件不存在于您告诉链接器它们将存在的位置,因此它会失败。

这条规则不对:

%.o : 
        gcc $(CFLAGS) -c $< $(IFLAGS)
        mv $@ $(OPATH)

此外,您不能使用 vpath 搜索由 makefile 构建的目标。那是行不通的。 vpath 只能用于搜索源文件(如 .c.h 文件)。所以这些行没有做任何事情,应该被删除:

vpath %.o obj
vpath main bin

而且这些规则是不对的:你需要把对象目录放在这里:

grille.o : grille.c grille.h
io.o: io.c io.h
jeu.o: jeu.c jeu.h

你想要这样的东西:

CC = gcc
CFLAGS = -g -Wall
IFLAGS = -Iinclude
OPATH = obj/

vpath %.h include
vpath %.c src

bin/main: obj/main.o obj/grille.o obj/io.o obj/jeu.o
    $(CC) $(CFLAGS) -o $@ $^

main.o: main.c grille.h io.h jeu.h
grille.o : grille.c grille.h
io.o: io.c io.h
jeu.o: jeu.c jeu.h

$(OPATH)%.o : %.c
        $(CC) $(CFLAGS) $(IFLAGS) -c -o $@ $<

【讨论】:

    猜你喜欢
    • 2017-03-07
    • 1970-01-01
    • 2021-09-07
    • 2016-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多