wildcard 后边必须是文件吗? 文件夹是否也可以?、
时间: 2024-03-27 18:37:44 浏览: 132
`wildcard` 函数后面不仅可以是文件名,还可以是文件夹路径,可以使用通配符匹配文件夹中的所有文件。下面是一个使用通配符匹配文件夹中所有文件的示例:
```
# 查找当前目录下所有以 .c 和 .h 结尾的文件
SRCS := $(wildcard *.c) $(wildcard *.h)
# 查找当前目录下的所有子目录中的 .c 文件
SRCS += $(wildcard */*.c)
# 生成可执行文件
myprog: $(SRCS)
gcc -o $@ $^
# 清除生成的.o文件和可执行文件
clean:
rm -f $(OBJS) myprog
```
上面的示例中,首先使用 `wildcard` 函数查找当前目录下所有以 `.c` 和 `.h` 结尾的文件,并将它们赋值给变量 `SRCS`。然后,使用 `wildcard */*.c` 语句查找当前目录下的所有子目录中的 `.c` 文件,并添加到 `SRCS` 列表中。最后,将 `SRCS` 列表作为依赖关系生成可执行文件。
注意,使用通配符匹配文件夹中的文件时,需要注意文件名中是否包含空格等特殊字符,以免出现错误。
相关问题
is the usage of wildcard and firstword in makefile?
In a Makefile, a wildcard is a special character that matches any number of characters in a filename. The `wildcard` function can be used to find all files that match a particular pattern. For example, the following line will find all files in the current directory that end with ".c":
```
SOURCES := $(wildcard *.c)
```
The `firstword` function, on the other hand, is used to extract the first word from a string. This is often used in Makefiles to extract the first word from a list of space-separated values. For example, the following line extracts the first word from the `SOURCES` variable defined above:
```
FIRST_SOURCE := $(firstword $(SOURCES))
```
This would set the `FIRST_SOURCE` variable to the name of the first C file in the current directory.
makefile文件wildcard
在 Makefile 文件中,`wildcard` 是一个函数,用于匹配文件名模式。它可以用来获取指定目录下符合特定模式的文件列表。以下是 `wildcard` 函数的使用示例:
```makefile
# 获取当前目录下所有的.c文件
C_FILES := $(wildcard *.c)
# 获取src目录下所有的.c和.cpp文件
SRC_FILES := $(wildcard src/*.c src/*.cpp)
# 打印文件列表
print_files:
@echo "C Files: $(C_FILES)"
@echo "Source Files: $(SRC_FILES)"
```
在上述示例中,`wildcard` 函数会返回符合特定模式的文件列表,并将其赋值给变量 `C_FILES` 和 `SRC_FILES`。然后,在 `print_files` 目标中,我们可以使用这些变量来打印文件列表。
假设当前目录下有三个.c文件,分别为 `main.c`、`utils.c` 和 `config.c`,那么在运行 `make print_files` 命令后,将会输出以下内容:
```
C Files: main.c utils.c config.c
Source Files: src/file1.c src/file2.cpp
```
希望这个示例对你有帮助!如果你有更多关于 Makefile 的问题,请随时提问。
阅读全文
相关推荐
















