c++ makefile

21 篇文章 0 订阅

c++ makefile
      
假设我们有下面这样的一个程序,源代码如下:
********illustrate:**********
there ere three files class.h class.c++ main1.c++
//*********list class.h**********
class tdate
{
private:
int month;
int day;
int year;
public:
//you must define a contructor which didn't include parameters
//when have one or more contructor which have one or more parameters
tdate();
tdate(int t_month, int t_day, int t_year)
{
month=t_month;
day=t_day;
year=t_year;
}
//tdate(const tdate &obj)
//{}
void display();
};
//********list class.c++:************
#include
#include "class.h"
void tdate::display()
{
cout
#include "class.h"//need class tdate
int main()
{
tdate t1(12,30,2007);
tdate t2=tdate(12,31,2007);//impractical but allowed
tdate t3=t2;//invoking the copy contructor
//the method is can not running normal
//tdate t4(t1)
//it can running normal
tdate t4(t2);
t1.display();
t2.display();
t3.display();
t4.display();
return 0;
}
当然由于这个程序很短,我们可以这样来编译:
g++ -c class.c++
g++ -o main1 main1.c++ class.o
这样的话我们也可以产生main程序,而且也不是很麻烦。但是如果我们考虑一下如果有一天我们修改了其中的一个文件(比如说class1.c++)那么我们难道还要重新输入上面的命令?也许你会说,这个很容易解决啊,我写一个 SHELL脚本,让它帮我去完成不就可以了。是的对于这个程序来说,是可以起到作用的。但是当我们把事情想的更复杂一点,如果我们的程序有几百个源程序的时候,难道也要编译器重新一个一个的去编译?因为SHELL脚本将全部编译作任何源文件,包括那些不必要重新编译的源文件,而make工具则可根据目标上一次编译的时间和目标文件所依赖的源文件的更新时间而自动判断应当编译哪个源文件
现在我们来编写这个makefile文件
(文件名为makefile)
**********list makefile*******
# 这是上面那个程序的Makefile文件:
main1:main1.o class.o
    g++ -o main1 main1.o class.o
main1.o: main1.c++ class.h
    g++ -c main1.c++
class.o: class.c++ class.h
    g++ -c class.c++
clean:
    rm *.o
*******it is over**********
现在在shell下输入make
结果如下:
[zhanghong@localhost c++]$ make
g++ -c class.c++
In file included from /usr/include/c++/3.2.2/backward/iostream.h:31,
                 from class.c++:1:
/usr/include/c++/3.2.2/backward/backward_warning.h:32:2: warning: #warning This
file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples
include substituting the  header for the  header for C++ includes, or  instead of the deprecated header . To disable this warning
use -Wno-deprecated.
#g++ -o main1 main1.o class.o
g++ -o main1 main1.o class.o
[zhanghong@localhost c++]$
再输入./main1就可以运行程序
如:[zhanghong@localhost c++]$./main1
有了这个Makefile文件,不论我们什么时候修改了源程序当中的什么文件,我们只要执行make命令,我们的编译器都只会去编译和我们修改的文件有关的文件,其它的文件它连理都不想去理的。
当在shell下输入 make clean时,将调用rm *.o命令,即makefile文件中的最后一个命令
如:[zhanghong@localhost c++]$ make clean
    rm *.o
下面我们学习Makefile是如何编写的。
在Makefile中以#开始的行都是注释行。Makefile中最重要的是描述文件的依赖关系的说明。
一般的格式是:
target:dependency [dependency [........] ]
    command
    command
    [.............]
目标(target):make最终要创建的文件
依赖关系列表(dependency): 通过这张列表可以知道编译目标需要用到的文件
命令列表(command): 为了从指定的依赖关系创建创建出目标文件而需要执行的命令。这些命令不仅是编译命令,还可以shell命令。
第一行表示的是依赖关系。第二行是命令。
比如说我们上面的那个Makefile文件的第一行。
main1:main1.o class.o
表示我们的目标(target)main的依赖对象(dependency)是main1.o class.o 当依赖的对象在目标修改后修改的话,就要去执行规则一行所指定的命令。就象我们的上面那个Makefile第二行所说的一样要执行 g++ -o main1 main1.o class.o
 
注意:命令行是以TAB键开始的
Makefile有三个非常有用的变量。分别是$@,$^,$#' are comments
gx="g++ -c"
main1: main1.o class.o
        #g++ -o main1 main1.o class.o 
        g++ -o $@ $^
main1.o: main1.c++ class.h
        #g++ -c main1.c++
        #g++ -c $


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/45689/showart_491346.html 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值