4

So I'm trying to use C++ inside my ios project.

After I make a new project (all default settings, fresh install of xcode), I create a Question.h and a Question.mm file, like this:

Question.h #include <iostream> #include <string> using std::string; class Question { public: string text; }; Question.mm #include "Question.h" 

it screams with errors like: Iostream: No such file or directory

using Xcode 3.2.6 with iOS SDK 4.3

What am I doing wrong?

4
  • 1
    Iostream in the error message begins with a capital letter - have you done that in any of your code? Commented Jun 8, 2011 at 12:36
  • Does is show the same error if you rename Question.mm -> Question.cpp? Commented Jun 8, 2011 at 12:46
  • 1
    its with capital letter in the Build Results window, but in the code it's iostream. One more thing: if I move all the code from the h file to the mm file it does build successfully Commented Jun 8, 2011 at 12:49
  • @Alex, changing the extension has the same result Commented Jun 8, 2011 at 12:50

3 Answers 3

5

The most likely cause of this is that you're including Question.h in a non-C++ file, for instance perhaps your AppDelegate is a .m file and you're trying to include Question.h there.

There are two solutions. First, you can make everything Objective-C++ (renaming all .m files to .mm). Historically I've found that to be extremely inconvenient because both Xcode and gdb have always had a lot of trouble with ObjC++ and you can get a lot of confusion (the dreaded "no this pointer" and "unknown language for stack frame" errors in gdb). I haven't done enough work with the latest versions of Xcode and gdb to determine if this is still a problem, but I suspect it is since gdb hasn't gotten a lot of work. Also, ObjC++ is slower to compile.

The other option is to wrap up your C++ into ObjC so that you can include it freely into pure ObjC portions of the code. This is the approach I usually take. ObjC++ is a bit of a mess of a language IMO, and I believe it's better to keep ObjC (.m) and C++ (.cpp) pure and separate with a thin layer of ObjC++ (.mm) to glue them together.

Sign up to request clarification or add additional context in comments.

1 Comment

I still can't believe how blind I am. I WAS including Question.h in appDelegate. The funny thing is that I did a Project Search for Question.h and no result showed..
1

Make sure your main.m file isn't importing your AppDelegate file. It may be using it like this:

NSStringFromClass([AppDelegate class])

but instead you can just do this:

@"AppDelegate"

That solved my problem.

Comments

0

Try renaming Question.mm to Question.cpp as xcode decides which compiler to use based on the extension.

2 Comments

that doesn't fix it. besides, I have another project that i created some time ago, which uses h/mm files and they do compile successfully. can't point the difference between them tho..
As a side note: if you can rename Question.mm to Question.cpp, you should. Pure C++ is easier to manage and faster to compile than Objective-C++. If you need access to just a couple of Cocoa-like things (like reading an NSArray), you can also use Core Foundation, which is a pure-C interface to many of the Cocoa data structures. You can include that in C++ without switching to Objective-C++. Obviously there are some places that ObjC++ is required, but when you don't need it, it's nice to avoid.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.