Sergey
Sergey

Reputation: 49728

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?

Upvotes: 5

Views: 4837

Answers (2)

gavinb
gavinb

Reputation: 20018

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:

Upvotes: 7

trojanfoe
trojanfoe

Reputation: 122381

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.

Upvotes: 4

Related Questions