混合c++ 和 objc, 主要是當想要用 c++ 的 class 時, 一定要放到 .mm 檔里.
另外, 在 .cpp 檔案里, 一定不能夠include 任何系統有關的 .h 檔. 當要用到一些系統的東西, 要自己寫些wrapper function. 比如你想在 c/c++ class 里用 fopen 來打開一個檔案, 怎麼拿檔案的全路徑(FULL PATH)呢?
在我的一個叫 wrapper.m 的文檔里, 我寫了一個 GetPath 的 function:
复制内容到剪贴板
代码:
const char *GetPath(const char *s)
{
NSString *name = [[NSString alloc] initWithUTF8String: s];
NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
return [path UTF8String];
}在 c/c++ 檔里, 相關的設定:
复制内容到剪贴板
代码:
extern "C" const char *GetPath(const char *s);當我想在 c/c++ 原碼里打開一個檔案時, 我就用:
复制内容到剪贴板
代码:
const char *path = GetPath(filename);
FILE* file = fopen(path, "rb");[
本帖最后由 dr_watson 于 2008-4-18 09:47 编辑 ]