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
Xcode .m vs. .mm
提问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 .m
and .mm
are class file extensions of source code for Mac-based applications. .m
files 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-C和Objective-C++类。为了避免混合使用场景中两者之间的冲突,约定将所有Objective-C++类文件重命名为.mm
. 这有助于编译器区分。
So in a project which uses both Objective-Cand Objective-C++you will see:
因此,在同时使用Objective-C和Objective-C++的项目中,您将看到:
.m
files containing Objective-C.mm
files containing Objective-C++
.m
包含Objective-C 的文件.mm
包含Objective-C++ 的文件