Xcode .m 与 .mm

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4562255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 03:38:17  来源:igfitidea点击:

Xcode .m vs. .mm

xcode

提问by Nickolay Olshevsky

In the zxing iphone project the readme states:

在 zxing iphone 项目中,自述文件指出:

  • It can happen that when trying to build your own project with ZXingWidgetController you get linker errors like "undefined reference to". If this error looks like a c++ undefined reference, then renaming main.m into main.mm (Objective-C++ source suffix) may fix the problem
  • 当尝试使用 ZXingWidgetController 构建您自己的项目时,可能会出现链接器错误,例如“未定义的引用”。如果这个错误看起来像一个 c++ 未定义的引用,那么将 main.m 重命名为 main.mm(Objective-C++ 源代码后缀)可能会解决这个问题

It did indeed. But I'm wondering why?

确实如此。但我想知道为什么?

回答by Nickolay Olshevsky

.mm extension stands for Objective-C++, when compiler can process C++ classes. But when using .m extension it will be able to compile only C code, without C++ classes.

.mm 扩展名代表 Objective-C++,当编译器可以处理 C++ 类时。但是当使用 .m 扩展名时,它只能编译 C 代码,而没有 C++ 类。

回答by schmijos

Both .mand .mmare class file extensions of source code for Mac-based applications. .mfiles can contain both Objective-Cand Objective-C++classes. To avoid conflicts between the two in mixed-use scenarios there's the convention to rename all Objective-C++class files to .mm. This helps compilers to distinguish.

这两个.m.mm是源代码为基础的Mac应用程序类的文件扩展名。.m文件可以包含Objective-CObjective-C++类。为了避免混合使用场景中两者之间的冲突,约定将所有Objective-C++类文件重命名为.mm. 这有助于编译器区分。

So in a project which uses both Objective-Cand Objective-C++you will see:

因此,在同时使用Objective-CObjective-C++的项目中,您将看到:

  • .mfiles containing Objective-C
  • .mmfiles containing Objective-C++
  • .m包含Objective-C 的文件
  • .mm包含Objective-C++ 的文件