常用的C/C++工程Makefile模板

转帖:一辉的文章

在Linux下做开发难免要接触makefile,整个项目的构建都依赖于它。100个developer有100种makefile的写法,在一个较大的项目中,各种各样的makefile无论在开发、后期维护还是整个系统的持续集成都是一个负担。

有幸参与重构一个遗留系统的makefile,以下是一些心得和一个makefile模板。
重构目的:
1.清晰易懂、容易维护
2.方便系统的持续集成
重构原则:
1.子模块makefile模板化
2.外部依赖、通用宏定义集中化
3.中间和最终输出集中,便于系统构建

下面是总结出的一个makefile模板,我在日常的开发中都用它,其中加入了详细的注释。

  1. #Get the ROOT_PATH which common files located, assuming this makefile located in $(ROOT_PATH)/src/sub_module
  2. ROOT_PATH = $(shell cd ../..; pwd)
  3. #Where define the path of third party modules
  4. include $(ROOT_PATH)/path
  5. #Where define common macros such as CC=gcc, CXX=g++ and so on
  6. include $(ROOT_PATH)/common
  7. #Set target output path and the path of intermidiate object
  8. #The path macros should include in $(ROOT_PATH)/path
  9. OUT_PATH = $(OUTPUT_PATH)/submodule
  10. OBJ_PATH = $(TMPOBJ_PATH)/submodule
  11. TARGET = $(OUT_PATH)/targe
  12. #If the targe is share object then set corresponding flags
  13. #which should define in $(ROOT_PATH)/common
  14. CFLAGS += $(CFLAGS_SO)
  15. LDFLAGS += $(LDFLAGS_SO)
  16. #Custom Predefines
  17. CFLAGS += -DXXXXXXXX
  18. CFLAGS += -DYYYYYYYY
  19. #Dependent header files
  20. #The path macros should include in $(ROOT_PATH)/path
  21. CFLAGS += -I. /
  22. -I$(XXXX_INC) /
  23. -I$(YYYY_INC) /
  24. -I$(ZZZZ_INC)
  25. #Dependent libraries
  26. #The path macros should include in $(ROOT_PATH)/path
  27. LDFLAGS += -L$(XXXX_LIB) -lxxxx /
  28. -L$(YYYY_LIB) -lyyyy
  29. #Set CPP source directory
  30. CPP_SRCDIR = .
  31. #Or set specific CPP Source files
  32. ADDITIONAL_CPP_SOURCES = /
  33. $(PATH_A)/a.cpp /
  34. $(PATH_B)/b.cpp
  35. #Traverse every directory in $(CPP_SRCDIR), and find every cpp file
  36. CPP_SOURCES = $(foreach d,$(CPP_SRCDIR),$(wildcard $(d)/*.cpp) ) $(ADDITIONAL_CPP_SOURCES)
  37. #Traverse every cpp file in $(CPP_SOURCES) and get corresponding object file(.o)
  38. CPP_OBJFILES = $(patsubst %.cpp,$(OBJ_PATH)/%.o,$(notdir $(CPP_SOURCES)))
  39. #Set C source directory
  40. C_SRCDIR =
  41. #Or set specific C Source files
  42. ADDITIONAL_C_SOURCES = /
  43. $(PATH_A)/a.c /
  44. $(PATH_B)/b.c
  45. #C Source files
  46. C_SOURCES = $(foreach d,$(C_SRCDIR),$(wildcard $(d)/*.c) ) $(ADDITIONAL_C_SOURCES)
  47. C_OBJFILES = $(patsubst %.c,$(OBJ_PATH)/%.o,$(notdir $(C_SOURCES)))
  48. #Set vpath where to find these types of files
  49. vpath %.cpp $(dir $(CPP_SOURCES))
  50. vpath %.c $(dir $(C_SOURCES))
  51. vpath %.o $(OBJ_PATH)
  52. #The first target to be executed
  53. all: target
  54. target: $(TARGET)
  55. #Static dependecy pattern
  56. #$(OBJ_PATH)/%.o define the pattern of target and %.c or %.cpp is the final dependency
  57. $(C_OBJFILES): $(OBJ_PATH)/%.o: %.c
  58. -mkdir -p $(OBJ_PATH)
  59. $(CXX) -c $(CFLAGS) -o $@ $<
  60. $(CPP_OBJFILES): $(OBJ_PATH)/%.o: %.cpp
  61. -mkdir -p $(OBJ_PATH)
  62. $(CXX) -c $(CFLAGS) -o $@ $<
  63. $(TARGET): $(CPP_OBJFILES) $(C_OBJFILES)
  64. -mkdir -p $(OUT_PATH)
  65. $(CXX) -o $@ $^ $(LDFLAGS)
  66. clean:
  67. -rm -rf $(OBJ_PATH)
  68. -rm -f $(TARGET)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值