wildcard makefile
时间: 2024-08-17 10:02:44 浏览: 116
wildcard Makefile是一种特殊的Makefile编写技术,它利用了Unix shell的通配符功能,允许Makefile动态地处理未指定目标名的情况。在标准Makefile中,如果想要包含一组文件,通常需要手动列出每个文件的名字。然而,当有一组文件需要频繁更新,且文件名模式固定时,比如所有`.cpp`源文件或`.h`头文件,使用wildcard规则可以使Makefile更简洁、易维护。
例如,一个简单的Makefile片段可以这样编写:
```makefile
SOURCES := $(wildcard *.cpp)
OBJECTS := $(patsubst %.cpp,%.o,$(SOURCES))
all: $(OBJECTS)
%.o: %.cpp
gcc -c $< -o $@
clean:
rm -f *.o
```
在这个例子中,`$(wildcard *.cpp)`会匹配当前目录下所有的`.cpp`文件,并将它们保存在`SOURCES`变量中。`$(patsubst %.cpp,%.o,$(SOURCES))`则会根据这个模式生成对应的编译目标(`%.o`)。这样,每当有新的`.cpp`文件加入,Makefile就能自动识别并处理它们。
相关问题
wildcard in makefile
In Makefiles, a wildcard is a character or sequence of characters that represents one or more other characters. It is often used to match file names or patterns.
The most common use of wildcards in Makefiles is with the "wildcard" function. The syntax of the "wildcard" function is as follows:
```
$(wildcard pattern)
```
This function expands to a space-separated list of all the file names that match the given pattern. For example, to get a list of all the C source files in the current directory, you could use:
```
C_FILES := $(wildcard *.c)
```
This would set the variable "C_FILES" to a space-separated list of all the files in the current directory that end with ".c".
Another way to use wildcards in Makefiles is with pattern rules. Pattern rules are used to define a rule for building a set of files that match a particular pattern. For example:
```
%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@
```
This rule tells Make to build any file with the ".o" extension that corresponds to a file with the ".c" extension. The "$<" and "$@" variables are used to represent the input and output files, respectively.
Overall, wildcards are a powerful feature of Makefiles that allow you to write more generic rules and automate your build process.
makefile wildcard
makefile中的wildcard函数是一个用于查找文件的函数。它可以在makefile中使用通配符来查找指定目录下的文件,并将结果返回给makefile。例如,$(wildcard *.c)将查找当前目录下所有以.c结尾的文件,并将它们返回给makefile。这个函数在makefile中非常有用,可以帮助我们自动化构建过程中的文件查找和处理。
阅读全文