Adding C++ code to an iOS project - 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'
{
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?

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

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.

Related

How can I check if a cpp file is being compiled in an Objective-C++ project using the preprocessor?

I have a C++ source file (.cpp) which is shared between two of my projects, one using Objective-C++ and the other just using plain C++. I have a part of that file where I need to use different code for the Objective-C++ project and for the plain C++ project, so I need to check if the project is being compiled using Objective-C++ or not. After some searching, I found that the __OBJC__ macro can be used to check if the file is compiled by an Objective-C compiler, so I tried this code:
#ifdef __OBJC__
//Objective-C++ specific code here
#else
//Plain C++ specific code here
#endif
The problem is that the __OBJC__ macro is never defined, not even in the Objective-C++ project. I also noticed that the __OBJC__ macro is defined in the .mm files in the Objective-C++ project, but not in the .cpp file.
In case it matters, I'm compiling the Objective-C++ project for iOS with Xcode, and the plain C++ project is a cross-platform computer program which can be compiled using Visual Studio, Xcode or GCC.
How can I check using the preprocessor if my .cpp file is being compiled in an Objective-C++ project or a plain C++ project?
The problem was that by default, Xcode determines the file type by the extension and not by the project, so it thought that the .cpp file was a plain C++ file even though it's part of an Objective-C++ project, so it didn't define the __OBJC__ macro in that file. The solution was to change the compilation mode by right clicking on the .cpp file in the file list in the left part of Xcode, click on "Show File Inspector" and in the "Type" dropdown in the right side of Xcode select "Objective-C++ Source" instead of "Default - C++ Source":
This will make Xcode think that the .cpp file is an Objective-C++ file so it will define the __OBJC__ macro.
Since this is a per-project setting, it only has effect on the Objective-C++ project and does not change anything in the plain C++ project, so #ifdef __OBJC__ will be a way to check which project the file is being compiled in.
Thanks to Josh Caswell for giving me the idea in this comment.

Import C++ code to IOS application [duplicate]

This question already has answers here:
How can I use C++ with Objective-C in XCode
(2 answers)
Closed 8 years ago.
I write a code in C++, and I want to use some of it's methods in my IOS application, so is it possible to (import) C++ library "t.cpp" to IOS application in XCode?
if yes what's the simple way to do it?
Yes, that will work without error, however you might want to change the Xcode C++ compiler settings (language level and runtime library), depending on the version of iOS you are using.
When it comes to actually using the C++ classes within your Objective-C code, you simply need to rename any files from .m to .mm.
A complication occurs when you want to include the C++ headers in Objective-C headers where both .m and .mm see that Objective-C header file. In this case you might find that you need to change many more files from .m to .mm in order for this to work, but there are ways around this if that becomes too onerous.
Just add the file to the project.
You need to rename a file from .m too .mm only if the translation unit contains a C++ header. Then, the module gets "infected" by C++ and needs to be compiled with the Objective-C++ compiler.
This is only required if any C++ header will be directly or indirectly included/imported from that module. There is no need to rename all files.
Additionally, if this C++ code depends on the C++ standard lib, you need also ensure, that the executable binary links against the C++ standard lib, via setting the appropriate build setting in the executable binary.
For example, in the target build settings of your app, add the following option to Other Linker Flags:
-lc++
e.g.:
OTHER_LDFLAGS = -ObjC -lc++
Caution:
If possible, don't include/import a C++ header in a public Objective-C header, since then all modules will be infected by C++ when they import this Objective-C header and become Objective-C++.

Many C++ compile errors in iPhone project

i am working on a C++ file in Xcode 4.5 on my iPhone application.
for some reason the compiler do not accept C++ statements, it gaves me many errors.
why does this happen?
any help would be appreciated!
thanks
CAStreamBasicDescription.h and CAXException.h contain C++ code. They can only be included from C++ or Objective-C++ source files.
The errors indicate that the file that is including those two headers is not being compiled as C++ or Objective-C++. The references to Class within the errors suggest that it's being compiled as plain Objective-C instead. Ensure that the file has a .mm file extension if you do intend it to be compiled as Objective-C++, and check that the Type is not set to a non-default value in the Identity and Type section of the File inspector.

Objective C Project using C++ POSIX Classes

I have to create a iOS Programm using Code of some C++ POSIX Classes.
I already read the "Using C++ With Objective-C" manual of the Apple Developer Center.
They describe how to mix C++ & Objective C code in a .mm file.
My question is, is there any possibility to use the C++ Classes in my .h/.m files of a normal Objective C Project?
Or is it necessary to write the whole Project in that .mm file style with its own main?
You can combine in a project any types of files, say .c, .m, .cpp and .mm, and the compiler is chosen automatically depending on the extension. For example, you can keep the standard main.m file which comes with the XCode template, and add your new .h and .mm files to use Objective-C++.
In other words, there's no distinction between a normal Objective-C project and a Objective-C++ project. You just have to use .mm extension for the specific files which needs Objective-C++. This can be used in any project.
You have to force compilation of the Obj-C files in which you want to use C++ to Obj-C++ in the build menu. You can then create and use C++ objects in your Obj-C classes.
It depends on how you want to use the C++. In my projects I usually only make a few calls out to do some heavy lifting, etc.
What I do is have C++ in .cpp files, then create a few .mm files that have headers that have no C++ in them. These .mm files are obj-c wrappers for the C++. Then the C++ is 'contained' to the original posix files, plus a few files that give the C++ classes and calls all the interface you need. As few .mm files as possible is a good thing.
Keeping the C++ out of most of your code makes debugging, etc easier.
--Tom
Select Project directory in Xcode, In Build Settings tap, choose "Compile Sources As" is Objective-C++. No need change name type from .m to .mm.

include objective-c header in c++ file

Is there a way to include an objective-c header from a cpp? Because when I try to #include "cocos2d.h" from a cpp .h file, a lot of errors complaining about #'s and -'s are showing up.
Can c++ files include obj-c headers like that?
It is possible, but you need to use Objective-C++ (e.g. by making the file extension .mm) to mix the languages, plain C++ sources don't work.
To make that clear:
.m files only allow Objective-C sources
.cpp files only allow C++ sources
.mm allow mixed Objective-C++ sources - i.e. both Objective-C and C++ with some limitations
If you need to keep the two worlds seperated instead you need to write wrapper classes that hides the Objective-C details from C++ and vice versa.
C++ has no idea what Objective-C is. So including an Objective-C .h in a .cpp is a no-go.
The other way around, though is fine, if you use the .mm file extension (Objective-C++) instead of .m (Objective-C).
It is possible when you are compiling with mixed objc/c++. Cocoa applications can be written in languages mix in both directions: you can either use an obj-c class inside the C++ or a C++ class inside a obj-c object.
I assume in your case you are compiling pure C++ app where the obj-c code is not allowed.