views:

224

answers:

3

I have a C++ library I want to add to my iphone project.

In one header file I declare

@interface a
{
   cppvirtualclass V;
}

This compiles fine for the iPhone device with Release settings. However it refuses to compile for the Simulator with or without debug info.

It give the error

error: type 'V' has virtual member functions.

Is there a way out of this or do I have to define only concrete C++ classes?

A: 

I find mixing C++ and Objective-C types to be a pain. I usually just give up bung all the C++ stuff into a single struct that the Objective-C parent holds as a pointer.

Marcelo Cantos
+2  A: 

By default Objective C does not run constructors on C++ instance variables when it creates Objective C objects. This means (I think) that the C++ object's vtable will not get initialised correctly.

Try making your instance variable a pointer and allocating it in the init method (and destroying it in dealloc/finalize).

Or

try setting "Call C++ default Ctors/Dtors in Ovjective-C" in your target code generation settings.

(GCC_OBJC_CALL_CXX_CDTORS, -fobjc-call-cxx-cdtors)

JeremyP
I've taken the pointer route at the moment, but that fails to explain why device code (at least) compiles.
John Smith
I have no clue why that might be the case. Are your target settings different for the different architectures in some way? I would get the text of the invocation of g++for both builds and see how the command line switches differ.
JeremyP
A: 

Also, Objective-C++ is no longer a permitted language for iPhone development (per 4.0). You can use Objective-C, C and C++, but not Objective-C++.

Frank
This is an interesting interpretation of that statement.
KennyTM
Really?! How do they plan to differentiate between an Objective-C++ program and an (Objective-C, C++) one?
John Smith
The list of permitted languages are Objective-C, C and C++ (plus javacript for webapps). Objective-C++ is not in that list. As for differentiating, I'm sure there's enough symbols in nm output. Eg. .cxx_construct seems to only appears in ObjC++ binaries. Whether they'll enforce it or it's just a legal snafu... who knows.
Frank