二、makefile VPTAH vpath $@ $<

来源:互联网 发布:淘宝买的东西下架了 编辑:程序博客网 时间:2024/06/06 03:04

首先基于上篇文章makefile 的初认识,我们继续学习makefile 新东西(hello.cpp test.h文件和上文类同不在细谈)着重学习makefile,大型工程中存在很多文件夹,他们分别代表不同的功能,因此,我们 需要划分文件功能,讨论一下makefile;文件百度云盘(http://pan.baidu.com/s/1geVQJx5)

mkdir -p testcd testmkdir functiontouch hello.cppvi hello.cppmkdir sourcetouch test.hvi test.hcd ..touch makefilevi makefile

上述过程为建立新的文件夹,还是来看一下makefile 里面又出现了什么新知识;

VPATH=./sourceCC=g++#vpath %.cpp source#vpath %.h  functionobject=hello.o hello:$(object)    $(CC)  -o $@ -g $<$(object):hello.cpp     $(CC)  -o $@ -c $< clean:    -rm hello $(object)

上述的makefile文件应该位于test文件夹下;

VPATH=./source#vpath %.cpp source#vpath %.h  function

VPATH 是makefile的系统路径变量 它用来指明,在make解析makefile时候,发现所需要的源文件在source文件夹下,这是需要VPATH 预先指定路径告诉make的,至于test.h 为什么不需要指定所在的文件夹,这是因为,在hello.cpp 头文件已经定义了路径;

#include"../function/test.h"#include<iostream>using namespace std;int main(int argc,char* argv[]){cout<<"hello world"<<endl;print();return 0;}

上述的hello.cpp文件的第一句已经指明头文件所在的文件,所以不用再makefile里面在定义test.h 路径了。

“”# “”在makefile 中代表注释的含义VPATH 和vpath 都可以表达指定路径的作用,但是使用方法不同 ,上面已经讲过VPATH= 路径信息 而 vpath 文件名 文件夹 上面的makefile 也可以写成这样的形式

#VPATH=./sourceCC=g++vpath %.cpp sourceobject=hello.o hello:$(object)    $(CC)  -o $@ -g $<$(object):hello.cpp     $(CC)  -o $@ -c $< clean:    -rm hello $(object)

新的makefile 中,出现了了两个新的符号@S<hello.cpphello.ohello.ohello.cpphellohello.o使@ $< 替代命令中的 源文件夹和目标文件

文中cc 也类似object 变量含义,test.h文件内容不变;

#include<iostream>using namespace std;void print(){cout<<"i am a student"<<endl;}

test.h文件

0 0