On Jul 7, 2011, at 5:30 PM, David Frantz wrote:
You bring up a good question. Would it not make more sense to use #3. Personally I would think that #1 would be the worst choice. Why? Because you impact the way the compiler sees the file.
It will see it as Objective-C++ which forces the build environment to include the C++ library and prepare for the appropriate runtime, which would also include that the build environment automatically sets all required additional options for compiler and linker.
This "impact" is actually intended.
Plus if you are writing in Objective C it is a bit misleading to indicate that the file is an Objective C++ file. It would be like opening a dot-sh file to find Python code inside.
Only files with extension .mm are compiled as Objective-C.
If you create any other .m source file, it will be compiled as Objective-C. Sure, if the main function is located in file "main.mm" it will become Objective-C++, but this doesn't seem to have any consequences for other .m source files.
I'm sure others will pipe up it will be very interesting to see the reasoning. By the way I'm far from an expert Mac programmer so take my comments with a grain of salt.
When using a .mm extension, the build environment (or tool chain) will automatically setup the build settings so that it properly builds a product that runs in the C++ runtime and in the Objective-C runtime. For instance, it chooses the correct name for the standard C++ library and links against it and all required other libraries (if any), either dynamically or statically as specified in other Xcode's build settings.
Some tool chains may require to compile the main function with the C++ Compiler due to static initialization of C++ instances. I don't know if this is required for gcc or llvm, though.
I was worrying wether it is sufficient when just linking agains libstdc++ and specifying this in "Other Linker Flags" option in Xcode.
As to build settings, why the aversion to changing them? Isn't the whole point of "build settings" to configure XCode to build a proper app?
This is not an aversion, it is just one additional step which a programmer has to do when using this static library. And I was worrying if it is enough to link against libstdc++ and get a proper runtime.
And for convenience, if a programmer which wants to use this static library just needs to link against it and nothing else in order to set up his project, this would be optimal.
Andreas