简体   繁体   English

在Cocoa项目中使用C ++类时找不到标准C ++包含

[英]Can't find standard C++ includes when using C++ class in Cocoa project

I have a Cocoa project (a Mac OS X app), all Objective-C. 我有一个Cocoa项目(Mac OS X应用程序),所有Objective-C。 I pulled in one C++ class (which I know works) from another project, and make an Objective-C wrapper for it. 我从另一个项目中引入了一个C ++类(我知道它可以工作),然后为它创建一个Objective-C包装器。 The ObjC wrapper class is using a .mm extension. ObjC包装器类使用.mm扩展名。 However, the C++ header file contains #include s to standard C++ header files ( <vector> , for example), and I get errors on those. 但是,C ++头文件包含#include s到标准C ++头文件(例如<vector> ),我得到了错误。

A minimal example would look like the following. 最小的例子如下所示。 CppClass is the C++ class, and CppWrapper is the ObjC class which wraps it. CppClass是C ++类,CppWrapper是包装它的ObjC类。

//  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

The error I'm getting is the #include of vector in CppClass.h. 我得到的错误是CppClass.h中的vector#include The error is 错误是

lexical or Preprocessor issue: 'vector' file not found 词法或预处理器问题:找不到'vector'文件

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. 这段代码在另一个(所有C ++)项目中运行良好,所以我很确定它是一个构建设置,或者我在包装类中做错了。 I'm using Xcode 4. I created a default Cocoa Mac OS app project and all settings are default. 我正在使用Xcode 4.我创建了一个默认的Cocoa Mac OS应用程序项目,所有设置都是默认设置。

Update : I just realized that if I set TestAppDelegate's File Type to Objective-C++ (or rename it to TestAppDelegate.mm), it works. 更新 :我刚刚意识到如果我将TestAppDelegate的文件类型设置为Objective-C ++(或将其重命名为TestAppDelegate.mm),它就可以工作。 What I don't understand is, this class is pure Objective-C; 我不明白的是,这个类是纯粹的Objective-C; why does it have to be compiled as Objective-C++? 为什么必须编译为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++. 在我的C ++类上使用Objective-C包装器的全部意义在于我不必将整个项目构建为Objective-C ++。

The problem with your CppWrapper class is that it doesn't present a pure Objective-C interface. 您的CppWrapper类的问题是它不提供纯Objective-C接口。 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.h文件中,您要导入C ++类的头文件,这意味着任何导入包装类的Objective-C类都需要编译为Objective-C ++,包括TestAppDelegate

Instead, you'd need to do something like this to completely hide the C++ within the CppWrapper.mm file: 相反,你需要做这样的事情来完全隐藏CppWrapper.mm文件中的C ++:

//  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

Personally, I would 就我个人而言

#include "CppClass.h"

instead of importing it. 而不是导入它。

That's probably not your problem though. 这可能不是你的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM