我想在純C++類(.cpp)和純粹的目標c對象(.m)之間做一個objective-C++包裝(.mm)。一個好的工作示例可以是found on github。我可以構建並運行這個沒有問題。objective-c包裝調用c + +靜態成員函數
但是我需要訪問C++類中的靜態成員函數。
// ==================
// DummyModel.h
// ==================
class DummyModel
{
public:
static int test();
};
// ==================
// DummyModel.cpp
// ==================
#include "DummyModel.h"
static int test()
{
int x = 1;
return x;
}
// ==================
// DummyModelWrapper.h
// ==================
#import <Foundation/Foundation.h>
@interface DummyModelWrapper : NSObject
- (int) test;
@end
// ==================
// DummyModelWrapper.mm
// ==================
#import "DummyModelWrapper.h"
#import "DummyModel.h"
@implementation DummyModelWrapper
- (int) test
{
int result;
result = DummyModel::test();
return result;
}
@end
這產生以下錯誤::
Undefined symbols for architecture i386:
"DummyModel::test()", referenced from:
-[DummyModelWrapper test] in DummyModelWrapper.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
正是這種參考在DummyModelWrapper.mm即是測試我已經通過去除所有現有的功能,並引入一個靜態成員函數修改的github上例調用錯誤:
result = DummyModel::test();
測試項目從GitHub的項目,編譯和運行在它精細的調整修改前的形式(它實例DummyModel
並調用實例上的成員函數)。一旦我嘗試添加靜態成員並從包裝器對象訪問它,就會發生錯誤。
我已經閱讀了一切,我可以找到在stackoverflow和其他地方,但只能找到涉及非靜態成員函數的示例。
引用
http://www.philjordan.eu/article/mixing-objective-c-c++-and-objective-c++
http://robnapier.net/blog/wrapping-cppfinal-edition-759
http://www.boralapps.com/an-objective-c-project-that-uses-c/294/
環境
xcode的4.5.2/osx8.2(靶向的ios5 +)
如果你在「.M」文件調用C++代碼,應該不會是個「.mm」文件擴展名呢?或者,源文件中的評論可能不準確?無論如何,請參閱下面的[benjorbin's](http://stackoverflow.com/questions/14104510/objective-c-wrapper-invoking-c-static-member-functions/14104585#14104585)。 – WhozCraig
是的,對不起,這是一個錯誤的錯字,在問題中修復 – foundry