makefile - Makefile 中的多个 Shell 命令

标签 makefile

我正在尝试从文本文件中获取我的编译版本。我正在使用这个命令

grep -w -m 1 "V1" server.h | sed $(VERSION) 's/#define V1[\t]//'

它工作正常,但现在我尝试使用 shell 从我的 Makefile 执行它:

VERSION=$(shell grep -w -m 1 "V1" server.h | sed $(VERSION) 's/#define V1[\t]//')

但由于|,我无法使其工作。如果我只输入一个命令,例如 grep ,它运行良好,还有另一种方法来指示 | 来连接表达式吗?或者我还能怎么做?

编辑:

您好,感谢您的回答,

阅读你的答案,我意识到复制/粘贴背叛了我,jeje,这是我使用的正确表达方式:

版本:=$(shell grep -w -m 1 "V2"server.h | sed 's/#define V2[\t]//')

这是 Makefile 的输出:

  • 对函数 shell 的未终止调用':缺少)'。停下来。

我已经测试了您的建议,但它也因相同的错误而失败。

谢谢。

最佳答案

与管道无关。它不起作用的原因是您已将其定义为动态宏。通过使用“=”,它将重新评估每个引用上的宏。通过尝试重载 VERSION 变量作为版本和文件名,您实际上已经创建了一个递归宏。尝试使用不同的变量名称并将其设为静态:

VERSION_NUMBER:=$(shell grep -w -m 1 "V1" server.h | sed $(VERSION) 's/#define V1[\t]//')

$(error VERSION_NUMBER=$(VERSION_NUMBER))

一旦你让它工作,就删除$(error)。另外,无论如何,awk 在这种情况下可能会更有效:

VERSION_NUMBER:=$(shell awk '/\<V1\>/ { print gensub(/^\#define V1[[:space:]]+/, "", ""); exit }' $(VERSION))

你还有哈希的问题(美国佬们请注意)#。它正在终止作为注释的表达式。试试这个:

VERSION:=$(shell grep -w -m 1 "V2" server.h | sed 's/\#define V2[\t]//')

或者这个:

VERSION:=$(shell awk '/\<V1\>/ { print gensub(/^\#define V1[[:space:]]+/, "", ""); exit }' server.h)

make 中有很多字符时你会遇到这个问题。美元是最难逃脱的。我见过这样的表达:

V:=$(foreach f,$(list),$(eval $(shell create_rule $$$$(f))))

有时最好编写一个 shell 脚本并调用它:

script.sh:

#!/bin/sh

awk '/\<V1\>/ { print gensub(/^#define V1[[:space:]]+/, "", ""); exit }' "$@"

Makefile:

VERSION=$(shell script.sh server.h)

关于makefile - Makefile 中的多个 Shell 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18399273/

相关文章:

c++ - Makefile:Foreach 和 eval 缺少分隔符?

firefox - 构建 GNU make 的 Cygwin 版本

makefile - cmake和并行构建与 "make -jN"

linux - Shell - 抑制单个命令的输出

javascript - Backbone 应用程序的网络服务器?

c++ - 尝试在 C++ 中制作 makefile 时出错

c++ - 生成文件错误 : undefined reference to main

java - Java 中的 Makefile 创建

c++ - 构建 main.c 和 main.cpp 的单个 Makefile

c++ - 我的文档的空白自由路径