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
Related
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 am programming in Xcode in ObjC using a Cocoa static library built in c++ (built by me). For the library project, I made one public header(.hpp) that includes all the other header files(.hpp). Then in the ObjC project, I wrote a wrapper code(.hpp and .mm) to play as the interface, so it includes the public hpp header. To access the library, I then include the wrapper code in a normal m file. I believe all of these followed the tutorials, as it worked well for a few weeks.
However, after doing something stupid yesterday, the compiler for the ObjC project started to insist on building all the headers in ObjC. It first had problems with openCV packages in the c++ library, producing errors saying thoses are supposed to be built in c++ (as shown below). Even if I removed the openCV, the compiler cannot find any standard c++ libararies. I tried renaming the .hpp's to .h but it did not work.
Any suggestions on what I did wrong and how I can fix this?Much appreciated!
()
Your errors are coming from compiling ImageProcessor.m. .m is the extension Obj-C files, so the compiler will try to compile it, and everything it includes as Obj-C.
I'd suggest renaming it to ImageProcessor.mm.
After learning more about header files, I realized what I did wrong: I included every header in the interface, so they are re-built every time outside the library, even if they are referenced by none-c++ code. The solution is to separate the interface header with other headers in the library completely.
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.
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.
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++.