Is it possible to only use C syntax in a .mm file? - c++

Is it possible to only use C syntax in a .mm file?

There's nothing wrong with writing .mm file that works in terms of plain old functions, strings and arrays, rather than the additions made by objective-c and c++.
However, there are a few cases where valid C isn't valid C++ (or Objective-C++) which you'll want to consider (http://www.parashift.com/c++-faq-lite/big-picture.html#faq-6.11).
Also, if you're using Objective-C++ files in the same project as Objective-C, then you need to be careful not to include any C++-only stuff in headers you reference from vanilla Objective-C files. You'll also need to make sure that you export any C-style functions defined in a .mm propertly if you want to use them in a .m (http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html#faq-32.6).

Related

OpenCV with Objective C vs Objective C++

When using OpenCV for iOS projects, most articles on the internet recommends changing .m to .mm.
However, since Objective C is a strict superset of C++, why do we need to do this change? Even worse, without changing .m to .mm, #import <opencv2/opencv.hpp> fails
Objective-C is not a superset of C++. Changing .m to .mm changes the file from Objective-C to Objective-C++, which is a superset of C++.
The compiler need to know what type of code is dealing with.
By using .mm the compiler expects Obj-C and C++.
By using .m it only expects Obj-C
The compilation is handled differently according to the file extension.

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

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.

Using a C++ library in an Objective-C app?

I am planning on learning Objective-C to write an OS X application but it will depend on a library written in C++. Can I interface with C++ in an Objective-C app? I'm new to desktop development.
The C++ library will be used simply to analyze a file and return some data about that file. For example, in the libraries compiled example, in terminal you'd type
./xlsanalysis my_spreadsheet.xls
and it returns:
rows: 34
columns: 10
first row: "My Spreadsheet header"
Can I include this library directly into the Objective-C app or interface with it some how?
For this purpose, there is Objective-C++, e.g. Objective-C plus C++ (or vice versa). From Objective-C++ files (e.g. .mm files), you have full access to all C++ functionality. Be careful when casting types from C++ to Objective-C, e.g. you should convert a C++ string to a NSString by using something like [NSString stringWithCString:cPlusPlusString.c_str()] The other direction would be string cPlusPlusString([objectiveCString cString]) (or cStringUsingEncoding:).
Yes, you'll just need to switch any Obj-C files that include (directly or indirectly) C++ content into objective-c++. Basically that just means changing the extension to .mm -- this will give you the ability to use C++ and Obj-C together in those files.
Objective-C++.
http://www.google.com/search?q=%22objective-c%2B%2B%22
How well is Objective-C++ supported?
There is really something like Objective C++?
How much of C++ is supported in Objective-C++