Including cocoa (objective C) in a C++ program

Hello,

I am running OS X Lion 10.7.5 and trying to include cocoa.

1
2
3
4
5
6
#include <iostream>
#include <Cocoa/Cocoa.h>
int main(int argc, char** argv) {
    std::cout << "Hello, world!" << std::endl;
    return 0;
}


When I attempt to compile the above code, I get several thousand errors, mostly "stray ‘@’ in program", coming from the cocoa framework. It compiles and runs correctly if I omit the cocoa include or if I name the file main.mm instead of main.cpp.

I'm pretty sure it's failing because cocoa is written in objective c and I'm reading it as c++ code. If I'm wrong about this, I'd certainly like to know. However, assuming I'm right about that part...

Does anyone know how to include an objective c file in a c++ program? Thanks in advance.
Last edited on
You can mix Objective-C and C++, surprisingly it's called Objective-C++

You must give any files that mix the two languages a .mm extension to instruct the compiler to compile your sources as Objective-C++ instead of Objective-C or you'll get the errors you've already seen. I strongly suggest that you try and keep the two languages as separate as possible and only use Objective-C++ to create bridging wrappers where necessary.

Read this:
http://philjordan.eu/article/mixing-objective-c-c++-and-objective-c++

There are a number of other caveats, Google is your friend.
I've read through that article several times now, along with a few of the associated ones. However, I'm still struggling to get things included. The motivating case is that I want to use SDL, which is written in c. I know right now that everything except the cocoa wrapped inside SDL will be c++, so I want to build a single wrapper. Here's my minimal example at the moment.

main.cpp:
1
2
3
4
#include "CocoaWrapper.mm"
int main (int argc, char** argv) {
	return 0;
}

CocoaWrapper.mm:
#import "Cocoa/Cocoa.h"

This is one of several ways to include cocoa I've attempted, and they all have the same problem: the thousands of compile errors I originally described.

Could you show me a basic example of how to structure includes among multiple files? I feel like I'm missing something basic here.

Also, I'm curious what query you used to find that article. My original post (like this one) was preceded by at least one unsuccessful hour on Google.
Last edited on
I've never actually used SDL so I couldn't give you a working example I'm afraid. I'm also at work at the moment so I don't have any time to create an example for you but I'll see what I can do when I get home. I can say however that it's not as simple as dropping an #import directive into a .mm file and including that in your .cpp files.

The article I posted does explain everything in a much clearer manner than I ever could though. I suggest starting without SDL at first. Get your head around how it all works before attempting to include a third party library.

To find that article I simple searched Google for "Mixing objective C and C++" (without quotes). The second result down should be the first article from the same author which contains a link to the one I posted.

Sorry I can't be any more help at the moment, I'll try to whip up a quick example for you later on.
Last edited on
Well, I've still gotten nowhere. Even when I use the exact code examples from the Objective-C++ article, nothing will compile the moment I include cocoa unless I use .mm. I'm on the verge of doing the entire c++-only project with .mm files just because nothing else is working. If you can show me any way for any .cpp file to call any method in cocoa with any level of indirection, I can go from there, but in nearly a week now I have not been able to find or generate such an example.
Last edited on
Topic archived. No new replies allowed.