7

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.

Todd Ditchendorf
  • 11,217
  • 14
  • 69
  • 123
  • 2
    This should help you http://stackoverflow.com/questions/2317868/objective-c-m-mm-performance-difference – gsach Aug 12 '12 at 17:44
  • 3
    Note that C++ is not a strict superset of C, so it's possible to run into cases where you use e.g. C99 code which will not compile if you put it in an Objective-C++ file. – echristopherson Aug 12 '12 at 17:53
  • @echristopherson that's a pretty important point. You should put it in a proper answer. :) – jalf Aug 12 '12 at 18:44
  • 1
    Compiling as ObjC++ limits your range of potential variable names, because you're not allowed to use reserved words from C++. For example, you can't name variables things like "operator" or "new". – Dave DeLong Aug 12 '12 at 20:10

6 Answers6

16

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.

benzado
  • 82,288
  • 22
  • 110
  • 138
4

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

gsach
  • 5,715
  • 7
  • 27
  • 42
  • Yes, when I take a look at the building log in Xcode. All the Objective-C file takes less than 1 second to build. A file with 1180 lines of code takes 0.9 seconds to build in the Xcode building log. However, in general, the `mm` and `c++` files take a longer time. One `mm` file with 400 lines of code takes about 4.8seconds. – RY_ Zheng Sep 14 '20 at 09:08
4

As I wrote in a comment, C++ is not a strict superset of C, so it's possible you'd run into cases where you use e.g. C99 code which will not compile if you put it in an Objective-C++ file. I had this problem recently using C99 compound literals.

echristopherson
  • 6,974
  • 2
  • 21
  • 31
3

Yes, it's bad idea.

When I see a .mm file, I expect it to have C++ code (in addition to Objective-C of course). There are a few things not directly related to OOP that are a bit different in C++ comparing to C.

So name all your Objective-C files as .m. As soon as you need any C++ features – rename it to .mm and verify that everything works.

You get bonus points if you keep your header files C++–less.

Farcaller
  • 3,070
  • 1
  • 27
  • 42
0

.mm extension means Objective-C++ file. Compiler takes more time to compile c++ code than C code.

So, if it is not required, keep the extension as .m only.

Apurv
  • 17,116
  • 8
  • 51
  • 67
0

From my experience (at Apple): 1) the xcode team thinks about c++ last (took forever to get blocks support in objc++) 2) objc++ is much slower in compiling

Mark Pauley
  • 1,445
  • 12
  • 11
  • 4
    It isn't that the Xcode team thinks about C++ last, it is that doing *anything* with C++ is about an order of magnitude more complex and difficult than one might assume! – bbum Aug 12 '12 at 20:52
  • 1
    In that case, KISS would dictate that you avoid obj-c++ until you absolutely need it. By the way: what sort of VCS are you using that can't handle file renames easily? SVN / Git / Hg all work pretty well when renaming files. – Mark Pauley Aug 13 '12 at 15:43