@cyysu
2017-10-13T09:13:54.000000Z
字数 3559
阅读 1020
- 时间:2017年10月13日
- 作者:Kali
- 邮箱:cyysu.github.io@gmail.com
- 版本:3.0
- 描述:Makefile-基础篇二,继续介绍Makefile里面知识
Makefile系列教程
# makefile中的if
SUBDIR += $(if $(SRC_DIR) $(SRC_DIR),/home/src)
函数的结果是:如果“SRC_DIR”变量值不为空,则将变量“SRC_DIR”指定
的目录作为一个子目录;否则将目录“/home/src”作为一个子目录。
# 这里在讨论一下makefile的赋值问题,这里先给出答案,然后我们写一个例子去验证
= 是最基本的赋值
:= 是覆盖之前的值
?= 是如果没有被赋值过就赋予等号后面的值
+= 是添加等号后面的值
ifdef DEFINE_VRE
VRE = “Hello World!”
else
endif
ifeq ($(OPT),define)
VRE ?= “Hello World! First!”
endif
ifeq ($(OPT),add)
VRE += “Kelly!”
endif
ifeq ($(OPT),recover)
VRE := “Hello World! Again!”
endif
all:
@echo $(VRE)
# 这里附上测试
mj@DZ:~/桌面$ make DEFINE_VRE=true OPT=define
“Hello World!”
mj@DZ:~/桌面$ make DEFINE_VRE=true OPT=add
“Hello World!” “Kelly!”
mj@DZ:~/桌面$ make DEFINE_VRE=true OPT=recover
“Hello World! Again!”
mj@DZ:~/桌面$ make DEFINE_VRE= OPT=define
“Hello World! First!”
# 在举一个常见的错误
Makefile:19: *** missing separator. Stop
出现上面的错误基本就是命令那一部分不是用Tab键开头的,Makefile对格式的要求还是蛮严格的。
# 如果一个工程很庞大,那么我们就需要一些调试信息了
1,使用info/warning/error增加调试信息
方法1: $(info, "here add the debug info") # 这个无法显示行号
方法2: $(warning, "here add the debug info")
方法3: $(error "error: this will stop the compile") # 这个可以停止当前makefile的编译
方法4: 打印变量的值
$(info, $(TARGET_DEVICE) )
# 1.字符串处理函数
$(subst FROM,TO,TEXT)
# eg1
mj@DZ:~/桌面$ cat Makefile
TARGETS=111.cpp 222.cpp 333.cpp
TARGETS1=$(subst cpp,o,$(TARGETS))
SIGNAL = $(shell printf "\033[34;1m★\033[0m")
all: ;$(info $(SIGNAL) build all goal)
@echo "替换之前:" $(TARGETS)
@echo "替换之后:" $(TARGETS1)
mj@DZ:~/桌面$ make
★ build all goal
替换之前: 111.cpp 222.cpp 333.cpp
替换之后: 111.o 222.o 333.o
# 2.去除空格函数 返回值:无前导和结尾空字符、使用单一空格分割的多单词字符串。
$(strip STRINT)
# eg2
SIGNAL = $(shell printf "\033[34;1m★\033[0m")
# 第一个栗子
string1 = 111.cpp 222.cpp 333.cpp
string2 = $(subst cpp,o,$(string1))
# 第二个栗子
stringStrip1 = " <Hello World!!> "
stringStrip2 = $(strip $(stringStrip1))
string: ;$(info $(SIGNAL) build $@ goal)
@echo "替换之前:" $(string1)
@echo "替换之后:" $(string2)
stringStrip:;$(info $(SIGNAL) build $@ goal)
@echo "替换之前:" $(stringStrip1)
@echo "替换之后:" $(stringStrip2)
# 3. 引用其他makefile和定义函数以及取单词
原型:
$(word <n>;,<text>;)
$(wordlist <s>;,<e>;,<text>;)
mj@DZ:~/桌面$ cat test.mk
SIGNAL = $(shell printf "\033[34;1m★\033[0m")
define testMakefile
@echo Receive parameter $(1) $(2) $(3)
endef
# 查看主Makefile
mj@DZ:~/桌面$ cat Makefile
SIGNAL = $(shell printf "\033[34;1m★\033[0m")
# 第一个栗子
string1 = 111.cpp 222.cpp 333.cpp
string2 = $(subst cpp,o,$(string1))
# 第二个栗子
stringStrip1 = " <Hello World!!> "
stringStrip2 = $(strip $(stringStrip1))
# 第三个栗子
include ./test.mk
# 第四个栗子
stringword1 = "test1 test2 test3 test4"
stringword2 = $(word 2,$(stringword1))
stringword3 = $(wordlist 2,3,$(stringword1))
# 目标
call: ;$(info $(SIGNAL) build $@ goal)
@echo $(1)
$(call testMakefile,var1,var2,var3)
string: ;$(info $(SIGNAL) build $@ goal)
@echo "前:" $(string1)
@echo "后:" $(string2)
stringStrip: ;$(info $(SIGNAL) build $@ goal)
@echo "前:" $(stringStrip1)
@echo "后:" $(stringStrip2)
stringWord:
@echo "前:" $(stringword1)
@echo "中:" $(stringword2)
@echo "后:" $(stringword3)
# 获取当前makefile
通过 $(MAKEFILE_LIST) 这个内置变量
SIGNAL = $(shell printf "\033[34;1m★\033[0m")
# 第一个栗子
string1 = 111.cpp 222.cpp 333.cpp
string2 = $(subst cpp,o,$(string1))
# 第二个栗子
stringStrip1 = " <Hello World!!> "
stringStrip2 = $(strip $(stringStrip1))
# 第三个栗子
include ./test/test.mk
# 第四个栗子
stringword1 = "test1 test2 test3 test4"
stringword2 = $(word 2,$(stringword1))
stringword3 = $(wordlist 2,3,$(stringword1))
call: ;$(info $(SIGNAL) build $@ goal)
@echo $(1)
$(call testMakefile,var1,var2,var3)
string: ;$(info $(SIGNAL) build $@ goal)
@echo "前:" $(string1)
@echo "后:" $(string2)
stringStrip: ;$(info $(SIGNAL) build $@ goal)
@echo "前:" $(stringStrip1)
@echo "后:" $(stringStrip2)
stringWord:
@echo "前:" $(stringword1)
@echo "中:" $(stringword2)
@echo "后:" $(stringword3)
getMakefile:
@echo "当前Makefile有:" $(MAKEFILE_LIST)
@echo $(MAKEFILE_LIST) | cut -d " " -f 1
@echo $(MAKEFILE_LIST) | cut -d " " -f 2
这里附上一张我配置gedit开发环境
支付宝 微信