天天看点

linux-c++-makefile的使用方法

1 c++编程中一般把文件分为三种,xx.h、xx.cpp、 main.cpp;linux环境下我们可以使用make工具进行编译,生成可执行文件之后便可以使用了。这里写一个helloworld,分享      一下.

hello.h文件内容如下:

#ifndef hell_h

#define hell_h

class HelloWorld()

{

public :

HelloWorld();

~HelloWorld();

};

#endif

helloworld.cpp文件内容如下:

#include<iostream>

#include "hello.h"

using namespace std;

HelloWorld::HelloWorld()

{

cout<<"HelloWorld constr"

}

HelloWorld::~HelloWorld()

{

cout<<"析构"<<endl;

}

main.cpp文件内容如下:

#include<iostream>

#include "hello.h"

int main()

{

HelloWorld h;

return 0;

}

下面是 makefile文件的内容,这也是最重要的。

dest:helloworld.o main.o

g++ -c helloworld.o main.o -o dest

helloworld.o helloworld.cpp hello.h

g++ -c helloworld.cpp -o helloworld.o

main.o:main.cpp hello.h

g++ -c main.cpp main.o

然后在linux终端输入命令make makefile这可以得到dest文件;

然后执行./dest显示打印两行数据,分别来自HelloWorld类的构造函数和析构函数。

继续阅读