【发布时间】:2011-07-18 07:39:10
【问题描述】:
我正在构建一个将用于构建发布或调试目标库的 makefile。我想将对象和自动生成的依赖文件放入调试或发布目录结构中,具体取决于请求的 makefile 目标。我不想指定可测试的 make 命令行参数(即 DBG=1),但更愿意分别运行 make -f Makefile 或 make -f Makefiel dbg 来实现发布和调试目标。把那部分记下来了。我知道我无法分配包含对象目录名称(发布或调试)的特定于目标的变量,该变量可用作规则中目标规范的一部分,就像我在下面显示的示例中所做的那样。在此示例中,OBJDIR 是我想根据构建目标设置的特定于目标的变量。因此,在此示例中,目标规则 $(OBJDIR)/%.o 中的 $(OBJDIR) 为空。关于如何很好地执行建议的步骤的任何建议? (显示的示例只是一个复制/粘贴未经验证的示例......语法未经验证......事实上,我无法让选项卡正确显示......我希望得到一些实现想法)。 (另外,如图所示,$(OBJDIR) 未在 clean 目标中设置...因为它不在 dbg/all 目标依赖层次结构中...想法?)提前致谢。
生成文件:
OBJS := a.o b.o c.o
SRCS := $(OBJS:.o=.c)
-- Set up the release and the debug directory paths and object filenames
RELEASE_DIR := ./release
RELEASE_OBJ := $(OBJS:%=$(RELEASE_DIR)/%)
DEBUG_DIR := ./debug
DEBUG_OBJ := $(OBJS:%=$(DEBUG_DIR)/%)
.PHONY : all dbg
all: CFLAGS = -O3
all: OBJDIR := RELEASE_DIR
all: df := $(RELEASE_DIR)/$(*F)
all: init_release lib1.so
dbg: CFLAGS = -g -O0
dbg: OBJDIR := DEBUG_DIR
dbg: df := $(DEBUG_DIR)/$(*F)
dbg: init_debug lib1.so
Lib1.so: $(OBJ)
init_release:
-@mkdir -p $(RELEASE_DIR)
init_debug:
-@mkdir -p $(DEBUG_DIR)
lib1.so: $(OBJ)
@echo '--------------------------------------------------------------'
@echo linking $@
@gcc -shared -o lib1.so $(OBJ)
-Compile including advance dependancy generation alg, per Tom Tromey:
# http://make.paulandlesley.org/autodep.html
$(OBJDIR)/%.o: %.c
echo $@
echo $(OBJDIR)
echo compiling $@
$(COMPILE.c) -MD -o $@ $<
cp $(df).d $(df).P; \
sed -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$//' \
-e '/^$$/ d' -e 's/$$/ :/' < $(df).d >> $(df).P; \
rm -f $(df)/$*.d
# If the goal is "clean", don't include these to avoid trying to build them
ifneq($(MAKECMDGOALS),clean)
-include $(SRCS:%.c=$(OBJDIR)/%.P)
endif
clean:
-@rm -f $(OBJDIR)/*.[Pdo] lib1.so
【问题讨论】:
标签: makefile