第一個makefile文件,這麼簡單,爲什麼也搞了那麼長時間!!
main.cpp:
#include<iostream>
#include"first.h"
using namespace std;
int main()
{
first ff(3,2);
int b=ff.dis();
cout<<b<<endl;
//print("hahahah");
return 0;
}
first.cpp:
#include"first.h"
int first::dis()
{
return a*a + b*b;
}
first.h:
#ifndef FIRST_
#define FIRST_
class first
{
private:
int a;
int b;
public:
first(int x=0,int y=0){a=x;b=y;}
~first(){}
int dis();
};
#endif
makefile:
main:main.o first.o ; g++ -o main main.o first.o
main.o:main.cpp first.h ; cc -c main.cpp
first.o :first.cpp first.h; cc -c first.cpp
clean: rm main main.o first.o
也可以自己定製庫文件:
先把print.cpp編譯爲print.o
print.cpp:
#include<stdio.h>
void print(char *a)
{
printf("%s/n",a);
}
然後就可以直接在makefile中main:中添加print.o進行 鏈接就可
或者:用ar crv libprint.a print.o
然後就可以用libprint.a靜態庫來使用了
在main.cpp中要包含print.h頭文件
print.h://函數聲明
#ifndef PRINT_
#define PRINT_
void print(char *);
#endif