Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it a bad idea to use .mm files instead of .m just in case I use C++ later?

Assume I'm developing a typical Mac or iOS application using Apple's latest Xcode tools. Further assume that I am primarily developing this application using Objective-C and leveraging all of the relevant APIs from Apple's Cocoa or Cocoa Touch frameworks.

Let's say that I don't currently have any plans to use C++ or Objective-C++ in my code base, but I suspect that some time in the future I might want to sprinkle in a little Objective-C++ here an there.

So I'm considering naming all of my .m files as .mm instead, just in case. (This will have the desireable effect of a cleaner history in my SCM system, as I won't have to rename files later.)

Is this a bad idea? Is there any reason why using .mm files is definitely or significantly worse than using .m when the file doesn't actually contain any Objective-C++?

Presumably this file extension flips some switch in the compiler which will then have to parse the source code for not only ObjC, but also C++. Does this have a significant negative effect on build times for moderate-to-large code bases?

Does it have any other negative (or positive) effects that I should keep in mind?

NOTE: please do not respond with any comments about whether ObjC or C++ is better. That is not what this question is about.

like image 569
Todd Ditchendorf Avatar asked Aug 12 '12 17:08

Todd Ditchendorf


2 Answers

It's not the worst idea, but it's not really a good idea, either.

The main purpose of Objective-C++ is to act as a bridge for Objective-C code that needs to use a C++ library. Thus, in most projects, almost all of the code is plain old Objective-C, with maybe a few .mm files to create a "wrapper" object to talk to the C++ library.

Therefore, it is extremely unlikely that you will need to change significant parts of your code over from Objective-C to Objective-C++. You shouldn't have a lot of file renames in your SCM history.

The main problem with using Objective-C++ everywhere is that you will be following "the road less traveled": 99% of the tutorials you read and open-source code you use and learn from will all be written to be compiled by the Obj-C compiler. Using the Obj-C++ compiler will be mostly the same, and probably won't make a difference most of the time, but you will eventually run into some problem that is due to Obj-C++ being compiled slightly differently, but when you find the bug it won't be obvious, and you'll spend a lot of time trying to diagnose it before you realize that it is because you are using a less well-tested compiler setup.

If you have a lot of C++ experience and find yourself "needing" features from C++ in your code, you probably don't really need them, you probably need to spend a little more time figuring out how to do the equivalent in Objective-C. When in Rome, do as the Romans do.

In general, "just in case" is not a good reason to stray from standard practice. You often wind up spending a lot of effort on something you aren't going to need.

like image 172
benzado Avatar answered Dec 21 '22 23:12

benzado


Quote from Barry Wark:

The major disadvantage to using .mm over .m for "normal" Objective-C is that compile times are significantly higher for Objective-C++. This is because the C++ compiler takes longer than the C compiler. With Xcode 3.2 and higher, Objective-C code can use the Clang frontend tool chain to significantly speed up Objective-C/C compiling times. Since Clang does not yet support Objective-C++/C++, this further widens the gap in compiling times between the two.

BUT

UPDATE Feb 17, 2012 As of Xcode 4.0 (with LLVM 3.0), Clang has supported Objective-C++. Even C++11 support is quite strong now.

So I think that its ok to use .mm as long as if you only use C features, .mm files should generate code that performs very similar to .m

like image 27
gsach Avatar answered Dec 22 '22 01:12

gsach