Makefile 示例
介绍
make
命令在当前目录依次搜索 Makefile
、makefile
、GNUmakefile
文件,若找不到就报错
- make 命令支持通过时间戳判断源文件和编译产物的先后关系,减少文件重新编译,提高编译速度
help
$ make --help
Usage: make [options] [target] ...
Options:
-b, -m Ignored for compatibility.
-B, --always-make Unconditionally make all targets.
-C DIRECTORY, --directory=DIRECTORY
Change to DIRECTORY before doing anything.
-d Print lots of debugging information.
--debug[=FLAGS] Print various types of debugging information.
-e, --environment-overrides
Environment variables override makefiles.
--eval=STRING Evaluate STRING as a makefile statement.
-f FILE, --file=FILE, --makefile=FILE
Read FILE as a makefile.
-h, --help Print this message and exit.
-i, --ignore-errors Ignore errors from recipes.
-I DIRECTORY, --include-dir=DIRECTORY
Search DIRECTORY for included makefiles.
-j [N], --jobs[=N] Allow N jobs at once; infinite jobs with no arg.
-k, --keep-going Keep going when some targets can't be made.
-l [N], --load-average[=N], --max-load[=N]
Don't start multiple jobs unless load is below N.
-L, --check-symlink-times Use the latest mtime between symlinks and target.
-n, --just-print, --dry-run, --recon
Don't actually run any recipe; just print them.
-o FILE, --old-file=FILE, --assume-old=FILE
Consider FILE to be very old and don't remake it.
-O[TYPE], --output-sync[=TYPE]
Synchronize output of parallel jobs by TYPE.
-p, --print-data-base Print make's internal database.
-q, --question Run no recipe; exit status says if up to date.
-r, --no-builtin-rules Disable the built-in implicit rules.
-R, --no-builtin-variables Disable the built-in variable settings.
-s, --silent, --quiet Don't echo recipes.
-S, --no-keep-going, --stop
Turns off -k.
-t, --touch Touch targets instead of remaking them.
--trace Print tracing information.
-v, --version Print the version number of make and exit.
-w, --print-directory Print the current directory.
--no-print-directory Turn off -w, even if it was turned on implicitly.
-W FILE, --what-if=FILE, --new-file=FILE, --assume-new=FILE
Consider FILE to be infinitely new.
--warn-undefined-variables Warn when an undefined variable is referenced.
This program built for x86_64-pc-linux-gnu
Report bugs to <bug-make@gnu.org>
说明:
-C
指定路径
make -j 4
指定并行编译的任务数,可以大大缩短编译时间,-j
指定CPU个数
-j [N], --jobs[=N] Allow N jobs at once; infinite jobs with no arg.
规则
Makefile 由多条规则组成
目标: [依赖1[ 依赖2 [ 依赖3 [...]]]]
\t命令
[target] ... : [prerequisites] ...
<tab>[command]
...
...
xxx-%.gz: xxx-%
gzip --force --keep xxx-$*.exe
.DEFAULT_GOAL
设置默认目标,不设置默认为第一个
.DEFAULT_GOAL := default
default:...
shell
shell 函数
参数是操作系统的 shell 命令,功能和使用 (`) 相同
- 示例
ipaddress := $(shell ip a)
include
将别的 Makefile
包含进来,这很像 C 语言的 #include
# 假设有 Makefile a.mk、b.mk、c.mk,$(bar) 包含 e.mk f.mk,则
include foo.make *.mk $(bar)
# 等价于
include foo.make a.mk b.mk c.mk e.mk f.mk
# include 当文件不存在时会报错,使用 `-` 忽略错误,也可以使用 `sinclude` 替代
-include <filename>
类似的参数:
条件判断
# ifeq
mode = debug
hello: hello.c
ifeq ($(mode),debug)
@echo "debug mode"
gcc -g -o hello hello.c
else
@echo "release mode"
gcc -o hello hello.c
endif
# ifneq
mode = debug
hello: hello.c
ifneq ($(mode),)
@echo "debug mode"
gcc -g -o hello hello.c
else
@echo "release mode"
gcc -o hello hello.c
endif
# ifdef
mode =
hello: hello.c
ifdef mode
@echo "debug mode"
gcc -g -o hello hello.c
else
@echo "release mode"
gcc -o hello hello.c
endif
# ifndef
mode =
hello: hello.c
ifndef mode
@echo "debug mode"
gcc -g -o hello hello.c
else
@echo "release mode"
gcc -o hello hello.c
endif
Makefile 示例
Golang
# https://www.xiexianbin.cn/program/tools/2016-01-09-makefile/index.html
.PHONY: all test clean build build-linux build-mac build-windows
GOCMD=go
GOBUILD=$(GOCMD) build
GOCLEAN=$(GOCMD) clean
GOTEST=$(GOCMD) test
BINARY_NAME=main
BINARY_LINUX=$(BINARY_NAME)-linux
BINARY_MAC=$(BINARY_NAME)-darwin
BINARY_WIN=$(BINARY_NAME)-windows
help: ## Show this help.
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {sub("\\\\n",sprintf("\n%22c"," "), $$2);printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)
all: clean test build build-linux build-mac build-windows ## Build all
test: ## run test
$(GOTEST) -v ./...
clean: ## run clean bin files
$(GOCLEAN)
rm -f bin/$(BINARY_NAME)
build: ## build for current os
$(GOBUILD) -o bin/$(BINARY_NAME) -v
build-linux: ## build linux amd64
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GOBUILD) -o bin/$(BINARY_LINUX) -v
build-mac: ## build mac amd64
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 $(GOBUILD) -o bin/$(BINARY_MAC) -v
build-windows: ## build windows amd64
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 $(GOBUILD) -o bin/$(BINARY_WIN) -v