三个文件,一个头文件,两个程序文件
/*
d.h
*/
#include <iostream>
using namespace std;
class Dataset
{
public:
int getdata();
};
d.h
*/
#include <iostream>
using namespace std;
class Dataset
{
public:
int getdata();
};
/*
d.cpp
*/
#include "d.h"
int Dataset::getdata()
{
return 1231;
}
d.cpp
*/
#include "d.h"
int Dataset::getdata()
{
return 1231;
}
/*
out.cpp
*/
#include <iostream>
#include "d.h"
using namespace std;
int main()
{
Dataset Ds;
cout<<Ds.getdata()<<endl;
return 1;
}
out.cpp
*/
#include <iostream>
#include "d.h"
using namespace std;
int main()
{
Dataset Ds;
cout<<Ds.getdata()<<endl;
return 1;
}
编译:
[root@localhost code]# g++ -o test.o d.cpp out.cpp
[root@localhost code]# ./test.o
1231
[root@localhost code]#
编译成动态库
[root@localhost code]# g++ -o d.o -c d.cpp
[root@localhost code]# ar -r d.a d.o
ar: 正在创建 d.a
[root@localhost code]# g++ -o out out.cpp ./d.a
[root@localhost code]# ./out
1231
[root@localhost code]#
[root@localhost code]# g++ -o test.o d.cpp out.cpp
[root@localhost code]# ./test.o
1231
[root@localhost code]#
编译成动态库
[root@localhost code]# g++ -o d.o -c d.cpp
[root@localhost code]# ar -r d.a d.o
ar: 正在创建 d.a
[root@localhost code]# g++ -o out out.cpp ./d.a
[root@localhost code]# ./out
1231
[root@localhost code]#