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

一、须要用到的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的具体编写请百度,谷歌。此文档仅仅适用入门。

原文地址:https://www.cnblogs.com/yfceshi/p/6961434.html