練習用にいくつかの純粋な C プログラムを作成しました。各小さなプログラムには、XXX.hファイルXXX.cfile とXXX_test.cmain 関数を含むファイルがあります。XXX.cファイルと の両方に、XXX_test.c他のYYY.hファイルが含まれている可能性があります。gcc -MM を使用して、XXX_test.cファイルの依存関係を自動的に見つけます。最後に、(GNU makefile を使用して) 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聞きたい質問は、すべてのメイクファイル、つまり ... を 1 つのメイクファイルに変換するにはどうすればよいかということです。XXX.mkYYY.mkたとえば、 に割り当てられgeneric.mkた変数TARGETS( に注意してください) を使用します。私が扱いにくいと思うのは、次のとおりです。SXXX_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の使い方を学んでいます。