简体   繁体   中英

Objective-C with cpp extensions, I don't know what I'm doing

So I'm still feeling like a novice at times when strange errors jump out of XCode. Yesterday I started getting this:

___gxx_personality_v0", referenced from: ___gxx_personality_v0$non_lazy_ptr

That was when building a unit test target that used a .mm file in XCode with the iPhoneSDK. The error went away when I changed the file extension back to .m since I was not yet using C++ code in it. (Though I plan to.) With my limited knowledge I googled and saw others indicating the wrong compiler was being used. Another link referred to a flag being incorrectly set. With even more limited knowledge I decided the linker options in the UnitTest target was the place to go. As it turns out the Standard C++ Library type setting is set to Dynamic by default . Changing this setting to static made my error go away and my test now green bars... err... green buttons. I'm asking here because I still don't know exatcly what I'm doing. Obviously this setting affects the compiler but I need more detail. Am I setting myself up for another catastrophy down the road. What negative impact will this have if any? Why have I not hit the problem before? I've used .mm files in unit tests before though I believe I was then using the GoogleToolsForMac code base to do unit testing where I now use native XCode support for unit testing. Can somebody explain for me?

Short answer: The error happens because you compile C++ code with the gcc driver instead of g++ .

Longer answer: When you do call gcc myfile.mm , the GCC driver is smart enough to recognize from the .mm file extension that you're compiling Objective-C++ code, and so it compiles it well. But then, at link time, if you still use gcc as a driver (maybe you compile and link in one step?), it doesn't automatically link in the C++ stanbard library (and support object files), thus failing to link.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM