Any flaw to never create ObjC files, but always create ObjC ++ files?

By default, Xcode creates both an .h and .m file when requesting a new ObjC class. Everything works fine until you need to reference any C ++ file elsewhere in your project and do not run it in a .h or .m file.

At that moment, when the ObjC compiler is completely confused and throwing mountains of parsing errors, and you, the user (that is: me), are even more confused until it hits me: of course, I have to make this file instead this objc ++ file.

Possible options:

  • tell Xcode that this particular file, even if it is a .m file, is really an ObjC ++ file or
  • rename this file to .mm.

The first option is not very pleasant for me, because this file is really ObjC ++, regardless of what, according to the project, is.

The second option doesnโ€™t work, because it twists the Git repository, which then forgets that another .m file was used earlier, which is really the history of this โ€œnewโ€ .mm file.

So, from now on, I decided to always rename any .m file that Xcode creates for me .mm first, after creating, so that I don't lose the story.

So far, it worked well for me, but I have this slight concern in my head that there might be some kind of angular case when I really want to have an ObjC file, not an ObjC ++ file.

What would these corner cases be? Does anyone know of any ObjC ++ file that happens to NOT contain a C ++ link, but somehow strangled the ObjC compiler, simply thanks to the .mm file?

And if there are no flaws, why not just give up .m forever and stick with .mm instead?

+4
source share
5 answers

There are no shortcomings, although two methods ( .cxx_construct and .cxx_destruct ) were created because of you, but they are used only to create and destroy C ++ objects when creating / deallocating an instance. If your class does not have C ++ members, these functions do nothing and add only very low overhead. Otherwise, you still have C functions created for your Objective-C methods, not C ++.

+2
source

C ++ analysis is much slower than ObjC parsing, so ObjC ++ files have significantly longer compilation times. I'm not sure that this overhead will apply to ObjC ++ files that do not contain C ++, but it will make some sense, which is harder to parse because the compiler needs to look for C ++ constructs.

In addition, the C ++ type system has several slightly different rules from C, which also apply to the ObjC / C code in the C ++ file. I do not remember the details, but it will not be harmful; it may just take a few extra throws.

+3
source

Create an Objective-C ++ file template so that you get a .mm file instead of a .m file when you create a new file. Create a copy of the Apple Objective-C class templates and rename the .m files to .mm. For more information on creating Xcode 4 file templates, see the following article:

Creating Custom Xcode 4 File Templates

+2
source

Incompatibility of languages โ€‹โ€‹aside, one of the reasons to avoid the project completely .mm is that you may be tempted to start sections of your methods in C ++, which will lead to a project written in a (relatively obscure) hybrid of two languages, and there will be understand only those who know both. (I did it before)

A good way to avoid cluttering your obj-c headers with C ++ is to declare instance variables in your implementation file (which is allowed with xcode 4.2 / clang 3.0, possibly earlier). For instance:

 @implementation MyClass { std::vector<int> myVector; } 

This helps to maintain common ground between objective-c and C ++ with minimal values.

+1
source

I use the .mm file exclusively for my iOS code and have never had any problems. Yes, my compilations are a bit slower since a clean compiler takes 15 seconds versus 10 seconds. At least on iMac this is not essential.

+1
source

Source: https://habr.com/ru/post/1392977/


All Articles