makefile                
            高度なメークファイル
        
        
            
    サーチ…
異なるソースフォルダから異なるターゲットフォルダへのビルド
このMakefileの主な特長:
- 特定のフォルダ内のCソースの自動検出
- 複数のソースフォルダ
- オブジェクトファイルと依存ファイルの複数の対応するターゲットフォルダ
- 各ターゲットフォルダの自動ルール生成
- ターゲットフォルダが存在しない場合の作成
-  gcc依存関係管理:必要なものだけを構築する
-  UnixおよびDOSシステムでDOSする
- GNU Make用
このMakefileを使って、次のような構造のプロジェクトを構築することができます:
\---Project
    +---Sources
    |   +---Folder0
    |   |       main.c
    |   |       
    |   +---Folder1
    |   |       file1_1.c
    |   |       file1_1.h
    |   |       
    |   \---Folder2
    |           file2_1.c
    |           file2_1.h
    |           file2_2.c
    |           file2_2.h
    \---Build
        |   Makefile
        |   myApp.exe
        |   
        +---Folder0
        |       main.d
        |       main.o
        |       
        +---Folder1
        |       file1_1.d
        |       file1_1.o
        |       
        \---Folder2
                file2_1.d
                file2_1.o
                file2_2.d
                file2_2.o
メークファイル
# Set project directory one level above of Makefile directory. $(CURDIR) is a GNU make variable containing the path to the current working directory
PROJDIR := $(realpath $(CURDIR)/..)
SOURCEDIR := $(PROJDIR)/Sources
BUILDDIR := $(PROJDIR)/Build
# Name of the final executable
TARGET = myApp.exe
# Decide whether the commands will be shwon or not
VERBOSE = TRUE
# Create the list of directories
DIRS = Folder0 Folder1 Folder2
SOURCEDIRS = $(foreach dir, $(DIRS), $(addprefix $(SOURCEDIR)/, $(dir)))
TARGETDIRS = $(foreach dir, $(DIRS), $(addprefix $(BUILDDIR)/, $(dir)))
# Generate the GCC includes parameters by adding -I before each source folder
INCLUDES = $(foreach dir, $(SOURCEDIRS), $(addprefix -I, $(dir)))
# Add this list to VPATH, the place make will look for the source files
VPATH = $(SOURCEDIRS)
# Create a list of *.c sources in DIRS
SOURCES = $(foreach dir,$(SOURCEDIRS),$(wildcard $(dir)/*.c))
# Define objects for all sources
OBJS := $(subst $(SOURCEDIR),$(BUILDDIR),$(SOURCES:.c=.o))
# Define dependencies files for all objects
DEPS = $(OBJS:.o=.d)
# Name the compiler
CC = gcc
# OS specific part
ifeq ($(OS),Windows_NT)
    RM = del /F /Q 
    RMDIR = -RMDIR /S /Q
    MKDIR = -mkdir
    ERRIGNORE = 2>NUL || true
    SEP=\\
else
    RM = rm -rf 
    RMDIR = rm -rf 
    MKDIR = mkdir -p
    ERRIGNORE = 2>/dev/null
    SEP=/
endif
# Remove space after separator
PSEP = $(strip $(SEP))
# Hide or not the calls depending of VERBOSE
ifeq ($(VERBOSE),TRUE)
    HIDE =  
else
    HIDE = @
endif
# Define the function that will generate each rule
define generateRules
$(1)/%.o: %.c
    @echo Building $$@
    $(HIDE)$(CC) -c $$(INCLUDES) -o $$(subst /,$$(PSEP),$$@) $$(subst /,$$(PSEP),$$<) -MMD
endef
.PHONY: all clean directories 
all: directories $(TARGET)
$(TARGET): $(OBJS)
    $(HIDE)echo Linking $@
    $(HIDE)$(CC) $(OBJS) -o $(TARGET)
# Include dependencies
-include $(DEPS)
# Generate rules
$(foreach targetdir, $(TARGETDIRS), $(eval $(call generateRules, $(targetdir))))
directories: 
    $(HIDE)$(MKDIR) $(subst /,$(PSEP),$(TARGETDIRS)) $(ERRIGNORE)
# Remove all objects, dependencies and executable files generated during the build
clean:
    $(HIDE)$(RMDIR) $(subst /,$(PSEP),$(TARGETDIRS)) $(ERRIGNORE)
    $(HIDE)$(RM) $(TARGET) $(ERRIGNORE)
    @echo Cleaning done ! 
このMakefileの使い方このMakefileをプロジェクトに適応させるには、以下のことが必要です。
- 目標名に合うようにTARGET変数を変更してください
-  SOURCEDIRとBUILDDIRSourcesとBuildフォルダの名前を変更する
- Makefile自体のMakefileの冗長レベルを変更するか、makeの呼び出しで変更する
- ソースとビルドフォルダに一致するようにDIRS内のフォルダの名前を変更する
- 必要に応じて、コンパイラとフラグを変更します
ジッピングリスト
 GNU make 
    
    
    
    
    このpairmap関数は3つの引数をとります: 
- 関数名
- スペースで区切られた最初のリスト
- 2番目のスペース区切りリスト
リスト内の各圧縮されたタプルについて、次の引数を使用して関数を呼び出します。
- 最初のリストのタプル要素
- 2番目のリストのタプル要素
関数展開のスペースで区切られたリストに展開されます。
list-rem = $(wordlist 2,$(words $1),$1)
pairmap = $(and $(strip $2),$(strip $3),$(call \
    $1,$(firstword $2),$(firstword $3)) $(call \
    pairmap,$1,$(call list-rem,$2),$(call list-rem,$3)))
たとえば、次のようになります。
LIST1 := foo bar baz
LIST2 := 1 2 3
func = $1-$2
all:
    @echo $(call pairmap,func,$(LIST1),$(LIST2))
.PHONY: all
 foo-1 bar-2 baz-3ます。 
Modified text is an extract of the original Stack Overflow Documentation
        ライセンスを受けた CC BY-SA 3.0
        所属していない Stack Overflow