【发布时间】:2013-01-23 08:04:06
【问题描述】:
我只是写了一些纯c程序来练习,每个小程序都有一个XXX.h文件XXX.c文件和XXX_test.c文件,里面有main函数。 XXX.c 文件和 XXX_test.c 都可能包含其他一些 YYY.h 文件。我使用 gcc -MM 自动查找 XXX_test.c 文件的依赖关系。最后,我想出了makefile(使用GNU makefile)XXX.mk:
# change your targets to your real targets
MINGW := MINGW32_NT-6.1
CC = gcc
OFLAG = -o
CFLAGS = -Wall -g -DDEBUG
# get the dependent header files and change their name to source files
TARGET := XXX_test
FILES := $(filter %.c %.h, $(shell gcc -MM $(addsuffix .c, $(TARGET))))
SRCS := $(FILES:.h=.c)
DEPS := $(SRCS:.c=.d)
OBJECTS := $(SRCS:.c=.o)
#determine the os platform
ifdef SystemRoot
ifeq ($(shell uname), ${MINGW})
RM = rm -f
FixPath = $1
else
RM = del /Q
FixPath = $(subst /,\,$1)
endif
else
ifeq ($(shell uname), Linux)
RM = rm -f
FixPath = $1
endif
endif
all: ${TARGET}
# include .d files generated by gcc -MMD opt
-include $(DEPS)
# to generate the .o files as well as the .d files
%.o:%.c
$(CC) $(CFLAGS) -c -MMD $< -o $@
# generate the executable program
${TARGET}: ${OBJECTS}
${CC} $^ ${OFLAG} $@
.PHONY:clean
# the "$" is not needed before "FixPath"
clean:
$(RM) $(call FixPath, ${OBJECTS} ${DEPS})
每个小程序都有一个XXX.mk 文件。它们几乎相同,只是变量TARGET 中的内容不同(参见上面的makefile)。所以我有XXX.mk、YYY.mk 等...我想问的问题是如何将所有的makefile,即XXX.mk、YYY.mk...转换成一个makefile? 例如generic.mk,变量TARGETS(注意S)分配给XXX_test YYY_test ...。我觉得难以处理的是variable dependencies:
TARGET --> OBJECTS --> SRCS --> FILES --> TARGET(the FILES is generate by gcc using -MM opt)
↓
DEPS(used to include the .d files)
每个目标(例如 XXX_test)应该依赖于它们的一堆文件。我想看看 GNU makefile 的强大功能,我该怎么做呢?我正在学习使用 gmake。
【问题讨论】:
-
无论二进制文件的数量如何,您是否可以使用总共需要两个单独的 Makefile 的 2 阶段构建?