首先基于上篇文章makefile 的初认识,我们继续学习makefile 新东西(hello.cpp test.h文件和上文类同不在细谈)着重学习makefile;
mkdir -p test cd test mkdir function touch hello.cpp vi hello.cpp mkdir source touch test.h vi test.h cd .. touch makefile vi makefile同样建立操做和三个文件
#include<iostream> using namespace std; void print() {cout<<"i am a student"<<endl;}test.h文件
#include"../function/test.h" #include<iostream> using namespace std; int main(int argc,char* argv[]) {cout<<"hello world"<<endl; print(); return 0; }hello.cpp文件
VPATH=./source CC=g++ #vpath %.cpp source #vpath %.h function object=hello.o hello:$(object) @ echo 正在编译文件 $< $(CC) -o $@ -g $^ $(object):hello.cpp @ echo 正在编译文件 \ $< $(CC) -o \ $@ \ -c $^ exec: cd /home/ubuntu/test;pwd #ubuntu为本机用户名 clean: -rm hello $(object)上面是新的makefile文件
这里简单说一下, < 代 表 的 是 一 个 依 赖 的 文 件 ; <代表的是一个依赖的文件; <代表的是一个依赖的文件;^代表所有的依赖的文件,更多详细的说明后面到了大型makefile 会在谈论;所以这里也可以使用$^代替源文件
@ echo 信息 代表打印输出的信息,同时若一行语句写不完,可以使用\换行输入,命令和打印信息都是可以的;
当执行make exec 的时候,make 或解析到/home/ubuntu/test目录下执行一些特殊的操作 这里只是使用了pwd命令 打印出路径,当然用户可以使用其他命令完成自己特定操作;