5

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?

2
  • Are You uses .m or .mm files? Commented Oct 2, 2013 at 13:39
  • Used .m before. Changed to .mm - it worked! Commented Oct 4, 2013 at 8:17

2 Answers 2

7

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:

Sign up to request clarification or add additional context in comments.

Comments

5

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.