在 makefile 中使用 sed;如何转义变量?
我正在编写一个通用 makefile 来构建静态库。到目前为止,除了调用 sed 的行之外,它似乎运行良好:
# Generic makefile to build a static library
ARCH = linux
CFLAGS = -O3 -Wall
SOURCES = src
BUILD_DIR = build/$(ARCH)
TARGET = $(BUILD_DIR)/libz.a
CFILES = $(foreach dir,$(SOURCES),$(wildcard $(dir)/*.c))
OBJECTS = $(addprefix $(BUILD_DIR)/,$(CFILES:.c=.o))
# Pull in the dependencies if they exist
# http://scottmcpeak.com/autodepend/autodepend.html
-include $(OBJECTS:.o=.dep)
default: create-dirs $(TARGET)
$(TARGET): $(OBJECTS)
$(AR) -rc $(TARGET) $^
$(BUILD_DIR)/%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@
$(CC) -M $(CFLAGS) $*.c > $(BUILD_DIR)/$*.tmp
sed s/.*:/$(BUILD_DIR)\\/$*.o:/ $(BUILD_DIR)/$*.tmp > $(BUILD_DIR)/$*.dep
@rm $(BUILD_DIR)/$*.tmp
.PHONY: create-dirs
create-dirs:
@for p in $(SOURCES); do mkdir -p $(BUILD_DIR)/$$p; done
.PHONY: clean
clean:
rm -fr $(BUILD_DIR)
sed 用于将对象文件的路径/名称替换为对象实际所在位置的完整路径。例如,在此示例中,'src/foo.o:' 被替换为 'build/linux/src/foo.o:'。 替换字符串中的 $(BUILD_DIR) 和 $* 在展开时都包含正斜杠 - 如何将它们传递给 sed?
注意:这可能已经在这里得到了回答,但到目前为止我无法将这些答案应用于我的具体问题!
I am writing a generic makefile to build static libraries. It seems to work well so far, except for the line calling sed:
# Generic makefile to build a static library
ARCH = linux
CFLAGS = -O3 -Wall
SOURCES = src
BUILD_DIR = build/$(ARCH)
TARGET = $(BUILD_DIR)/libz.a
CFILES = $(foreach dir,$(SOURCES),$(wildcard $(dir)/*.c))
OBJECTS = $(addprefix $(BUILD_DIR)/,$(CFILES:.c=.o))
# Pull in the dependencies if they exist
# http://scottmcpeak.com/autodepend/autodepend.html
-include $(OBJECTS:.o=.dep)
default: create-dirs $(TARGET)
$(TARGET): $(OBJECTS)
$(AR) -rc $(TARGET) $^
$(BUILD_DIR)/%.o: %.c
$(CC) $(CFLAGS) -c lt; -o $@
$(CC) -M $(CFLAGS) $*.c > $(BUILD_DIR)/$*.tmp
sed s/.*:/$(BUILD_DIR)\\/$*.o:/ $(BUILD_DIR)/$*.tmp > $(BUILD_DIR)/$*.dep
@rm $(BUILD_DIR)/$*.tmp
.PHONY: create-dirs
create-dirs:
@for p in $(SOURCES); do mkdir -p $(BUILD_DIR)/$p; done
.PHONY: clean
clean:
rm -fr $(BUILD_DIR)
sed is used to replace the path/name of the object file with the full path of where the object actually is. e.g. 'src/foo.o:' is replaced with 'build/linux/src/foo.o:' in this example.
$(BUILD_DIR) and $* in the replacement string both contain forward slashes when expanded - how do I pass them to sed?
Note: This might have been answered here before, but I am so far unable to apply those answers to my specific problem!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
sed s~foo~bar~g
"
(至少在 shell 中),变量仍然会被扩展:echo "Hello $PLANET"
sed s~foo~bar~g
"
(at least in the shell), and variables will still be expanded:echo "Hello $PLANET"
如果您希望将路径扩展到您使用的文件
如果您的变量 PATH_VARIABLE 看起来像 /opt/path/user/home/etc
现在它会膨胀为:
这应该允许 sed 正确插入“/”。
-马特
If you want the path to expand into a file you use
If your variable PATH_VARIABLE looked like /opt/path/user/home/etc
it will now inflate to:
This should allow sed to insert the ' / ' correctly.
-Matt