将 C++ 代码添加到 iOS 项目 [英] Adding C++ code to an iOS project
问题描述
我正在尝试向 iOS 项目添加 C++ 库.我将源代码文件添加到项目中,但似乎它们不像 C++ 代码那样被解释.
I'm trying to add a C++ library to an iOS project. I added the source code files to the project, but seems like they are not interpreted like a C++ code.
例如,我在头文件中收到以下错误:
For instance, I get the following error in a header file:
namespace soundtouch // Unknown type name 'namespace'
{
我已经尝试将文件检查器中的类型更改为C++ Source"和C++ Header"——没有任何改变.
I already tried to change the type in the File inspector to "C++ Source" and "C++ Header" - nothing changed.
如何将 C++ 库导入 XCode 项目?
How can I import a C++ library to an XCode project?
推荐答案
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.
这篇文章值得一读:
这篇关于将 C++ 代码添加到 iOS 项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!