前面讲了Makefile 的简单语法和简单的应用模板,但在实际项目应用中比这个肯定复杂很多,但是我想说他的Makefile应用模式都是大同小异,只是代码量和工程复杂度大写而已。
我觉得一个完整的工程目录应该这样:
1、工程文件结构目录应该要非常清晰,并且模块化,各个模块下的Makefile 独立
2、可以分别单独编译各个单独模块成.a 或.so
3、在顶层目录下可以一次性生成可执行文件。
现在我搭建了如下工程目录
build为编译产生的结果文件,out 为可执行bin文件,libs为生成的库
project 为工程文件夹,下面有module1 、module2 、module3三个模块,main.c 为主函数它将生成可执行文件,虚线框为编译时生成的文件夹或文件
module2 module3和module1的文件结构相同图中没有画出来。
现进行如下分工,module1 module2 将产生静态库到 build/libs/static 中,module3将产生动态库到 build/libs/dynamic 中,最后main.c 将调用这些库编译生成可执行文件到
build/out 中。
为了提高模块的复用性,我们会在顶层目录下,写一个rules.mk文件,事实上很多项目是这么做的比如uboot
# rules .mk
# Generic Makefile for C/C++ Program
# Author:
# Description: # ------------
# This is an easily customizable makefile template. The purpose is to
# provide an instant building environment for C/C++ programs.
#
# It searches all the C/C++ source files in the specified directories,
# makes dependencies, compiles and links to form an executable.
#
# Besides its default ability to build C/C++ programs which use only
# standard C/C++ libraries, you can customize the Makefile to build
# those using other libraries. Once done, without any changes you can
# then build programs using the same or less libraries, even if source
# files are renamed, added or removed. Therefore, it is particularly
# convenient to use it to build codes for experimental or study use.
#
# GNU make is expected to use the Makefile. Other versions of makes
#
.PHONY : all clean
# Top directory Makefile
# The C program compiler
CC = gcc
MACRO = DEBUGALL
CFLAGS += -g -werror -D$(MACRO)
AR = ar
ARFLAGES = crv
# default execute output directory
ifeq ($(DIR_EXES),)
DIR_EXES = $(TOPDIR)/build/out
endif
# defaulet libaray creat directory
ifeq ($(DIR_LIBS),)
DIR_LIBS = $(TOPDIR)/build/libs
endif
# directory
DIRS = $(DIR_OBJS) $(DIR_DEPS) $(DIR_EXES) $(DIR_LIBS)
# include directory
ifneq ($(DIR_INCS),"")
DIR_INCS := $(strip $(DIR_INCS))
DIR_INCS := $(addprefix -I,$(DIR_INCS))
endif
# when build execute file
ifneq ($(EXES),)
EXES := $(addprefix $(DIR_EXES)/,$(EXES))
RMS += $(EXES)
DIR_LIBS := $(strip $(DIR_LIBS))
DIR_LIBS := $(addprefix -L,$(DIR_LIBS))
endif
# when build static libaray file
ifneq ($(LIBS),"")
LIBS := $(addprefix $(DIR_LIBS)/,$(LIBS))
RMS += $(LIBS)
endif
# default source code file directory
ifeq ($(DIR_SRCS),)
DIR_SRCS = .
endif
SRCS = $(wildcard $(DIR_SRCS)/*.c)
OBJS = $(patsubst %.c, %.o,$(notdir $(SRCS)))
OBJS := $(addprefix $(DIR_OBJS)/,$(OBJS))
RMS += $(OBJS) $(DIR_OBJS)
DEPS = $(patsubst %.c, %.dep,$(notdir $(SRCS)))
DEPS := $(addprefix $(DIR_DEPS)/,$(DEPS))
RMS += $(DEPS) $(DIR_DEPS)
ifneq ($(EXES),"")
all : $(EXES)
endif
ifneq ($(LIBS),"")
all : $(LIBS)
endif
ifneq ($(LINK_LIBS),"")
LINK_LIBS := $(strip $(LINK_LIBS))
LINK_LIBS := $(addprefix -l,$(LINK_LIBS))
endif
# include dependent files
ifneq ($(MAKECMDGOALS), clean)
-include $(DEPS)
endif
$(DIRS):
mkdir -p $@
# creat execute file
$(EXES) : $(DIR_OBJS) $(OBJS) $(DIR_EXES)
$(CC) $(DIR_INCS) $(CFLAGES) -o $@ $(OBJS) $(DIR_LIBS) $(LINK_LIBS)
# creat libaray file
$(LIBS) : $(DIR_LIBS) $(DIR_OBJS) $(OBJS)
# library type is static
ifeq ($(LIB_TYPE),static)
$(AR) $(ARFLAGS) $@ $(OBJS)
endif
# library type is dynamic
ifeq ($(LIB_TYPE),dynamic)
$(CC) -shared -o $@ $(OBJS)
endif
# creat object file
$(DIR_OBJS)/%.o : $(DIR_SRCS)/%.c
@echo "source files:" $<
@echo "object files:" $@
ifeq ($(LIB_TYPE),static)
$(CC) $(DIR_INCS) $(CFLAGES) -o $@ -c $<
else
$(CC) $(DIR_INCS) $(CFLAGES) -fPIC -o $@ -c $<
endif
# creat depandant file
$(DIR_DEPS)/%.dep : $(DIR_SRCS)/%.c $(DIR_DEPS)
@echo "creating depend file ..." $@
@set -e;\
$(CC) $(DIR_INCS) -MM $< > [email protected];\
sed 's,\($*\)\.o[ :]*,$(DIR_OBJS)/\1.o $@ : ,g' < [email protected] > $@
# rm [email protected]
clean:
rm -rf $(RMS)
这个makefile 没有什么好分析的和第二篇文章单目录工程makefile一样的
好了进入module1 下我们想把 module1编译成libmodule1.a 对应的Makefile 就很简单啦
# module1 Makefile
DIR_CUR = $(shell pwd)
DIR_SRCS = $(DIR_CUR)/src
DIR_INCS += $(DIR_CUR)/inc
DIR_OBJS = objs
DIR_DEPS = deps
DIR_LIBS = ../../build/libs/static
# library type an name
LIB_TYPE = static
LIBS = libmodule1.a
# LIB_TYPE = dynamic
# LIBS = libmodule1.so
include ../../rules.mk
这个makefile就是要配置输出文件路径和名字然后将顶层rules.mk包含就ok啦,
同样的module2 目录下Makefile 也是一样的只要将LIBS 改为 libmodule2.a即可
module3 的我们想把他编译成动态库 则对应为如下
# module1 Makefile
DIR_CUR = $(shell pwd)
DIR_SRCS = $(DIR_CUR)/src
DIR_INCS += $(DIR_CUR)/inc
DIR_OBJS = objs
DIR_DEPS = deps
DIR_LIBS = ../../build/libs/static
# library type an name
#LIB_TYPE = static
#LIBS = libmodule3.a
LIB_TYPE = dynamic
LIBS = libmodule3.so
include ../../rules.mk
# Current directory Makefile
CURDIR = $(shell pwd)
# executable file name
EXES = justest
# head file directories
DIR_INCS = module1/inc\
module2/inc\
module3/inc
# objs and dependant file directories
DIR_OBJS = objs
DIR_DEPS = deps
# static libraries
LINK_LIBS = module1 module2 module3
# library file directories
DIR_LIBS = $(TOPDIR)/build/libs/static\
$(TOPDIR)/build/libs/dynamic
# include common rule makefile
include ../rules.mk
在这个makefile 只要配置生成的可执行文件的路径名,还有头文件目录,链接库的路径和名字就可以生成可执行文件了,这里也顶层的rules.mk
当然在实际应用中可以到各个模块中去执行make 但当想一次性生成可执行文件,则还需要一个顶层Makefile
.PHONY: all clean
# sofeware version information
VERSION = 1
PROJECT = mk_demo
SUBLEVEL = 4
YEAL = 2013
RELEASE_VERSION = $(PROJECT).$(YEAL).$(VERSION).$(SUBLEVEL)
BUILD_DIRS = $(TOPDIR)/source/module1\
$(TOPDIR)/source/module2\
$(TOPDIR)/source/module3\
$(TOPDIR)/source
all :
# @shell(export TOPDIR=`pwd`)
@echo "version information:" $(RELEASE_VERSION)
@echo "building ..."
@set -e;\
for dir in $(BUILD_DIRS);\
do\
cd $$dir && $(MAKE);\
done
@echo ""
@echo "\33[35m ~@^_^@~ \33[m"
@echo ""
@echo "Build Completed"
clean:
@echo "cleaning ...."
for dir in $(BUILD_DIRS);\
do\
cd $$dir && $(MAKE) clean;\
done
rm -rf $(RMS)
@echo ""
@echo "Clean Completed"
version:
@echo $(TOPDIR)
@echo $(RELEASE_VERSION)
@echo $(BUILD_DIRS)
@echo $(RMS)
这里最关键的几句代码就是
for dir in $(BUILD_DIRS)
do \
cd $$dir && $(MAKE);\
done
在这里将进入各个模块目录下 执行make ,到这里一个稍微可以用工程makefile 工程实例,实现完了,其实只要模块划分好,还是很好理解的。我想说的时这种方法很常用,但不是唯一的,我有见过有些工程中也许并不需要每个模块都编译成库,或者说他要编译成库本身就是个很大的工程,一个模块的文件就特别多,目录结果就比较复杂这中该怎么写,先讲个思路,对应模块的Makefile 就搜索该目录下所以的Makefile 并包含进来。而子目录下的Makefile 将只将目录下需要编译的文件包含和路径包含经来就ok了,有时间也要总结下。
至此非常感谢我参考的《驾驭Makefile》,《Makefile编程》,《GCC 中文手册》