Last active
May 13, 2017 11:00
-
-
Save motchy869/ac022c3f91026b348f025c9cfbd74d1a to your computer and use it in GitHub Desktop.
Windows用Makefile。ソースファイル自動検索
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#makefile for Windows | |
#プログラム名 | |
PROGNAME := main | |
#ビルドタイプ | |
BUILD_TYPE := debug | |
#プログラミング言語(C, C++) | |
LANG := C++ | |
#コンパイラ, ソースファイル拡張子 | |
ifeq ($(LANG), C) | |
CMP := gcc | |
SRCEXT := c | |
else ifeq ($(LANG), C++) | |
CMP := g++ | |
SRCEXT := cpp | |
endif | |
#ソースファイル検索 | |
#(dir の出力は改行区切りだが、make内で変数に代入する際に自動的に半角スペース区切りになるらしい) | |
SRCS := $(subst $(CURDIR)/,,$(subst \,/,$(shell dir *.$(SRCEXT) /b /a-d /s))) | |
#コンパイルオプション | |
CMPFLAGS := -Wall | |
ifeq ($(BUILD_TYPE), release) | |
CMPFLAGS += -DNDEBUG -O3 | |
else ifeq ($(BUILD_TYPE), debug) | |
CMPFLAGS += -O0 -g | |
endif | |
#実行時引数 | |
ARG := | |
#出力先ディレクトリ | |
OUTDIR := ../build/$(BUILD_TYPE) | |
#実行ファイルパス | |
EXEPATH := $(OUTDIR)/$(PROGNAME) | |
#オブジェクトファイル | |
OBJS := $(SRCS:%.$(SRCEXT)=$(OUTDIR)/%.o) | |
#ヘッダ依存関係記述ファイル | |
DEPS := $(SRCS:%.$(SRCEXT)=$(OUTDIR)/%.d) | |
.PHONY: default | |
default: $(EXEPATH) | |
#プログラム実行 | |
.PHONY: run | |
run: $(EXEPATH) | |
$(EXEPATH) $(ARG) | |
#依存関係記述ファイルのインクルード | |
-include $(DEPS) | |
#実行ファイル生成 | |
$(EXEPATH): $(OBJS) | |
$(CMP) $(LDFLAGS) -o $@ $^ | |
#オブジェクトファイルとヘッダ依存関係ファイルを生成 | |
$(OUTDIR)/%.o: %.$(SRCEXT) | |
@if not exist $(subst /,\,$(dir $@)) mkdir $(subst /,\,$(dir $@)) | |
$(CMP) $(CMPFLAGS) -o $@ -c -MMD -MP -MF $(@:%.o=%.d) $< | |
#クリーニング | |
#ファイルがない場合に del が煩いので >nul 2>&1 で黙らせる。 | |
.PHONY: clean | |
clean: | |
@del /f /q $(subst /,\,$(EXEPATH)).exe $(subst /,\,$(OBJS)) $(subst /,\,$(DEPS)) >nul 2>&1 || rem hoge | |
#削除 | |
.PHONY: remove | |
remove: | |
@if exist $(subst /,\,$(OUTDIR)) rd /s /q $(subst /,\,$(OUTDIR)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment