[转]g++ 编译多个相关文件

三个文件,一个头文件,两个程序文件

/*
  d.h 
*/
#include 
<iostream>
using namespace std; 

class Dataset
{
  
public:
     
int getdata();
}; 

 

/*
  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;

 

编译: 
[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
++ -out out.cpp ./d.a
[root@localhost code]# .
/out
1231
[root@localhost code]#
posted @ 2008-11-09 21:06  killkill  阅读(7101)  评论(1编辑  收藏  举报