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

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

Related

Mix C and C++ with CMake

I have a project written in C (on Linux) and now want to use a third-party C ++ library that provides .h and .c source files.
I renamed the .c file to .cpp and then include this library in the C project. However, the following error appears when compiled:
unknown type name ‘class’
Added: The third-party library is here https://github.com/0xmalloc/c-log
The author say it's friendly to both C and C++
There are two options here:
you make your whole project a C++ one - this doesn't mean you need to convert your C code to C++ but rather that you will probably have to touch this and that part of it and also go for a C++ and not C-only compiler
provide wrappers - usually if you write C++ code in C style (no classes etc.) you will only have to do that extern "C" void foo() routine for your C++ functions. However the moment you start working with features that C doesn't support you will have to provide wrappers for your C++ functionality so that it can be exposed to the C part of your application. Such procedure can be from very easy to incredibly complex. For example many modern C/C++ API also provide Python API. In order to do that without having to rewrite everything in Python the authors create wrappers that translate the C/C++ functionality to Python. Depending on how well this is done features of the target language (Python in case we go from C/C++ to Python) can be used to allow better error handling, type checking, readability, native (to the target language) data containers etc.
I do believe that the author of the library misled you since the library is clearly for C++ (the classes in the header are definitely the most obvious thing that just screams C++).

Use C++ library from Swift in iOS App

I want to use a C++ library in Swift: GiNaC. I have seen tutorials on how to use C++ libraries in Swift but I do not know if this will work for iOS or how to deal with dependencies. GiNaC has CLN as a dependency. How to achieve this?
C++ .h files cannot be imported in a Swift Bridging Header because Swift is not compatible with C++ only with C or Objective-C
You will probably need to use an Objective-C wrapper you create to call the C++ functions, then Swift can call the methods exposed by your Objective-C wrapper.
This thread is relevant : Can I mix Swift with C++? Like the Objective-C .mm files

Using Swift with Qt

We can, quite easily, use Objective-C with C++ in Qt.
Having watched many of Apple's WWDC 2015 talks, I expect that Swift will supersede Objective-C for OS X (and iOS) development; all the demonstrations used Swift.
Considering that you can use Objective-C and Swift together, with a bridging header, is it possible to compile Swift code in a Qt project and access Swifts first class objects (Classes, Structs, Enums etc) with C++?
If it is possible...
Calling an Objective-C function from Qt requires wrapping the code in a C function, with a C header to be called from Qt.
Calling Swift from Objective-C requires a bridging header to denote which Swift files are available. This header is then referenced in an XCode project; can we do this in a Qt .pro and if so, how?
Assuming we can specify the bridging header, we've still only made it possible to call Swift from the Objective-C files, but can Swift be called directly from Qt, in C++?
Calling an Objective-C function from Qt requires wrapping the code in a C function, with a C header to be called from Qt.
That's not quite true, Obj-C and Obj-C++ functions and methods can be called directly from Obj-C++. Given that Obj-C++ is (mostly) C++, the interfacing between Qt and Obj-C/C++ is trivial. You can simply name your Qt implementation files .mm instead of .cpp! You can call Qt or standard C++ directly from Obj-C method implementations, compiled as Obj-C++ files (.mm, not .m), and vice-versa.
There's a way to coax the swift compiler to generate a bridging header for you, and this could be integrated into the .pro file as a custom compiler or a custom target.

Add code to both iOS/objective-C project and c++ library at once?

I have a usual Objective-C project and MonkSVG li which contains C++ classes.
I want to add some common functions/methods which I could use in both Objective-C and C++ code (for example work with regular expressions). But I don't want to import C++ and .mm classes because for example Xcode doesn't support refactoring in such code.
Currently I duplicate the same functions in both Objective-C and C++ code. Potentially I could write these classes in ANSI C code and extend MonkSVG library, but this language is too limited in comparison with Objective-C and C++.
So are there better ways to resolve this issue?
If you don't want to use Objective-C++ (.mm files) you'd have to go down to the next common level:
Plain C code.
If you're simplied worried about not having refactoring in Xcode for Objective-C++ code you could still use Xcode and at the same time edit your code in something like AppCode.
It understands Xcode project files and does all the refactoring you could want.

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.