Objective-C++

Member
Posts: 215
Joined: 2008.06
Post: #1
How well/how does Objective-C integrate with C++? Can it be done? I am considering using a mix of C++ and Obj-C to build an intro game in SDL. Will this work, and how would I go about doing so? I plan to use the C++ for the OGL aspects almost exclusively, and then use Objective-C for object management and model-level management. Recommendations, comments, advice?

Mac users swear by their computers, PC users swear at their computers. ~Unknown

iSayz
Quote this message in a reply
Member
Posts: 156
Joined: 2002.10
Post: #2
Yes it can be done. Goolge up on Objective-c++ - essentially, rename your source files to *.mm and then you can include a mixture of obj-c and C++ syntax.

I find this a bit clumsy though, and in the past when I have done something similar (a C++ game & graphics engine, with Cocoa for windows, dialogs, user input - I made a simple C wrapper, which I had functions that the Obj-C event handlers would call that would call into the C++ code, and vice versa, functions that would be called from C++ and would call methods on the Obj-C classes.

So yes it can be done, just have a think about exactly what you are trying to do.

Cheers

- Iain
Quote this message in a reply
Member
Posts: 336
Joined: 2004.07
Post: #3
With some jiggling of project build settings, you can get it to compile + link your c++ code without the rename. I believe setting "Compile Sources As" to "according to file type" allowed me to drop C++ code in as-is.

Justin Ficarrotta
http://www.justinfic.com
"It is better to be The Man than to work for The Man." - Alexander Seropian
Quote this message in a reply
Member
Posts: 215
Joined: 2008.06
Post: #4
So, I once tried to build a class that encapsulated some basic OpenGL calls, but it would die on glEnable and other initialization steps. Do calls like glEnable, glDisable, and others HAVE TO reside in 'int main (int argc, char** argv){ }'? Any idea why it wouldn't let me abstract my OpenGL calls in such a way?

Mac users swear by their computers, PC users swear at their computers. ~Unknown

iSayz
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #5
No idea at all. You'll have to be *way* more specific.
Quote this message in a reply
Apprentice
Posts: 19
Joined: 2007.03
Post: #6
Had you created a valid OpenGL context yet? Making any OpenGL calls without a context automatically results in a program crash.
Quote this message in a reply
Member
Posts: 215
Joined: 2008.06
Post: #7
Is there any overhead to add the .mm extension to implementation files? Is it irresponsible to set this extension on classes that do not have C++ code in them, but may be used in projects that involve C++?

Mac users swear by their computers, PC users swear at their computers. ~Unknown

iSayz
Quote this message in a reply
DoG
Moderator
Posts: 869
Joined: 2003.01
Post: #8
.mm takes longer to compile, and is no longer fully C compatible, as C++ isn't. I would strongly advise against naming everything .mm, just in case you ever use C++. With ObjC, you shouldn't really have to use C++ at all, unless you need to use some library or API that is C++.
Quote this message in a reply
Member
Posts: 254
Joined: 2005.10
Post: #9
DoG Wrote:.mm takes longer to compile, and is no longer fully C compatible, as C++ isn't. I would strongly advise against naming everything .mm, just in case you ever use C++. With ObjC, you shouldn't really have to use C++ at all, unless you need to use some library or API that is C++.

IMHO, if you need to use Objective-C++ you should try to keep it as separated as possible for code re-use. Maybe make some Objective-C wrappers for your C++ code to keep it contained to those classes.
Quote this message in a reply
Member
Posts: 215
Joined: 2008.06
Post: #10
Blacktiger Wrote:IMHO, if you need to use Objective-C++ you should try to keep it as separated as possible for code re-use. Maybe make some Objective-C wrappers for your C++ code to keep it contained to those classes.

Alright, well maybe I can shed some more light on the question: I have some *purely* Obj-C classes that I am using for an OpenGL project. Is it necessary for me to specify .mm on these classes in order to make them work with the C++ OGL calls, or can I leave them as .m and still work with them in the C++ context?

Mac users swear by their computers, PC users swear at their computers. ~Unknown

iSayz
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #11
Talyn Wrote:C++ OGL calls

OpenGL is a C API. There shouldn't be any C++ involved.
Quote this message in a reply
Member
Posts: 215
Joined: 2008.06
Post: #12
ThemsAllTook Wrote:OpenGL is a C API. There shouldn't be any C++ involved.

OK, so you're saying that I could use my Obj-C classes WITHOUT .mm extension without issue in OpenGL?

Mac users swear by their computers, PC users swear at their computers. ~Unknown

iSayz
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #13
As long as you're not using any C++ features, yes. Objective-C is a superset of C, so anything that works in a .c file will work the same way in a .m file.
Quote this message in a reply
Member
Posts: 215
Joined: 2008.06
Post: #14
Cool! But I am using SDL. Does this impose restrictions as far as the use of Objective-C?

Mac users swear by their computers, PC users swear at their computers. ~Unknown

iSayz
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #15
SDL is also a C API.
Quote this message in a reply
Post Reply