且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将 C++ 代码添加到 iOS 项目

更新时间:2021-09-10 02:12:53

C++ 源文件必须具有可识别的扩展名;.cpp.cxx.cc 等,它们将被编译为 C++ 文件.如果在添加文件时扩展名正确(并被识别),则不需要手动更改文件类型.编译语言是在每个模块的基础上确定的.

C++ source files must have a recognised extension; .cpp, .cxx, .cc etc. and they will be compiled as C++ files. You shouldn't need to change the file type manually if the extension is correct (and recognised) when you add the file. The compilation language is determined on a per-module basis.

混合 C++ 和 Objective-C 是另一回事.ADC 文档中有一个关于 Objective-C++ 的完整部分(使用 .mm 扩展名).因此,如果您想从 Objective-C 调用 C++ 代码,则需要从 .mm 模块中完成.

Intermixing C++ and Objective-C is a different story. There's a whole section in the ADC documentation on Objective-C++ (which uses the .mm extension). So if you want to call C++ code from Objective-C, it will need to be done from a .mm module.

鉴于您引用的错误,看起来您可能试图在 Objective-C 模块中包含 C++ 头文件.为此,您需要将 Objective-C 模块重命名为 .mm,然后编译器会将其视为 Objective-C++.

Given the error you quoted, it looks like you're probably trying to include a C++ header file in an Objective-C module. To make this work, you need to rename the Objective-C module to .mm and then the compiler will treat it as Objective-C++.

确定 C++ 和 Objective-C 代码之间的清晰边界需要一些计划,但值得预先考虑.

It takes a little planning to determine a clean boundary between the C++ and Objective-C code, but it is worth some up-front thinking.

这篇文章值得一读: