[关闭]
@cyysu 2017-10-13T09:13:54.000000Z 字数 3559 阅读 1020

Makefile-基础篇(二)

  • 时间:2017年10月13日
  • 作者:Kali
  • 邮箱:cyysu.github.io@gmail.com
  • 版本:3.0
  • 描述:Makefile-基础篇二,继续介绍Makefile里面知识

Makefile系列教程


基础知识

  1. # makefile中的if
  2. SUBDIR += $(if $(SRC_DIR) $(SRC_DIR),/home/src)
  3. 函数的结果是:如果“SRC_DIR”变量值不为空,则将变量“SRC_DIR”指定
  4. 的目录作为一个子目录;否则将目录“/home/src”作为一个子目录。
  5. # 这里在讨论一下makefile的赋值问题,这里先给出答案,然后我们写一个例子去验证
  6. = 是最基本的赋值
  7. := 是覆盖之前的值
  8. ?= 是如果没有被赋值过就赋予等号后面的值
  9. += 是添加等号后面的值
  10. ifdef DEFINE_VRE
  11. VRE = Hello World!”
  12. else
  13. endif
  14. ifeq ($(OPT),define)
  15. VRE ?= Hello World! First!”
  16. endif
  17. ifeq ($(OPT),add)
  18. VRE += Kelly!”
  19. endif
  20. ifeq ($(OPT),recover)
  21. VRE := Hello World! Again!”
  22. endif
  23. all:
  24. @echo $(VRE)
  25. # 这里附上测试
  26. mj@DZ:~/桌面$ make DEFINE_VRE=true OPT=define
  27. Hello World!”
  28. mj@DZ:~/桌面$ make DEFINE_VRE=true OPT=add
  29. Hello World!” Kelly!”
  30. mj@DZ:~/桌面$ make DEFINE_VRE=true OPT=recover
  31. Hello World! Again!”
  32. mj@DZ:~/桌面$ make DEFINE_VRE= OPT=define
  33. Hello World! First!”
  34. # 在举一个常见的错误
  35. Makefile:19: *** missing separator. Stop
  36. 出现上面的错误基本就是命令那一部分不是用Tab键开头的,Makefile对格式的要求还是蛮严格的。
  37. # 如果一个工程很庞大,那么我们就需要一些调试信息了
  38. 1,使用info/warning/error增加调试信息
  39. 方法1: $(info, "here add the debug info") # 这个无法显示行号
  40. 方法2: $(warning, "here add the debug info")
  41. 方法3: $(error "error: this will stop the compile") # 这个可以停止当前makefile的编译
  42. 方法4: 打印变量的值
  43. $(info, $(TARGET_DEVICE) )

Makefile内置函数和变量

  1. # 1.字符串处理函数
  2. $(subst FROM,TO,TEXT)
  3. # eg1
  4. mj@DZ:~/桌面$ cat Makefile
  5. TARGETS=111.cpp 222.cpp 333.cpp
  6. TARGETS1=$(subst cpp,o,$(TARGETS))
  7. SIGNAL = $(shell printf "\033[34;1m★\033[0m")
  8. all: ;$(info $(SIGNAL) build all goal)
  9. @echo "替换之前:" $(TARGETS)
  10. @echo "替换之后:" $(TARGETS1)
  11. mj@DZ:~/桌面$ make
  12. build all goal
  13. 替换之前: 111.cpp 222.cpp 333.cpp
  14. 替换之后: 111.o 222.o 333.o
  15. # 2.去除空格函数 返回值:无前导和结尾空字符、使用单一空格分割的多单词字符串。
  16. $(strip STRINT)
  17. # eg2
  18. SIGNAL = $(shell printf "\033[34;1m★\033[0m")
  19. # 第一个栗子
  20. string1 = 111.cpp 222.cpp 333.cpp
  21. string2 = $(subst cpp,o,$(string1))
  22. # 第二个栗子
  23. stringStrip1 = " <Hello World!!> "
  24. stringStrip2 = $(strip $(stringStrip1))
  25. string: ;$(info $(SIGNAL) build $@ goal)
  26. @echo "替换之前:" $(string1)
  27. @echo "替换之后:" $(string2)
  28. stringStrip:;$(info $(SIGNAL) build $@ goal)
  29. @echo "替换之前:" $(stringStrip1)
  30. @echo "替换之后:" $(stringStrip2)
  31. # 3. 引用其他makefile和定义函数以及取单词
  32. 原型:
  33. $(word <n>;,<text>;)
  34. $(wordlist <s>;,<e>;,<text>;)
  35. mj@DZ:~/桌面$ cat test.mk
  36. SIGNAL = $(shell printf "\033[34;1m★\033[0m")
  37. define testMakefile
  38. @echo Receive parameter $(1) $(2) $(3)
  39. endef
  40. # 查看主Makefile
  41. mj@DZ:~/桌面$ cat Makefile
  42. SIGNAL = $(shell printf "\033[34;1m★\033[0m")
  43. # 第一个栗子
  44. string1 = 111.cpp 222.cpp 333.cpp
  45. string2 = $(subst cpp,o,$(string1))
  46. # 第二个栗子
  47. stringStrip1 = " <Hello World!!> "
  48. stringStrip2 = $(strip $(stringStrip1))
  49. # 第三个栗子
  50. include ./test.mk
  51. # 第四个栗子
  52. stringword1 = "test1 test2 test3 test4"
  53. stringword2 = $(word 2,$(stringword1))
  54. stringword3 = $(wordlist 2,3,$(stringword1))
  55. # 目标
  56. call: ;$(info $(SIGNAL) build $@ goal)
  57. @echo $(1)
  58. $(call testMakefile,var1,var2,var3)
  59. string: ;$(info $(SIGNAL) build $@ goal)
  60. @echo "前:" $(string1)
  61. @echo "后:" $(string2)
  62. stringStrip: ;$(info $(SIGNAL) build $@ goal)
  63. @echo "前:" $(stringStrip1)
  64. @echo "后:" $(stringStrip2)
  65. stringWord:
  66. @echo "前:" $(stringword1)
  67. @echo "中:" $(stringword2)
  68. @echo "后:" $(stringword3)
  69. # 获取当前makefile
  70. 通过 $(MAKEFILE_LIST) 这个内置变量

完整Makefile

  1. SIGNAL = $(shell printf "\033[34;1m★\033[0m")
  2. # 第一个栗子
  3. string1 = 111.cpp 222.cpp 333.cpp
  4. string2 = $(subst cpp,o,$(string1))
  5. # 第二个栗子
  6. stringStrip1 = " <Hello World!!> "
  7. stringStrip2 = $(strip $(stringStrip1))
  8. # 第三个栗子
  9. include ./test/test.mk
  10. # 第四个栗子
  11. stringword1 = "test1 test2 test3 test4"
  12. stringword2 = $(word 2,$(stringword1))
  13. stringword3 = $(wordlist 2,3,$(stringword1))
  14. call: ;$(info $(SIGNAL) build $@ goal)
  15. @echo $(1)
  16. $(call testMakefile,var1,var2,var3)
  17. string: ;$(info $(SIGNAL) build $@ goal)
  18. @echo "前:" $(string1)
  19. @echo "后:" $(string2)
  20. stringStrip: ;$(info $(SIGNAL) build $@ goal)
  21. @echo "前:" $(stringStrip1)
  22. @echo "后:" $(stringStrip2)
  23. stringWord:
  24. @echo "前:" $(stringword1)
  25. @echo "中:" $(stringword2)
  26. @echo "后:" $(stringword3)
  27. getMakefile:
  28. @echo "当前Makefile有:" $(MAKEFILE_LIST)
  29. @echo $(MAKEFILE_LIST) | cut -d " " -f 1
  30. @echo $(MAKEFILE_LIST) | cut -d " " -f 2

附开发环境

这里附上一张我配置gedit开发环境

gedit开发环境

打赏

                    支付宝                                                         微信

微信与支付宝支付

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注