Use C++ library from Swift in iOS App - c++

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

Related

swift compiler not able to find c++ libraries

I created two single view iOS projects from Xcode. The first one in Swift and the second one in Objective-C.
In those two projects, I add both Test.cpp / Test.hpp files which only include C++ library. Of course, for the Swift project only, I have to create a Bridging Header to import this file.
Test.hpp file looks like this:
#ifndef TestCpp_hpp
#define TestCpp_hpp
#include <stdio.h>
#include <cstdlib>
#endif /* TestCpp_hpp */
The issue I have is that the Objective-C project is building successfully, meanwhile the Swift project fails to find the cstdlib C++ library.
I checked the compilation command and the Swift project uses swift to build. The Objective-C project uses clang.
I tried to change the C++ Language Dialect and C++ Standard Library in the build settings but nothing is working.
Is there some special setup to do in order to be able to build the Swift project?
You cannot include C++ headers in your bridging header to call C++ code directly from Swift. Essentially, there are at least two options.
Write a C wrapper around C++ code and incorporate the C wrapper into
Swift via the bridging header.
Write an Objective-C++ wrapper with an API that does not involve C++
types. Internally the wrapper implementation can call C++ code. Invoke your wrapper API in Swift like you would invoke any Objective-C code.
Both of these approaches are described here:
Interacting with C++ classes from Swift

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.

Using a c++ static library (CCFits) in Xcode iOS Project

I am trying to using the CCfits library in an iOS project. CCfits is a C++ wrapper to the c library cfitsio.
I have the source code to both of these, and also have them built as static libraries (.a files). I would like to add the CCfits library (libCCfits.a) to an iOS project but when I am confused on how to access the library classes from my iPhone source files. I have the library added under "Link Binary with Libraries".
I understand that Xcode needs me to import a header file, but what is the proper way to attach the headers to the project? Do I need to make a framework file somehow? Or is the .a file enough?
You can easily mix C++ and Objective-C using Objective-C++. In XCode just rename your .m files to .mm and it will recognize the files as Objective-C++. Then you can use the C++ classes in your Objective-C code (some restrictions apply, but not many).
Since Objective-C is an extension to C you can use C libraries in any Objective-C program easily.
See https://stackoverflow.com/posts/10156243/edit

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