linux build文件,创建linux make / build文件

小编典典

什么是 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

#include "rawr.h"

#include "simple_ls.h"

#include "2dquicksort.h"

#include // Boost!

int main(int argc, char **argv)

{

boost::array a = { { 1, 2, 3, 4} };

std::cout << a[1] << std::endl;

return 0;

}

生成文件

如果您Makefile从stackoverflow 复制源,请不要忘记用真实的Tab代替空格:

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

没有理由不使用它!

2020-06-03

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值