Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding C++ code to an iOS project

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'
{

I already tried to change the type in the File inspector to "C++ Source" and "C++ Header" - nothing changed.

How can I import a C++ library to an XCode project?

like image 898
Sergey Avatar asked Oct 02 '13 13:10

Sergey


2 Answers

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.

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.

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++.

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.

This article is worth reading:

  • Mixing Objective-C, C++ and Objective-C++
like image 137
gavinb Avatar answered Oct 14 '22 02:10

gavinb


Every Objective-C implementation file (.m) that directly, or indirectly, #imports any of the C++ header files must be changed to an Objective-C++ implementation file by changing its file extension to .mm.

like image 27
trojanfoe Avatar answered Oct 14 '22 01:10

trojanfoe