Makfile的写法笔记

Makefile的基本组成部分:

 

1、dependency  依赖关系。

2、rule 规则

3、Marco 宏定义

--------------------------------

 

dependency的写法如下:

 

target: file1.o file2.o

[target是目标文件, file1.o,file2.o等是目标文件所依赖的文件]

 

如果target后面所依赖的文件为空(冒号后什么也不写),表示该target总是需要重新生成。

即这个dependency下的rule无条件地被执行。

 

特殊的target 以及含义:

 

1、all,这个all所依赖的文件指明了最终的文件。如:

 

 all: myApp myApp.1

 

 

2、clean,这个目标表示我们执行 make clean时应当执行的动作,典型写法:

 

 clean: -rm *.o

 

 

3、install,它表示我们执行 make install时,应当执行的动作。典型写法:

 

INSTDIR = /usr/local/bin install: myApp @if [ -d $(INSTDIR) ]; / then / cp myApp $(INSTDIR) / chmod a+x $(INSTDIR)/myApp;/ chmod og-w $(INSTDIR)/myApp;/ echo "Successfully installed";/ else / echo "failed to install";/ fi

这里的install所依赖的文件就是myApp,即工程最后生成的程序文件。 

rule的写法为:

 

<table> command

 

[即一个跳格键,然后写shell命令]

 

----------------------------------

一个或多个 rule 必须跟在一个dependcy的下面(当target所依赖的文件被修改后,就会执行这个dependency下的rules)如:

 

MyApp: main.o func1.o func2.o gcc -o MyApp main.o func1.o func2.o rm *.o

 

 

-----------------------------------

注释的方法:

 

用#号,注释的范文是从#开始起,直到本行的末尾

 

-------------------------------------

Marco的使用

 

make命令对宏的处理方式与C语言中的 #define 一样,都是把字符串展开。

 

定义:

CFLAGS = -g -Wall -ansi

 

使用:

 

 $(CFLAGS) 或 ${CFLAGS}

 

make命令中预定义的宏(特殊的宏)如下:

 

$?       List of prerequisites (files the target depends on) changed more recently
than the current target (谁帮忙翻译下这句话)

 

$@     当前的目标文件

 

$<      当前的依赖文件

 

$<      当前的依赖文件,无前缀

 

------------------------------

其他内容:

 

在rule的命令行前面加个负号(-),可以忽略本行命令的错误。

在rule的命令行前面加 @,表示执行完这行命令之后才打印出这行命令

 

 

 

 

 

你可能感兴趣的:(Makfile的写法笔记)