我只是写了一些纯c程序来练习,每个小程序都有一个XXX.h
文件XXX.c
文件和XXX_test.c
带有main函数的文件。该XXX.c
文件和该文件XXX_test.c
都可能包含其他一些YYY.h
文件。我使用 gcc -MMXXX_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。