Note that there are some explanatory texts on larger screens.

plurals
  1. POCan't find standard C++ includes when using C++ class in Cocoa project
    primarykey
    data
    text
    <p>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 <code>#include</code>s to standard C++ header files (<code>&lt;vector&gt;</code>, for example), and I get errors on those. </p> <p>A minimal example would look like the following. CppClass is the C++ class, and CppWrapper is the ObjC class which wraps it. </p> <pre><code>// CppClass.h #ifndef _CPP_CLASS_H_ #define _CPP_CLASS_H_ #include &lt;vector&gt; class CppClass { public: CppClass() {} ~CppClass() {} private: std::vector&lt;int&gt; my_ints; }; #endif /* _CPP_CLASS_H_ */ // CppWrapper.h #import &lt;Foundation/Foundation.h&gt; #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 </code></pre> <p>The error I'm getting is the <code>#include</code> of <code>vector</code> in CppClass.h. The error is</p> <blockquote> <p>lexical or Preprocessor issue: <code>'vector'</code> file not found</p> </blockquote> <p>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.</p> <p><strong>Update</strong>: 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++.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload