views:

363

answers:

2

Because I'm a novice to all things C++ and gcc related I need to understand something that's potentially critical. If I use .mm files in a new iphone project whose output is a static lib I get this error:

"___gxx_personality_v0", referenced from:

If I change the C++ Standard Library type from Dynamic (the default) to static the error goes away. Is this a limitation of the SDK? Can Obj-C++ extensions NOT be enabled in a dynamic library? Keep in mind that I'm a novice and I only pretend to know what I'm talking about here. I also want to know the difference between these settings and if changing them has a potentially negative impact. Is my assumption correct in that a dynamic library is loaded at runtime while a static library is linked into the final binary at build time? If this is so, then why would the iPhone SDK allow the building of a dynamic library? You can't install 3rd party libs on the iPhone to date and have them shared between apps.

Update A couple people have responded and I appreciate the answers. However I wish to clarify something that appears misunderstood from my original question. Switching the file type from ".m" to ".mm" is not the answer, rather it's what triggered the exception. The situation is this, develop a static lib for iPhone that uses Obj-C++ files with ".mm" extension. Then use this library in an iPhone application (or unit test suite). Unless the client target is switched to use "static" instead of the default "dynamic" for the Std C++ library type you will see this error. I originally thought I may have been introducing a subtle error by changing this setting but I'm now trying to get an understanding of the difference and why the default is set in a way that seems opposite of its typical use case.

A: 

*.m is the Obj-C extension.
*.M is the Obj-C++ extensions.

Rename your files so that the compiler kows to use the C++ standard library when linking.

Martin York
I did rename my files, that what caused the error. I use ".m" for Obj-C++, though I heard you could use ".M" as well. My problem is that I don't know what, if anything, will be affected by using "static" for the std C++ library type. I also don't understand why the default is set to dynamic.
Cliff
It is set to dynamic so that it uses the latest one that is released with the OS. If you link statically then your app will always be using the same version which will go out of date. Also it allows several applications to share the same piece of code and this makes the OS more effecient.
Martin York
You should not use .m files for code that uses objective C++ ro links against C++ code. This as you have found will lead to linker problems.
Martin York
You're correct that you cannot use .m files for code that uses Obj-C++, but you can definitely use .m files for code that links against C++ code. There is no problem mixing languages in the project at link time.
Rob Napier
The file extension is not what I'm asking about here. I'm completely aware that you need to use the correct extension for Obj-C++. My question is specifically the switching and defaulting of "dynamic" vs "static" library types in iPhone projects.
Cliff
+2  A: 

Martin York is correct that you're compiling with the wrong compiler, but the default file system for OSX is not case sensitive, so you cannot generally use ".M" to indicate Obj-C++ in practice. You need to name your Obj-C files with .m and your Obj-C++ files with .mm.

While you cannot use Obj-C++ in .m files, you can certainly link .m files against C++ code. There is no problem mixing Obj-C, Obj-C++ and C++ in the same project. Just name you files correctly so that you use the right compiler:

  • m - Obj-C
  • mm - Obj-C++
  • cpp - C++

You are correct that you cannot load private dynamic libraries at runtime on iPhone. Xcode allows it for two reasons: Mac does allow dynamic libraries (and so it would be a pain to have special-case code in the IDE based on the SDK), and Apple developers can and do create dynamic libraries on iPhone. So Xcode does not forbid it; they just don't give you a template for it.

Rob Napier
I understand that there is a place for dynamic libraries in Mac and even iPhone. My question is why is the default set to "dynamic" in the iPhone project template? It implies that this is the way libraries are supposed to be developed for iPhone. If that is the case, then what are the repercussions for using static for iPhone libs?
Cliff
I've never seen it set to dynamic by default. Did you use the static library template when you created the target or project? It should have set it to static. You should not make dynamic libraries for iPhone; you should link with system dynamic libraries, however. A static library means that it will be linked directly into your final binary. There will only be one binary file in the end; no .a will be copied to the phone.
Rob Napier