上一期: 《简单的、智能搜索源文件的 Makefile》
下载代码:http://download.csdn.net/detail/wxqee/4377256
包括《简单的、智能搜索源文件的 Makefile》和本期《简单的、智能搜索源文件的 Makefile(随意扩展bin或lib)》的代码。
过了一天,过来写一个模版,其预期的场景是这样的:
同一个工程下,需要编译一个名为AppDemo的可执行文件,它依赖本工程下的另外一个叫helloworld的动态库。
另外,AppDemo和helloworld库的源码分别保存在各自的目录中。
这个时候应该怎么做呢,看看模版的布局:
xiwang@ubuntu:~/Dev/AppDemo$ tree
.|-- App <-- 存放AppDemo源码的目录| |-- Makefile| `-- main.cpp|-- HelloLib <-- 存放helloworld库源码的目录| |-- HelloWorld.cpp| |-- HelloWorld.h| `-- Makefile|-- Makefile|-- build_bin.mk`-- build_lib.mk2 directories, 8 files
其中根目录下的 Makefile 是控制编译哪些子项目的, build_bin.mk, build_lib.mk 是编译bin或lib的模版,这样在子项目中就可以简单的设定一些NAME、CXXFLAGS等就可以编译当前子项目的源文件了。当然,子项目之间的依赖(谁编译谁,后编译谁)是在根目录里表现的,没有对此优化。
# Makefile, 2012-06-15 T1738 .PHONY: all clean all: make -C HelloLib make -C App clean: make clean -C HelloLib make clean -C App
# build_bin.mk, 2012-06-16 T1928 # Folders SRC_DIR = . BIN = $(NAME) OBJS = $(patsubst %.cpp,%.o,$(wildcard $(SRC_DIR)/*.cpp)) .PHONY: all clean all: $(BIN) $(BIN): $(OBJS) $(CXX) $(CPPFLAGS) $(LDFLAGS) $^ -o $@ $(LIBS) %.o: %.cpp $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c $^ -o $@ clean: $(RM) $(OBJS) $(BIN)
# Makefile, 2012-06-16 T1932 # # This's a demo makefile using model build_bin.mk to build app, with source # source code in this folder. # # Require ../build_bin.mk NAME = AppDemo VERSION = 1.0.0 RELEASE = 01 # Flags CXXFLAGS = -Wall CXXFLAGS += -I. -I$(HELLODIR) CPPFLAGS = -D_DEBUG LDFLAGS = -L. -L$(HELLODIR) #<! set folder of helloworld library LIBS = -lhelloworld #<! load library helloworld # Folders HELLODIR = ../HelloLib # END ---------------------------------- include ../build_bin.mk
# build_lib.mk, 2012-06-16 T1928 # Folders SRC_DIR = . BIN = lib$(NAME).so OBJS = $(patsubst %.cpp,%.o,$(wildcard $(SRC_DIR)/*.cpp)) CXXFLAGS += -fPIC .PHONY: all clean all: $(BIN) $(BIN): $(OBJS) $(CXX) $(CPPFLAGS) $(LDFLAGS) $^ -o $@ $(LIBS) -shared %.o: %.cpp $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c $^ -o $@ clean: $(RM) $(OBJS) $(BIN)
# Makefile, 2012-06-16 T1932 # # This's a demo makefile using model build_bin.mk to build app, with source # source code in this folder. # # Require ../build_bin.mk NAME = AppDemo VERSION = 1.0.0 RELEASE = 01 # Flags CXXFLAGS = -Wall CXXFLAGS += -I. -I$(HELLODIR) CPPFLAGS = -D_DEBUG LDFLAGS = -L. -L$(HELLODIR) #<! set folder of helloworld library LIBS = -lhelloworld #<! load library helloworld # Folders HELLODIR = ../HelloLib # END ---------------------------------- include ../build_bin.mk