我正在尝试使用 sed 更改 Makefile 变量。我写了一个小的 Makefile 来说明我想要做什么。CINCS 变量最终将附加到 CFLAGS 变量。CSINCS 变量应该保存所有包含文件的路径,前面没有“-I”。
#SHELL = /bin/sh
SRCS = /usr/local/src/jpeg/jpeg-9b
CINCS = -I/usr/local/src/jpeg/jpeg-9b
CSINCS = $(CINCS) | sed -e "s/-I//g"
check:
@echo 1. $(SRCS)
find $(SRCS) -name "*.c" -print > cscope.files
@echo 2. $(CSINCS)
find '$(CSINCS) -name" "*.h' -print >> cscope.files
cscope -k -b
cat cscope.files | xargs ctags -u
Run Code Online (Sandbox Code Playgroud)
#
我试图删除所有包含路径前面的“-I”。执行时:
$ make -f test check
1. /usr/local/src/jpeg/jpeg-9b
find /usr/local/src/jpeg/jpeg-9b -name "*.c" -print > cscope.files
2. /usr/local/src/jpeg/jpeg-9b
find '-I/usr/local/src/jpeg/jpeg-9b | sed -e "s/-I//g" -name" "*.h' -print >> cscope.files
find: unknown predicate `-I/usr/local/src/jpeg/jpeg-9b | sed -e "s/-I//g" -name" "*.h'
test:8: recipe for target 'check' failed
make: *** [check] Error 1
Run Code Online (Sandbox Code Playgroud)
在位置“2”,CSINCS 变量看起来是正确的。但是“find 命令有一个扩展。这就是问题所在。
我知道我可以在 cscope 命令中使用 CINCS 变量:
cscope -I $(CINCS)
Run Code Online (Sandbox Code Playgroud)
但我也想将 cscope.files 用于 ctags 文件。我可以生成一个单独的 CSINCS 变量并始终保持 CINCS 和 CSINCS 同步。只是好奇是怎么回事。
您还没有告诉 makeCSINCS
作为 shell 脚本执行的值,您需要类似的东西
CSINCS := $(shell echo $(CINCS) | sed -e "s/-I//g")
Run Code Online (Sandbox Code Playgroud)
或者,如果您最近制作了 4.0 或更高版本
CSINCS != echo $(CINCS) | sed -e "s/-I//g"
Run Code Online (Sandbox Code Playgroud)
尽管对于这么简单的事情,您不需要使用 sed 或 shell
CSINCS := $(subst -I,,$(CINCS))
Run Code Online (Sandbox Code Playgroud)