博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Makefile Demo案例
阅读量:7216 次
发布时间:2019-06-29

本文共 6702 字,大约阅读时间需要 22 分钟。

# Comments can be written like this.# File should be named Makefile and then can be run as `make 
`.# Otherwise we use `make -f "filename"
`.# Warning - only use TABS to indent in Makefiles, never spaces!#-----------------------------------------------------------------------# Basics#-----------------------------------------------------------------------# Rules are of the format# target:
# where prerequisites are optional.# A rule - this rule will only run if file0.txt doesn't exist.file0.txt: echo "foo" > file0.txt # Even comments in these 'recipe' sections get passed to the shell. # Try `make file0.txt` or simply `make` - first rule is the default.# This rule will only run if file0.txt is newer than file1.txt.file1.txt: file0.txt cat file0.txt > file1.txt # use the same quoting rules as in the shell. @cat file0.txt >> file1.txt # @ stops the command from being echoed to stdout. -@echo 'hello' # - means that make will keep going in the case of an error. # Try `make file1.txt` on the commandline.# A rule can have multiple targets and multiple prerequisitesfile2.txt file3.txt: file0.txt file1.txt touch file2.txt touch file3.txt# Make will complain about multiple recipes for the same rule. Empty# recipes don't count though and can be used to add new dependencies.#-----------------------------------------------------------------------# Phony Targets#-----------------------------------------------------------------------# A phony target. Any target that isn't a file.# It will never be up to date so make will always try to run it.all: maker process# We can declare things out of order.maker: touch ex0.txt ex1.txt# Can avoid phony rules breaking when a real file has the same name by.PHONY: all maker process# This is a special target. There are several others.# A rule with a dependency on a phony target will always runex0.txt ex1.txt: maker# Common phony targets are: all make clean install ...#-----------------------------------------------------------------------# Automatic Variables & Wildcards#-----------------------------------------------------------------------process: file*.txt #using a wildcard to match filenames @echo $^ # $^ is a variable containing the list of prerequisites @echo $@ # prints the target name #(for multiple target rules, $@ is whichever caused the rule to run) @echo $< # the first prerequisite listed @echo $? # only the dependencies that are out of date @echo $+ # all dependencies including duplicates (unlike normal) #@echo $| # all of the 'order only' prerequisites# Even if we split up the rule dependency definitions, $^ will find themprocess: ex1.txt file0.txt# ex1.txt will be found but file0.txt will be deduplicated.#-----------------------------------------------------------------------# Patterns#-----------------------------------------------------------------------# Can teach make how to convert certain files into other files.%.png: %.svg inkscape --export-png $^# Pattern rules will only do anything if make decides to create the# target.# Directory paths are normally ignored when matching pattern rules. But# make will try to use the most appropriate rule available.small/%.png: %.svg inkscape --export-png --export-dpi 30 $^# make will use the last version for a pattern rule that it finds.%.png: %.svg @echo this rule is chosen# However make will use the first pattern rule that can make the target%.png: %.ps @echo this rule is not chosen if *.svg and *.ps are both present# make already has some pattern rules built-in. For instance, it knows# how to turn *.c files into *.o files.# Older makefiles might use suffix rules instead of pattern rules.png.ps: @echo this rule is similar to a pattern rule.# Tell make about the suffix rule.SUFFIXES: .png#-----------------------------------------------------------------------# Variables#-----------------------------------------------------------------------# aka. macros# Variables are basically all string typesname = Tedname2="Sarah"echo: @echo $(name) @echo ${name2} @echo $name # This won't work, treated as $(n)ame. @echo $(name3) # Unknown variables are treated as empty strings.# There are 4 places to set variables.# In order of priority from highest to lowest:# 1: commandline arguments# 2: Makefile# 3: shell environment variables - make imports these automatically.# 4: make has some predefined variablesname4 ?= Jean# Only set the variable if environment variable is not already defined.override name5 = David# Stops commandline arguments from changing this variable.name4 +=grey# Append values to variable (includes a space).# Pattern-specific variable values (GNU extension).echo: name2 = Sara # True within the matching rule # and also within its remade recursive dependencies # (except it can break when your graph gets too complicated!)# Some variables defined automatically by make.echo_inbuilt: echo $(CC) echo ${CXX)} echo $(FC) echo ${CFLAGS)} echo $(CPPFLAGS) echo ${CXXFLAGS} echo $(LDFLAGS) echo ${LDLIBS}#-----------------------------------------------------------------------# Variables 2#-----------------------------------------------------------------------# The first type of variables are evaluated each time they are used.# This can be expensive, so a second type of variable exists which is# only evaluated once. (This is a GNU make extension)var := hellovar2 ::= $(var) hello#:= and ::= are equivalent.# These variables are evaluated procedurally (in the order that they# appear), thus breaking with the rest of the language !# This doesn't workvar3 ::= $(var4) and good luckvar4 ::= good night#-----------------------------------------------------------------------# Functions#-----------------------------------------------------------------------# make has lots of functions available.sourcefiles = $(wildcard *.c */*.c)objectfiles = $(patsubst %.c,%.o,$(sourcefiles))# Format is $(func arg0,arg1,arg2...)# Some examplesls: * src/* @echo $(filter %.txt, $^) @echo $(notdir $^) @echo $(join $(dir $^),$(notdir $^))#-----------------------------------------------------------------------# Directives#-----------------------------------------------------------------------# Include other makefiles, useful for platform specific codeinclude foo.mksport = tennis# Conditional compilationreport:ifeq ($(sport),tennis) @echo 'game, set, match'else @echo "They think it's all over; it is now"endif# There are also ifneq, ifdef, ifndeffoo = trueifdef $(foo)bar = 'hello'endif

 

转载于:https://www.cnblogs.com/onelikeone/p/8439026.html

你可能感兴趣的文章
Hadoop快速入门
查看>>
Problem S
查看>>
SVN上传的时候没法显示文件名,只显示后缀名
查看>>
Python:pygame游戏编程之旅四(游戏界面文字处理)
查看>>
fedroa 编译安装mysql5.5
查看>>
WC2018游记
查看>>
毕设开发日志2017-10-23
查看>>
***微信公众平台开发: 获取用户基本信息+OAuth2.0网页授权
查看>>
第二章 例题2-2 在屏幕上显示两个短句
查看>>
【转】iOS学习之适配iOS10
查看>>
OC语言BLOCK和协议
查看>>
C++创建一个动态链接库工程
查看>>
(六)maven之本地仓库
查看>>
如何使用 SPICE client (virt-viewer) 来连接远程虚拟机桌面?
查看>>
CentOS7
查看>>
linux高编IO-------tmpnam和tmpfile临时文件
查看>>
微信的机器人开发
查看>>
从零开始学Java(二)基础概念——什么是"面向对象编程"?
查看>>
近期面试总结(2016.10)
查看>>
CodeForces 525D Arthur and Walls :只包含点和星的矩阵,需要将部分星变成点使满足点组成矩形 : dfs+思维...
查看>>