makefile 把字符串转换成单词
时间: 2024-11-18 12:06:49 浏览: 26
Makefile 是一种自动化构建工具,在 Unix 和 Linux 系统中广泛用于管理软件项目的编译、链接和部署过程。它通常包含一系列规则(recipes),其中有一部分规则可能涉及处理字符串操作,比如将连续的空格或特定字符分隔的字符串分割成单独的单词。
如果你想要在 Makefile 中把一个字符串转换成单词,你可以使用 `$(subst)` 或者 `$(filter-out)` 预处理器函数。例如:
```makefile
STRING := This is a test string
WORDS := $(subst ' ', '', $(STRING)) # 使用空格作为分隔符
# 或者,如果你想移除所有非字母字符并将大写字母转换为小写
LOWER_WORDS := $(shell tr '[:upper:]' '[:lower:]' < <(echo $(STRING)))
# 输出单词列表
%.words: ; @echo $(WORDS)
%.lower.words: ; @echo $(LOWER_WORDS)
.PHONY: words lower.words
```
在这个例子中,`SUBST` 函数会替换掉字符串中的空格,而 `shell` 指令结合 `tr` 命令则可以去除非字母字符并转为小写。然后通过`.PHONY` 规则定义了一些目标(如 `.words` 和 `.lower.words`),当你运行 `make <target>` 时,它会打印出相应的单词列表。
相关问题
makefile 3.82 字符拼接
### Makefile 中的字符串连接
在 Makefile 版本 3.82 及以上版本中,可以利用多种方法来进行字符串连接。最常用的方式是通过简单的赋值操作以及使用特定的函数。
#### 使用简单赋值进行字符串连接
可以直接将多个单词或变量相加来创建新的字符串:
```makefile
VAR1 = Hello
VAR2 = World
RESULT = $(VAR1)$(VAR2)
```
这种方式适用于基本的字符串拼接需求[^1]。
#### 利用 `patsubst` 函数
对于更复杂的模式替换场景,可采用 `patsubst` 函数完成带有通配符的字符串转换与连接工作:
```makefile
SRCS := foo.c bar.c baz.s ugh.h
OBJS := $(patsubst %.c,%.o,$(filter %.c,$(SRCS))) \
$(patsubst %.s,%.o,$(filter %.s,$(SRCS)))
```
此例子展示了如何基于文件扩展名构建目标对象列表,并将其串联起来形成最终的目标集合[^3]。
#### 应用 shell 命令辅助处理
当遇到较为复杂的情况时,还可以借助于 Shell 命令的帮助,在 Makefile 内部执行命令并将结果返回作为新字符串的一部分:
```makefile
define get_version =
@echo "Version: $$(git describe --tags)"
endef
export VERSION=$(shell git describe --tags)
all:
@echo "Building with version $(VERSION)"
```
上述代码片段说明了怎样获取 Git 标签信息并与其它文本组合成完整的版本号输出。
makefile函数
Makefile 中的函数允许您在规则中执行一些简单的文本操作和转换。以下是一些常见的 Makefile 函数:
1. `$(subst from,to,text)`: 在 `text` 中将所有的 `from` 替换为 `to`。
2. `$(patsubst pattern,replacement,text)`: 将 `text` 中符合 `pattern` 的部分替换为 `replacement`。
3. `$(wildcard pattern)`: 扩展为匹配 `pattern` 的所有文件名。
4. `$(addprefix prefix,names)`: 在 `names` 中的每个单词前添加 `prefix`。
5. `$(addsuffix suffix,names)`: 在 `names` 中的每个单词后添加 `suffix`。
除了上述函数之外,Makefile 还提供了其他的函数,如字符串处理函数(`$(strip)`, `$(shell)`, `$(sort)` 等)、文件名函数(`$(dir)`, `$(notdir)`, `$(basename)` 等)和条件函数(`$(if)`, `$(foreach)` 等)等。您可以根据自己的需求在 Makefile 中使用这些函数来实现更复杂的操作。
阅读全文
相关推荐
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)