简体   繁体   English

Makefile 编译问题找不到 *.o 没有这样的文件或目录

[英]Makefile compliation issue cannot find *.o no such file or directory

I'm trying to make a simple Makefile with files in different subfolders.我正在尝试使用不同子文件夹中的文件制作一个简单的 Makefile。 This is my 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/*

I have the files in subfolders called src, obj, and include.我将文件放在名为 src、obj 和 include 的子文件夹中。 I get this error我收到这个错误

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错误

Any Ideas how to fix this?任何想法如何解决这一问题?

Writing output files to a different directory than the input files requires extra setup.将 output 文件写入与输入文件不同的目录需要额外的设置。 You should look at your compile line that make runs.您应该查看 make 运行的编译行。 Does it put the object file in the right place?它是否将 object 文件放在正确的位置? If not then that explains why you get those link errors: the object files don't exist where you told the linker they will be, so it fails.如果不是,那么这就解释了为什么会出现这些链接错误:object 文件在您告诉 linker 的地方不存在,因此它失败了。

This rule is not right:这个规则是不对的:

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

Also, you cannot use vpath to search for targets that are built by makefiles.此外,您不能使用 vpath 搜索由 makefile 构建的目标。 That can't work.那是行不通的。 vpath can ONLY be used to search for source files (like .c or .h files). vpath 只能用于搜索源文件(如.c.h文件)。 So these lines don't do anything and should be removed:所以这些行没有做任何事情,应该被删除:

vpath %.o obj
vpath main bin

And these rules are not right: you need to put the object directory here:而且这些规则是不对的:需要把object目录放在这里:

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

You want something like this:你想要这样的东西:

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 $@ $<

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM