什么是 Makefile ? (应用于Boost 项目)
Makefile 背后的根递归思想是:
要构建目标,我们需要先决条件(其他目标!)和说明来构建
先决条件
它们要么是文件、文件夹要么是虚假目标(通常在.PHONY)。测试文件/文件夹的存在和修改日期。
如果目标没有先决条件或比任何先决条件更旧,则需要重建目标。
说明
指令是 shell 命令,从一个选项卡开始。每个指令行是一个 shell 实例。当当前命令以反斜杠 \ 结尾时,shell 命令可以在下一行继续。
目标定义
目标是依赖项或规则。
依赖:
target : prerequisite1 prerequisite2 prerequisiteN
规则:
target : prerequisite1 prerequisite2 prerequisiteN
instructions1
@hidden_batch1 ; \
hidden_batch2
指令开始前有制表符。
调试
调试 Makefile 可能会让人头疼。
在您的 Makefile 中尝试以下操作以显示跟踪(warning 的文件和行位置):
$(info Shell: $(SHELL))
$(warning CXX: $(CXX))
当您的 Makefile 包含大量嵌套的 if/else/endif 并且您不确定时,这很有帮助
现在的路径是什么。
Makefile 结构
理想的makefile结构是:
- 变量设置
- 目标/依赖声明
真正的目标指令处理开始于整个 Makefile 及其包含文件
已被理解(存储在make内部数据库中)。
示例
最后,将理论应用到这个使用 Boost 的特定示例并创建假源文件
来说明。
rawr.cpp
#include "rawr.h"
simple_ls.cpp
#include "rawr.h"
converter.cpp
#include <iostream>
#include "rawr.h"
#include "simple_ls.h"
#include "2dquicksort.h"
#include <boost/array.hpp> // Boost!
int main(int argc, char **argv)
{
boost::array<int,4> a = { { 1, 2, 3, 4} };
std::cout << a[1] << std::endl;
return 0;
}
生成文件
如果您从 *stack***overflow** 复制 Makefile 源代码,请不要忘记用真正的制表符替换空格:
sed -i~ -e 's/^ /\t/' Makefile
Makefile 来源:
## Makefile for C++ project using Boost
#
# @author Cedric "levif" Le Dillau
#
# Some notes:
# - Using ':=' instead of '=' assign the value at Makefile parsing time,
# others are evaluated at usage time. This discards
# - Use ':set list' in Vi/Vim to show tabs (Ctrl-v-i force tab insertion)
#
# List to '.PHONY' all fake targets, those that are neither files nor folders.
# "all" and "clean" are good candidates.
.PHONY: all, clean
# Define the final program name
PROGNAME := converter
# Pre-processor flags to be used for includes (-I) and defines (-D)
CPPFLAGS := -DUSE_BOOST
# CFLAGS is used for C compilation options.
CFLAGS := -Wall -O0
# CXXFLAGS is used for C++ compilation options.
CXXFLAGS += -Wall -O0
# LDFLAGS is used for linker (-g enables debug symbols)
LDFLAGS += -g
# Which Boost modules to use (all)
BOOST_MODULES = \
date_time \
filesystem \
graph \
iostreams \
math_c99 \
system \
serialization \
regex
# Boost libraries' type (a suffix)
BOOST_MODULES_TYPE := -mt
# Define library names with their type
BOOST_MODULES_LIBS := $(addsuffix $(BOOT_MODULES_TYPE),$(BOOST_MODULES))
# Define the linker argument to use the Boost libraries.
BOOST_LDFLAGS := $(addprefix -lboost_,$(BOOST_MODULES_LIBS))
# Feed compiler/linker flags with Boost's
CPPFLAGS += $(BOOST_CPPFLAGS)
LDFLAGS += $(BOOST_LDFLAGS)
# List the project' sources to compile or let the Makefile recognize
# them for you using 'wildcard' function.
#
#SOURCES = simple_ls.cpp rawr.cpp converter.cpp
SOURCES = $(wildcard *.cpp)
# List the project' headers or let the Makefile recognize
# them for you using 'wildcard' function.
#
#HEADERS = simple_ls.h 2dquicksort.h rawr.h
HEADERS = $(wildcard %.h)
# Construct the list of object files based on source files using
# simple extension substitution.
OBJECTS = $(SOURCES:%.cpp=%.o)
#
# Now declare the dependencies rules and targets
#
# Starting with 'all' make it becomes the default target when none
# is specified on 'make' command line.
all : $(PROGNAME)
# Declare that the final program depends on all objects and the Makfile
$(PROGNAME) : $(OBJECTS) Makefile
$(CXX) -o $@ $(LDFLAGS) $(OBJECTS)
# Now the choice of using implicit rules or not (my choice)...
#
# Choice 1: use implicit rules and then we only need to add some dependencies
# to each object.
#
## Tells make that each object file depends on all headers and this Makefile.
#$(OBJECTS) : $(HEADERS) Makefile
#
# Choice 2: don't use implicit rules and specify our will
%.o: %.cpp $(HEADERS) Makefile
$(CXX) $(CXXFLAGS) $(CPPFLAGS) -c $(OUTPUT_OPTION) $<
# Simple clean-up target
# notes:
# - the '@' before 'echo' informs make to hide command invocation.
# - the '-' before 'rm' command to informs make to ignore errors.
clean :
@echo "Clean."
-rm -f *.o $(PROGNAME)
文件列表
2dquicksort.h
converter.cpp
Makefile
rawr.cpp
rawr.h
simple_ls.cpp
simple_ls.h
编译
make clean all
Clean.
rm -f *.o converter
g++ -Wall -O0 -DUSE_BOOST -c -o converter.o converter.cpp
g++ -Wall -O0 -DUSE_BOOST -c -o rawr.o rawr.cpp
g++ -Wall -O0 -DUSE_BOOST -c -o simple_ls.o simple_ls.cpp
g++ -o converter -g -lboost_date_time -lboost_filesystem -lboost_graph -lboost_iostreams -lboost_math_c99 -lboost_system -lboost_serialization -lboost_regex converter.o rawr.o simple_ls.o
结果
现在,几乎是最小的 Boost 程序的结果:
./converter
2
没有理由不使用它! Boost 确实是一个特色 C++ 工具箱 :)