Я использую небольшую библиотеку C ++ в приложении Какао (просто очень простой пример, чтобы узнать, как мне это сделать).
Итак, у меня есть небольшой класс C ++ в пространстве имен, выглядящий следующим образом:
namespace testlib {
class Test {
public:
Test(unsigned a);
Test operator+(const Test& other) const;
Test operator+(unsigned other) const;
Test& operator+=(unsigned other);
Test& operator+=(const Test& other);
unsigned getValue() const;
private:
unsigned theValue;
};
}
Тогда у меня есть файл .h с следующим кодом:
#import <Foundation/Foundation.h>
#import "testlib.h"
@interface test : NSObject {
testlib::Test* testClass;
}
- (id)init;
- (id)add: (unsigned)value;
- (unsigned)value;
- (void)dealloc;
@property void* testClass;
@end
и, наконец, реализация (в файле с именем test.mm):
#import "test.h"
@implementation test
@synthesize testClass;
- (id)init {
testClass = new testlib::Test(0);
return self;
}
- (void)dealloc {
delete testClass;
[super dealloc];
}
- (id)add: (unsigned)value {
*testClass += value;
return self;
}
- (unsigned)value {
return testClass->getValue();
}
Когда я пытаюсь скомпилировать это, я получаю следующие ошибки:
CompileC build/testipod.build/Debug-iphonesimulator/testipod.build/Objects-normal/i386/WindowController.o WindowController.m normal i386 objective-c com.apple.compilers.gcc.4_2
cd /Users/sausalito/eth/testipod
setenv LANG en_US.US-ASCII
setenv PATH "/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/gcc-4.2 -x objective-c -arch i386 -fmessage-length=0 -pipe -std=c99 -Wno-trigraphs -fpascal-strings -fasm-blocks -O0 -Wreturn-type -Wunused-variable -D__IPHONE_OS_VERSION_MIN_REQUIRED=30000 -isysroot /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.3.sdk -fvisibility=hidden -mmacosx-version-min=10.5 -gdwarf-2 -iquote /Users/sausalito/eth/testipod/build/testipod.build/Debug-iphonesimulator/testipod.build/testipod-generated-files.hmap -I/Users/sausalito/eth/testipod/build/testipod.build/Debug-iphonesimulator/testipod.build/testipod-own-target-headers.hmap -I/Users/sausalito/eth/testipod/build/testipod.build/Debug-iphonesimulator/testipod.build/testipod-all-target-headers.hmap -iquote /Users/sausalito/eth/testipod/build/testipod.build/Debug-iphonesimulator/testipod.build/testipod-project-headers.hmap -F/Users/sausalito/eth/testipod/build/Debug-iphonesimulator -F/Users/sausalito/eth/testipod/../testlib/build -I/Users/sausalito/eth/testipod/build/Debug-iphonesimulator/include -I/Users/sausalito/eth/testipod/build/testipod.build/Debug-iphonesimulator/testipod.build/DerivedSources/i386 -I/Users/sausalito/eth/testipod/build/testipod.build/Debug-iphonesimulator/testipod.build/DerivedSources -include /var/folders/4f/4fSYMOmtHHSRoBf+XVFQ+k+++TM/-Caches-/com.apple.Xcode.502/SharedPrecompiledHeaders/testipod_Prefix-cwdputxcxpofoydkulngkdplqxbt/testipod_Prefix.pch -c /Users/sausalito/eth/testipod/WindowController.m -o /Users/sausalito/eth/testipod/build/testipod.build/Debug-iphonesimulator/testipod.build/Objects-normal/i386/WindowController.o
In file included from /Users/sausalito/eth/testipod/Classes/test.h:10,
from /Users/sausalito/eth/testipod/WindowController.m:10:
/Users/sausalito/eth/testipod/Classes/testlib.h:1: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'testlib'
In file included from /Users/sausalito/eth/testipod/WindowController.m:10:
/Users/sausalito/eth/testipod/Classes/test.h:13: error: expected specifier-qualifier-list before 'testlib'
/Users/sausalito/eth/testipod/Classes/test.mm:13:0 /Users/sausalito/eth/testipod/Classes/test.mm:13: error: type of property 'testClass' does not match type of ivar 'testClass'
Что я делаю не так ?? Похоже, он использует компилятор C для компиляции заголовочного файла.
Я могу обернуть это в класс Objective-C, когда я объявляю член как тип void *, а затем использую приведение в реализации. Но это определенно не самый удобный способ сделать это.