linux-makefile-炼之路

序:

     我个人对于makefile文件的编写目前只是初出茅庐,通过学习网上相关资料的介绍,了解到makefile可以实现工程的自动化编译,只需要一个make命令,整个项目就会全部自动编译,生成目标文件,就不需要一一进行编译了,这将会大大提高项目的开发进度。So as a lazy man, I decide to learn and get it. And below is my makefile learning road. Let's enjoy this~

Start:

1.流程速览


2.makefile 规则构成

target: dependencies

[tab] system command

target是一个目标文件,可以是Object File,也可以是执行文件。还可以是一个标签(Label),后面的冒号紧跟其后;

dependencies就是要生成那个target所需要的文件或是目标;

command也就是make需要执行的命令,前面一定用tab键,否则执行会报错。

Here's an example:

(From:http://blog.csdn.net/wallwind/article/details/6791505)

all: hello  
02.  
03.hello: main.o factorial.o hello.o  
04.    g++ main.o factorial.o hello.o -o hello  
05.  
06.main.o: main.cpp  
07.    g++ -c main.cpp  
08.  
09.factorial.o: factorial.cpp  
10.    g++ -c factorial.cpp  
11.  
12.hello.o: hello.cpp  
13.    g++ -c hello.cpp  
14.  
15.clean:  
16.    rm -rf *o hello  

        如上example中,hello为要生成的target file,就是你要执行的文件,类似Windows里的name.exe文件.其他main.o, factorial.o, hello.o是生成hello文件所需要的目标文件,第4行命令则为生成hello的执行命令,在往下为生成各目标文件的对应依赖关系和生成命令,最后是clean命令,其作用是你最后执行make clean命令时的执行规则,即删除中间生成的目标文件和安装文件。

下面是如上example的依赖文件代码:

main.cpp
#include<iostream>
#include"functions.h"
using namespace std;
int main()
{
  print_hello();
cout << endl;
cout << "The factorial of 5 is " << factorial(5) << endl;
return 0;
}

hello.cpp
#include<iostream>
#include"functions.h"
using namespace std;

void print_hello()
{
  cout << "hello world!";
}

factorial.cpp
#include<iostream>
#include"function.h"
using namespace std;
int factorial(int n)
{
  if(n != 1)
    {return(n * factorial( n-1 ));}
  else 
    return 1;
}
<pre class="plain" name="code">functions.h
{
void print_hello();
int factorial(int n);
}


编写好如上文件和上面example中的makefile文件,运行make命令可生成hello可执行文件,运行./hello ,即可输出打印信息。

需要深入学习makefile的编写,请参考:http://blog.csdn.net/zhiboyongjian/article/details/46534667


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值