在Cocoa项目中使用C ++类时,找不到标准C ++包含 [英] Can't find standard C++ includes when using C++ class in Cocoa project
问题描述
我有一个Cocoa项目(Mac OS X应用程序),所有的Objective-C。我从一个C ++类(我知道工程)从另一个项目,并为它做一个Objective-C包装。 ObjC包装类使用.mm扩展名。但是,C ++头文件包含 #include
到标准C ++头文件(例如< vector>
,我得到的错误。
I have a Cocoa project (a Mac OS X app), all Objective-C. I pulled in one C++ class (which I know works) from another project, and make an Objective-C wrapper for it. The ObjC wrapper class is using a .mm extension. However, the C++ header file contains #include
s to standard C++ header files (<vector>
, for example), and I get errors on those.
一个最小示例如下所示。 CppClass是C ++类,CppWrapper是包装它的ObjC类。
A minimal example would look like the following. CppClass is the C++ class, and CppWrapper is the ObjC class which wraps it.
// CppClass.h
#ifndef _CPP_CLASS_H_
#define _CPP_CLASS_H_
#include <vector>
class CppClass
{
public:
CppClass() {}
~CppClass() {}
private:
std::vector<int> my_ints;
};
#endif /* _CPP_CLASS_H_ */
// CppWrapper.h
#import <Foundation/Foundation.h>
#import "CppClass.h"
@interface CppWrapper : NSObject {
CppClass* myCppClass;
}
@end
// CppWrapper.mm
#import "CppWrapper.h"
@implementation CppWrapper
- (id)init
{
self = [super init];
if (self) {
myCppClass = new CppClass;
}
return self;
}
- (void)dealloc
{
delete myCppClass;
[super dealloc];
}
@end
// The file that uses CppWrapper
// TestAppDelegate.m
#import "TestAppDelegate.h"
#import "CppWrapper.h"
@implementation TestAppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
myWrapper = [[CppWrapper alloc] init];
}
@end
我得到的错误是<$ cppClass.h中的向量
的c $ c> #include 错误是
The error I'm getting is the #include
of vector
in CppClass.h. The error is
词汇或预处理器问题:
'vector'
/ p>
lexical or Preprocessor issue:
'vector'
file not found
此代码在另一个(所有C ++)项目中正常工作,因此我确信它是一个构建设置,在包装类中做错了。我使用Xcode 4.我创建了一个默认的Cocoa Mac OS应用程序项目,所有设置都是默认的。
This code works fine in another (all C++) project, so I'm pretty sure it's a build setting, or something I've done wrong in the wrapper class. I'm using Xcode 4. I created a default Cocoa Mac OS app project and all settings are default.
更新:我刚刚意识到如果我将TestAppDelegate的文件类型设置为Objective-C ++(或重命名为TestAppDelegate.mm),它的工作原理。我不明白的是,这个类是纯Objective-C;为什么它必须被编译为Objective-C ++?在我的C ++类上有一个Objective-C包装器的整个要点是,我不必将整个项目构建为Objective-C ++。
Update: I just realized that if I set TestAppDelegate's File Type to Objective-C++ (or rename it to TestAppDelegate.mm), it works. What I don't understand is, this class is pure Objective-C; why does it have to be compiled as Objective-C++? The whole point of having an Objective-C wrapper on my C++ class is so that I don't have to build the entire project as Objective-C++.
推荐答案
CppWrapper
类的问题是它不提供纯粹的Objective-C接口。在你的 CppWrapper.h
文件中,你正在导入C ++类的头文件,这意味着任何导入包装类的Objective-C类都需要被编译为Objective -C ++,包括你的 TestAppDelegate
。
The problem with your CppWrapper
class is that it doesn't present a pure Objective-C interface. In your CppWrapper.h
file, you're importing the C++ class's header file, which means that any Objective-C class that imports the wrapper class will need to be compiled as Objective-C++, including your TestAppDelegate
.
相反,你需要这样做完全隐藏 CppWrapper.mm
文件中的C ++:
Instead, you'd need to do something like this to completely hide the C++ within the CppWrapper.mm
file:
// CppWrapper.h
#import <Foundation/Foundation.h>
@interface CppWrapper : NSObject {
void *myCppClass;
}
- (void)doSomethingWithCppClass;
@end
// CppWrapper.mm
#import "CppWrapper.h"
#import "CppClass.h"
@implementation CppWrapper
- (id)init {
self = [super init];
if (self) {
myCppClass = new CppClass;
}
return self;
}
- (void)dealloc {
delete myCppClass;
[super dealloc];
}
- (void)doSomethingWithCppClass {
static_cast<CppClass *>(myCppClass)->DoSomething();
}
@end
这篇关于在Cocoa项目中使用C ++类时,找不到标准C ++包含的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!