gpt4 book ai didi

使用 vpath 时 Makefile 找不到文件

转载 作者:行者123 更新时间:2023-12-02 19:01:33 26 4
gpt4 key购买 nike

当我使用 make 时,它​​说

gcc -I./header -c -g main.c
gcc: error: main.c: No such file or directory

我的 makefile 在这里:

vpath %.c ./src
vpath %.h ./header

CC = gcc
FLAG = -I./header -c -g
objects = main.o a.o b.o

app: ${objects}
${CC} -o app ${objects}

main.o: main.c command.h
${CC} ${FLAG} main.c

a.o: a.c command.h
${CC} ${FLAG} a.c

b.o: b.c command.h
${CC} ${FLAG} b.c

clean:
-rm *.o app

.PHONY: clean

文件的存储方式如下:

.
|-- header
| `-- command.h
|-- Makefile
`-- src
|-- a.c
|-- b.c
`-- main.c

Makefile 有什么问题?

最佳答案

您应该使用automatic variables而不是食谱中文件名的硬编码值:

main.o: main.c command.h
${CC} ${FLAG} $<

...

这允许 Make 插入在 vpath 中找到的正确文件名:

When a prerequisite is found in another directory through directory search, this cannot change the recipe of the rule; they will execute as written. Therefore, you must write the recipe with care so that it will look for the prerequisite in the directory where make finds it.

This is done with the automatic variables such as $^ (see Automatic Variables). For instance, the value of $^ is a list of all the prerequisites of the rule, including the names of the directories in which they were found, and the value of $@ is the target.

关于使用 vpath 时 Makefile 找不到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9148645/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com