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