Bungles
Bungles

Reputation: 2249

Xcode and C++ Headers

I have a question about using C++ header files in Objective-C++ modules in Xcode. Specifically, why can I #include them in source files but not header files?

Here is a specific example.

I'm using Xcode 7.2.1 and have two projects. The first is a C++ framework I package into "myFramework.framework". It exposes "myFramework.h", which in turn pulls in "myLib.h". At the top of "myLib.h" is an "#include <string>".

The second project is an Objective-C iOS app which consumes the above framework. In this project, "myViewController.mm" (Objective-C++ source) has "#import "myFramework/myFramework.h" at the top and makes reference to things defined in that header file.

At this point all is well and good. It builds and runs with no issues.

When I move the "#import myFramework/myFramework.h" line to "myViewController.h", the compile fails because it cannot locate the "" header dependency.

It doesn't matter if I change the file type for "myViewController.h" to Objective-C++ header from plain old "C Header". Either way, Xcode's header search paths don't look for standard C++ headers.

So my main question is why does it behave this way? Why is a #include/#import treated differently just because it's in a header file?

My second question is if there's some way to make Xcode treat the #include/#import the same when it's in the header file instead of the source file?

Thanks much!

Upvotes: 1

Views: 3403

Answers (2)

sergio
sergio

Reputation: 69027

Are you sure that you get the error while compiling the myViewController.mm file?

Check if myViewController.h is imported into some other, non ObjC++ file (and that that one is the file that fails to compile).

Upvotes: 3

trojanfoe
trojanfoe

Reputation: 122381

I suspect the issue with including C++ headers inside other headers is that an Objective-C source file gets to see the C++ header file, which upsets it.

If you have mixed C++/Objective-C++/Objective-C then you are probably better off only exposing a pure Objective-C interface to other modules in the project and include any C++ header files in the Objective-C++ source files only.

Alternatively make everything Objective-C++ and then you don't need to worry about it at all.

Hopefully this answers your second question as well.

Upvotes: 3

Related Questions