简单编写makefile文件,实现GCC4.9编译项目,加入boost库测试等等。。

时间:2014-05-12 15:34:53   收藏:0   阅读:319

一、需要用到的hw.cpp hw.h funtest.cpp funtest.h makefile 几个测试文件

1、hw.cpp代码如下:

#include "hw.h"
#include "funtest.h"
using namespace std;
using namespace boost;
int main()
{
  timer t; 
   {
     int i=1;
   }
   auto i="abc";
   cout<<i<<endl;
   cout<<"endl"<<endl;
   cout<<"abcdefj"<<endl;
   cout << "最大处理时间:" << t.elapsed_max() / 3600 << " h" << endl;  
   cout << "最小处理时间:" << t.elapsed_min() << " s" << endl;  
   cout << "逝去时间:" << t.elapsed() << " s" << endl;  
   cout<<"每行需要一个tab键"<<endl;
   funtest::testa test1;
   test1.testafun();
}

2、hw.h代码如下:

#ifndef __HW_H__
#define __HW_H__
#include <iostream>
#include <boost/timer.hpp>
#include <boost/progress.hpp>

#endif


3、funtest.cpp代码如下:

#include "funtest.h"

using namespace std;

namespace funtest
{
testa::testa()
{
  cout<<"testa()"<<endl;
}

testa::~testa()
{
  cout<<"~testa()"<<endl;
}

void testa::testafun()
{
   cout<<"testa::testafun()"<<endl;
}
}


4、funtest.h代码如下:

#ifndef __FUNTEST__H__
#define __FUNTEST__H__
#include <iostream>
namespace funtest
{
    class testa
        {
          public:
          testa();
          ~testa();
          void testafun();  
        };
}

#endif


二、makefile的编写以及使用示例

1、makefile代码如下:

#----------------------------------------------------------
#makefile helloworld测试用例
#
#
#
#
#-----------------------------------------------------------
ggg=g++49
exe=helloworld

#所有的.o文件写在这里
obj = hw.o funtest.o

#所要关联的cpp文件写在这里
cpp = hw.cpp funtest.cpp

$(exe):$(obj)
        @echo "链接开始................"
        $(ggg) -o $(exe) $(obj)


hw.o : $(cpp)
        @echo "编译开始................"
        $(ggg) -std=c++11 -c $(cpp)



.PHONY : clean delete
all:
        @echo "开始make all..........."

clean:
        @echo "开始清理................"
        -rm -rf $(obj) $(exe)
delete:
        @echo "delete.................."
        pwd


2、使用方法linux简单示例。

[mythcpp@localhost src]$ make clean
开始清理................
rm -rf hw.o funtest.o helloworld
[mythcpp@localhost src]$ make
编译开始................
g++49 -std=c++11 -c hw.cpp funtest.cpp
链接开始................
g++49 -o helloworld hw.o funtest.o
[mythcpp@localhost src]$ make all
开始make all...........
[mythcpp@localhost src]$ make delete
delete..................
pwd
/home/mythcpp/src

3、程序输出示例:
[mythcpp@localhost src]$ ./helloworld
abc
endl
abcdefj
最大处理时间:2.56205e+09 h
最小处理时间:1e-06 s
逝去时间:0 s
每行需要一个tab键
testa()
testa::testafun()
~testa()
[mythcpp@localhost src]$


三、需要注意的几点
1、g++49是g++4.9版本的g++
命令行示例:
[mythcpp@localhost src]$ ll /usr/bin/g++49
lrwxrwxrwx. 1 root root 23 May  8 05:05 /usr/bin/g++49 -> /home/gcc-4.9.0/bin/g++
[mythcpp@localhost src]$ type g++49
g++49 is /usr/bin/g++49

2、makefile以tab为间格,不要以空格开始,会报错的。

3、echo 要写在lable下面,如:
$(exe):$(obj)
        @echo "链接开始................"
        $(ggg) -o $(exe) $(obj)
4、makefile文件中的all clean delete 等等伪标签可以自行实现功能命令。
这里最主要是使用make 和make clean两条命令

5、关于makefile的详细编写请百度,谷歌,此文档只适用入门。


简单编写makefile文件,实现GCC4.9编译项目,加入boost库测试等等。。,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!