3

I just renamed one Objective-C class implementation file in a static library / framework from .m to .mm and then linking fails with this error:

Undefined symbols for architecture armv7s: "___gxx_personality_sj0"

I can resolve it by adding -lc++ (libc++.dylib) to other linker flags of the app's target. But my question really is: why does it fail? Why does this issue appear only in static library code, but not when renaming one of the app's implementation files?

And are there any other solutions besides adding -lc++?

Note: the implementation of the class contains no code whatsoever. There's no use or import of C++ code in this class either, nor is C++ code used anywhere else in the project.

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
  • possible duplicate of [undefined reference to \`\_\_gxx\_personality\_sj0](http://stackoverflow.com/questions/7751640/undefined-reference-to-gxx-personality-sj0) – Anya Shenanigans Sep 14 '13 at 18:52
  • C++ code needs to support stack unwinding when experiencing exceptions. When you renamed the file from `.m` to `.mm` you turned it into code that needs to support exceptions, and exception handling requires the c++ runtime to work properly. – Anya Shenanigans Sep 14 '13 at 18:55
  • For this reason I have both C++ and Objective-C exception handling disabled in build settings. – CodeSmile Sep 14 '13 at 18:59
  • @Petesh that doesn't explain why I can rename any file to .mm in the app target, but not in a static library target. And for example cocos2d+box2d project template doesn't have this issue and doesn't link with libc++ even though Box2D is C++ code. – CodeSmile Sep 14 '13 at 19:03

1 Answers1

3

When you create a static library you don't link in the dependent libraries. As a result, when you rename one of the files from .m to .mm it starts to now depend on C++ features such as stack unwinding when receiving exceptions. Even if you tell the compiler that you have no intent of using exceptions (by the denial of C++ exceptions in the compile flags), it still needs to known the potential mechanism for stack unwinding (this is what the personality variable means).

The reason why the template apps from Cocos2d+Box2d don't have this problem is that they possess some .mm files; as a result the c++ compiler is used to perform the final link, which pulls in the c++ library automatically.

Anya Shenanigans
  • 91,618
  • 3
  • 107
  • 122
  • Thanks! Not sure about OpenAL though because I also link the app target with OpenAL.framework. – CodeSmile Sep 14 '13 at 20:30
  • It looks like the demo app has some .mm files. As soon as there are c++ files the code gets linked using the c++ compiler as well. – Anya Shenanigans Sep 14 '13 at 20:34
  • Right, I just verified this. As soon as I change any app target file from .m to .mm the linker error goes away without having to add -lC++. Well I'm going to use -lC++ anyway because I don't want to rely on that. – CodeSmile Sep 14 '13 at 20:39