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類的構造函數和析構函數。