Using an Objective-C++ file from a C++ file


Using an Objective-C++ file from a C++ file



I have a c++ app that I'm trying to port to iPhone and to start off I'm trying to replace my c++ texture loader with an obj-c++ texture loader so that I can make use of the cocoa libraries.

A lot of my c++ files (.cpp files) call the texture loader with something like:

GLuint mTexture = TextureLoader::LoadTexture("file.png") //LoadTexture is a static method` 

but whenever I try to make a TextureLoader class (inside a .mm file) that has Obj-C code, I am forced to make the calling class also a .mm file.

I want to avoid a creep of .mm usage. How can I do it? Is it even possible?

Basically I have a .mm file that has...

GLuint TextureLoader::LoadTexture(const char* path) {    //...lots of c and obj-c code    return texture } 

and is apart of a c++ class (or is it obj-c++ at this point?)

I want to be able to use it from a .cpp file without having to make the calling class also .mm

Is there anyway to do this?

Cheers guys.




Why do I have to clean up memory if I have an IBOutlet with “retain”, but not when I have one with “assign”?

1:



If my property has no setter (readonly), is it true that UIKit retains the value if it's not NSNumber or NSValue?
I'm not aware of any way to do that.


What happens exactly, when I set an property to nil in the dealloc method?
However, you are not required to put all C++ member function definitions in a single file - just stick all the pure C++ stuff into .cpp, and those that need ObjC into .mm..
How do I find out if I need to retain or assign an property?
As well, if you actually have the same repeated ObjC code (perhaps parametrized), then refactor it into a plain C function or C++ class in one separate .mm - such that there are no ObjC constructs in the corresponding .h - and then use that function/class where needed by including the header..
Can I use a mono library from Objective-C (on the iPhone)?


Which are the Top Level Objects I need to create Outlests for in the File's Owner of my Nib, so that I have less memory problems?


iPhone Data

2:



Why don't I have to release these objects?
I want to avoid a creep of .mm usage.

How can I do it? Is it even possible?
. Not if you want to use Objective-C++, no..


3:


The simplest thing to do is just to compile everything as Objective-C++.

You don't have to change all of your extensions; leave 'em as .cpp and set Xcode to compile everything as Objective-C++: under the build settings of your project, in the category "GCC X.X - Language", change "Compile Sources As" to "Objective-C++".. (I assume your problem is that you have a whole bunch of .cpp files, and you don't feel like renaming them all to .mm.

Compiling as Objective-C++ will have no effect on the ones that don't actually use any Objective-C stuff (with only very rare exceptions), and it'll make your life easier for the ones that do.).


4:


In XCode, right-click on your .cpp file and select “Get Infos”.

In the “General” tab, change the file type to sourcecode.cpp.objcpp.

This will build this file and only this file as Objective C++..


5:


Not sure on the size of the code involved, but if it had a pretty small interface, you could write a C wrapper for your C++ code, and then use extern C { /* ...

*/ }
around the header file declarations and function definitions.

. This would essentially "hide" the C++ calls from Objective-C, and would allow you to circumvent the requirement of renaming your files from the m to the mm extension, and/or the requirement of compiling the calling files as Objective-C++.

. Objective-C++ uses different compiler rules and is slower than a standard Objective-C compile..



78 out of 100 based on 53 user ratings 428 reviews