在makefile中使用sed;如何转义变量? [英] Using sed in a makefile; how to escape variables?

查看:380
本文介绍了在makefile中使用sed;如何转义变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个通用的makefile来构建静态库.到目前为止,它似乎运行良好,但调用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 $< -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?

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!

推荐答案

  • 您可以使用除正斜杠之外的其他任何东西作为sed中的分隔符.例如. sed s~foo~bar~g
  • 您可以使用双引号"(至少在外壳程序中),并且变量仍将扩展:echo "Hello $PLANET"
    • You can use anything else than forward slashes as separator in sed. E.g. sed s~foo~bar~g
    • You can use double quotes " (at least in the shell), and variables will still be expanded: echo "Hello $PLANET"
    • 这篇关于在makefile中使用sed;如何转义变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆