Cliff
Cliff

Reputation: 11248

XCode and the iPhone SDK: C++ Standard Library Type?

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.

Upvotes: 2

Views: 1817

Answers (3)

karim
karim

Reputation: 15589

In XCode > with ARC enable and in Test target, I got this error ___gxx_personality_v0 with the files that have .mm extension. When I renamed the files from .mm to .m extension, that solved the issue.

Upvotes: 0

Rob Napier
Rob Napier

Reputation: 299345

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.

Upvotes: 2

Loki Astari
Loki Astari

Reputation: 264431

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

Upvotes: 0

Related Questions