日期:2014-05-16  浏览次数:20696 次

请问 : 怎么用 g++ 编译一个类?
//------------sigma_rmf.h----------------
#ifndef __SIGMA_RMF_H__
#define __SIGMA_RMF_H__

class SigmaMgr
{
public:
SigmaMgr();
~SigmaMgr();
};


#endif // __SIGMA_RMF_H__

//------------sigma_rmf.cpp---------------
#include "sigma_rmf.h"
#include <stdio.h>

SigmaMgr::SigmaMgr()
{
puts("SigmaMgr");
}

SigmaMgr::~SigmaMgr()
{
puts("~SigmaMgr---");
}

就是这样的一个类.很简单. 我用 rh9 自带的 gcc 3.2.2 编译, shell 如下:
g++ -c sigma_rmf.cpp
发现错误如下:
g++: compilation of header file requested

不明白的地方:
1. 编译一个类,应该编译 .h 和 .cpp 两个文件才对吧? 那么这里怎么编译 .h 文件呢?
2. 这个错误是啥意思?
3. 要怎么做?

------解决方案--------------------
1. 编译一个类,应该编译 .h 和 .cpp 两个文件才对吧? 那么这里怎么编译 .h 文件呢?
在cpp源文件中include后就自动编译了。
2. 这个错误是啥意思?
需要头文件,g++把sigma_rmf.cpp当成目标文件了,而没有当成源文件,应该这样编译:
g++ -c sigma_rmf.o sigma_rmf.cpp
3. 要怎么做?
如果使用这个类编译成可执行文件需先在main.c里include这个头文件和cpp文件。
g++ -o main main.c
要把这个类直接生成共享库,可参考:
http://blog.chinaunix.net/u/22617/showart_372070.html