从不创建 ObjC 文件但始终创建​​ ObjC++ 的任何缺点;文件代替?

发布于 2024-12-29 05:07:43 字数 705 浏览 6 评论 0原文

默认情况下,当您请求新的 ObjC 类时,Xcode 会同时创建 .h 和 .m 文件。 一切工作正常,直到您需要引用项目中其他位置的任何 C++ 文件并开始将其 #import 到您的 .h 或 .m 文件

中,此时,ObjC 编译器完全混乱并抛出大量解析错误,并且您用户(也就是说:我)会变得更加困惑,直到我想到:当然,我应该将该文件改为 ObjC++ 文件。

选项是:

  1. 告诉 Xcode 这个特定文件,即使它是一个 .m 文件 实际上是一个 ObjC++ 文件,或者
  2. 将该文件重命名为 .mm。

第一个选项对我来说不太合适,因为无论项目认为它是什么,该文件实际上是 ObjC++。

第二个选项也不好,因为它搞砸了 Git 存储库,然后“忘记”曾经有另一个 .m 文件,它实际上是这个“新”.mm 文件的历史记录。

因此,我决定从现在开始,在创建 Xcode 为我创建的任何 .m 文件后,首先将其重命名为 .mm,这样我就不会丢失历史记录。

到目前为止,它对我来说效果很好,但我脑子里有一个轻微的担心,可能存在一些极端情况,我真的想要一个 ObjC 文件而不是 ObjC++ 文件。

那些极端情况会是什么?任何人都知道任何 ObjC++ 文件恰好不包含任何 C++ 引用,但会以某种方式阻塞 ObjC 编译器,仅仅因为它是 .mm 文件?

如果没有缺点,为什么不永远弃用 .m 并坚持使用 .mm 呢?

By default Xcode creates both an .h and and an .m file when you ask for a new ObjC class.
Everything works fine until you need to refer to any C++ file elsewhere in your project and start #import ing it into either your .h or .m file

At that point, the ObjC compiler gest utterly confused and throws mountains of parsing errors, and you the user (that is to say: me) get even more confused until such time it hits me: of course I should make that file an ObjC++ file instead.

The options are:

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

The first option is not very palatable to me, because that file really is ObjC++ regardless of the what the project thinks it is.

The second option is not good either as it screws up the Git repo which then 'forgets' that there used to be another .m file which really is the history of this 'new' .mm file.

So I have decided from now on to always rename any .m file that Xcode creates for me to .mm first thing after creating it so that I won't loose the history.

It has worked well for me so far, but I have this slight worry in my head, that there might be some corner case where I would really want to have an ObjC file and not an ObjC++ file.

What would those corner cases be? Anyone is aware of any ObjC++ file which happens to NOT contain any C++ reference but would choke the ObjC compiler in some way, just by virtue of being an .mm file?

And if there are no downside, why not just deprecate the use of .m forever and stick to .mm instead?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

慈悲佛祖 2025-01-05 05:07:44

撇开语言不兼容不谈,避免完全使用 .mm 项目的原因之一是,您可能最终会忍不住用 C++ 启动方法的某些部分,这将导致项目以两种语言(相对晦涩的)混合形式编写,并且将只有同时了解两者的人才能理解。 (我之前已经这样做过)

避免用 c++ 弄乱 obj-c 标头的一个好方法是在实现文件中声明实例变量(从 xcode 4.2/clang 3.0 开始,可能更早)是允许的。例如:

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

这有助于最大限度地减少 Objective-C 和 C++ 之间的接触点。

Language incompatibilities aside, one reason to avoid an entirely .mm project is that you might end up being tempted to start sections of your methods in c++, which will result in a project written in a (relatively obscure) hybrid of two languages, and will only be understood by people who know both. (I have done this before)

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

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

This helps to keep the points of contact between objective-c and c++ minimised.

野鹿林 2025-01-05 05:07:44

我专门将 .mm 文件用于我的 iOS 代码,并且从未遇到过任何问题。是的,我的编译速度有点慢,干净的编译需要 15 秒,而干净的编译则需要 10 秒。至少在 iMac 上,这并不重要。

I use .mm file exclusively for my iOS code and have never had any issues. Yes, my compiles are a little slower in that a clean compile takes 15 seconds vs 10 seconds. At least on a iMac, it's not significant.

楠木可依 2025-01-05 05:07:43

解析 C++ 比解析 ObjC 慢得多,因此 ObjC++ 文件的编译时间明显更长。我不确定这种开销是否适用于不包含 C++ 的 ObjC++ 文件,但它在一定程度上意味着它更难解析,因为编译器需要查找 C++ 构造。

此外,C++ 类型系统有一些与 C 略有不同的规则,这些规则也适用于 C++ 文件中的 ObjC/C 代码。我不记得细节了,但这不会有什么坏处;只是可能需要一些额外的演员。

Parsing C++ is much slower than parsing ObjC, so ObjC++ files have significantly longer compile times. I'm not certain if this overhead will apply to ObjC++ files that contain no C++, but it would make a certain amount of sense that it's harder to parse just because the compiler needs to look for C++ constructs.

Also, the C++ type system has a few slightly different rules from C, that are applied to ObjC/C code in a C++ file as well. I don't recall the details, but it's not going to be harmful; just might require a few extra casts.

寂寞笑我太脆弱 2025-01-05 05:07:43

没有什么缺点,虽然有两个代表您创建的方法(.cxx_construct.cxx_destruct),但它们仅用于在您创建和销毁 C++ 对象时使用创建/释放一个实例。如果您的类没有 C++ 成员,这些函数不会执行任何操作,只会增加非常低的开销。否则,您仍然会为 Objective-C 方法生成 C 函数,而不是 C++ 函数。

There is no downside, although there are two methods created on behalf of you (.cxx_construct and .cxx_destruct), but they are only used for crafting and destroying C++ objects when you create/dealloc an instance. If your class has no C++ members, these functions do nothing and add only an really extremely low overhead. Otherwise, you still have C functions generated for your Objective-C methods, not C++ functions.

行至春深 2025-01-05 05:07:43

创建 Objective-C++ 文件模板,以便在创建新文件时获得 .mm 文件而不是 .m 文件。复制 Apple 的 Objective-C 类模板并将 .m 文件重命名为 .mm。有关创建 Xcode 4 文件模板的更多详细信息,请参阅以下文章:

创建自定义 Xcode 4 文件模板

Create an Objective-C++ file template so you get a .mm file instead of a .m file when you create a new file. Make a copy of Apple's Objective-C class templates and rename the .m files to .mm. More detailed information on creating Xcode 4 file templates is available in the following article:

Creating Custom Xcode 4 File Templates

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文