views:

358

answers:

3

Hi. 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'm 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.

A: 

I'm not aware of any way to do that. 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.

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.

Pavel Minaev
Thanks Pavel, you got it exactly. I think the problem was that I must have had Obj-c code in the header file of my texture loading code. That meant that when my c++ code came to include the .h file, the compiler didn't like it.I'll try and summarise:Say I have Foo.cpp and Foo.h (c++ only) and Foo.cpp needs to include TextureLoader.h, which contains a c++ class definition whose member methods use obj-c code (in TextureLoader.mm). Make sure there is NO obj-c or cocoa code in TextureLoader.h or else Foo.cpp won't be able to #include it.
The obvious answer is - don't define your member functions that use ObjC constructs in the header file; define them in .mm file instead.
Pavel Minaev
+3  A: 

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.

Alex Reynolds
+1  A: 

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.)

Jesse Beder