Integrating Box2d in Swift project gets cassert file not found error - c++

I want to integrate Box2d in my OS X application, so I've used the following Podfile to grab it:
pod 'box2d'
And the version of box2d is 2.3.0. In the xcworkspace I got from pod install, I've created a bridge header to interoperate with the C++ APIs (according to doc, developer cannot import C++ projects directly from Swift lang, you should create a ObjC bridge).
And when I hit build button, I got a compiler error:
<unknown>:0: error: /path/to/project/Pods/Headers/Box2D/Common/b2Settings.h:22: 'cassert' file not found
So I wonder how can I fix this?

Finally I figured it out by myself.
I've already created the bridging file as mentioned in apple's doc, but the content I was putting there was:
//
// Use this file to import your target's public headers that you would like to expose to Swift.
//
#import <Box2d/Box2d.h>
This is the root for the compiler error. So, I have to create a new Objective-C class, rename the .m file to .mm, and put the line of import in the .mm file. NOTE: putting the import line in .h file does not solve the compiler error.
And that's it, now it compiles happily.

Related

Xcode create bundle with cpp files

I'm trying to create a bundle application using Xcode 10.3.
The main code is going to live inside a C++ file, which I added to the bundle project, and I also need to use some Cocoa related stuff.
and here is the catch, anytime I include
#include <Cocoa/Cocoa.h>
I immediately get like 20 errors
which are rather strange. Is there anything else to be added or set in Xcode so I can include Cocoa headers inside a CPP file?
Looks like the solution was easier than expected, you just have to change the compilation type for the cpp file from C++ Source to Objective-C++ Source and then everything compiles as needed.

Could not build module 'opencv2' , when using OpenCV in Swift Project

I have added opencv framework through cocoapods. I added required frameworks to Linked Frameworks and Libraries as well.
As, I am using Swift. So, I made a bridging header with "Wrapper" NSObject in Objective C. Problem is that when I try to import these in Wrapper.h, it gives me error
#import <opencv2/opencv.hpp>
#import <opencv2/imgcodecs/ios.h>
Error is:
Could not build module 'opencv2'
Where as if I import them in Wrapper.mm they work fine. Kindly tell me how to use them in header file. As I need to call a few variables from header file.
When bridging header files to Swift, the files have to be pure Objective-C++. So you cannot include any opencv headers in Wrapper.h, they have to go in Wrapper.mm. For more detail how to write and use a wrapper, see my answer here.

How to use Superpowered lib in Swift project

I want to make an app in Swift that simply record via the mic of the iPhone and then play the sound recorded.
For that, I'd like to use the lib Superpowered that is a static library with header files.
For that, I tried to drag and drop the .a and headers files in my project (Xcode create for me a bridging header file), add the .a in "Linked Frameworks and Libraries" in Xcode > Target > General (and so in Xcode > Target > Build phases > "Link Binary With Libraries" too) and index the .h files path in Xcode > Target > Build Settings > Search Paths > Library Search Paths.
But with that I have the error "ld: symbol(s) not found for architecture arm64"
Exactly the same as here XCode: Undefined symbols for architecture arm64 error and I tried all the solutions that I found on the web for that, still no way to compile.
SO !
Superpowered gives us a sample app to show how to mix Objective-C++ and use their lib within a Swift project (Here's a link to the git if you want https://github.com/superpoweredSDK/Low-Latency-Android-Audio-iOS-Audio-Engine).
Here is some screenshots of the sample project with what I understand and what I don't :
The Bridging-Header-File with the prototypes of the methods of the lib that I want to use within my Swift code (I don't like this but if it's the only way...).
The viewController file where the code in Swift is, and where I can create a Superpowered object thank's to the Bridging-Header-File, and call the methods that I've put in it.
And wtf I don't even understand why this Objective-C++ file is here and what it contains. It comes out from nowhere, not even their lib files.
So with this sample project in mind, I've created my own project, here is some screenshots :
The same Bridging-Header-File that in the sample project except that I include SuperpoweredIOSAudioIO.h so I can use SuperpoweredIOSAudioIODelegate.
My viewController file where the code in Swift is, and where I can create a Superpowered object thank's to the Bridging-Header-File, and call the methods that I've put in it.
Until here, it's great, except that I can't for exemple create a SuperpoweredRecorder object. If I try to include the SuperpoweredRecorder.h file in my Bridging-Header-File I have these errors :
So I saw that it is because SuperpoweredRecorder.h includes some .cpp files and I have to create a wrapper for cpp (a little bit like I did with the bridging header, no ?) but that includes a .h and a .mm file and I don't know what I have to put in that .mm file (the code of SuperpoweredRecorder.cpp ? But I don't have access to it)
So yes, I'm a little bit confused with all that stuff, can you help me to understand how can I use all the Superpowered lib in my Swift project please ?
As I said in comment to #OmniProg, I had a little conversation with the CTO of Superpowered that helped me a lot to find the solution below.
So, as Swift cannot interact directly with C++ but can with Objective-C, I had to create objects in Objective-C++ (.mm file, a mix between C++ and Objective-C) that wrap C++ classes of the lib Superpowered.
Here is an example with the SuperpoweredRecorder object from the lib :
Here I create a .h file where I prototype my wrapper with the name SuperpoweredRecorderWrapped, and I also prototype in it all the methods of the SuperpoweredRecorder of the lib that I want to use.
Then I create a new .m file that I rename .mm and I implement SuperpoweredRecorderWrapped on it.
I import both SuperpoweredRecorderWrapped.h and SuperpoweredRecorder.h.
I create a SuperpoweredRecorder object as property named _wrapped and in my methods, I call the corresponding method of the _wrapped object.
With that, when I'll call start of a SuperpoweredRecorderWrapped in my Swift code, this one will call start of _wrapped, a SuperpoweredRecorder object. See the trick ?
And finally I include all the wrapped classes in my Bridging-Header, like that I can instantiate my wrapped objects from Swift.
NOTE: All the C++ code HAVE to be in .mm files, that's why I make my #include of .h that contains C++ code in my .mm file and not in my .h file.
I haven't programmed in Objective-C++, but I do have experience with C, C++, Objective-C, and Swift, so here are some observations and ideas based on looking at the Superpowered SDK and sample code.
A bridging header lets Swift interface directly with Objective-C, and since Objective-C is a strict superset of C, this implies interfacing with C as well. However, you cannot interface directly with C++, and that's where Objective-C++ comes to the rescue. From what I'm seeing you can mix Objective-C and C++ code in Objective-C++, which allows Objective-C to use C++ classes.
Now on to some specifics.
The bridging header in the SuperpoweredFrequencies example that you looked at introduces a new Objective-C class, Superpowered, which is not part of the library, but of the example, and is implemented in Superpowered.mm. It is an Objective-C++ file, because Superpowered calls some C++ code.
Looking at Superpowered.mm, you will see that it imports Objective-C headers:
#import "SuperpoweredFrequencies-Bridging-Header.h"
#import "SuperpoweredIOSAudioIO.h"
as well as C and C++ headers:
#include "SuperpoweredBandpassFilterbank.h"
#include "SuperpoweredSimple.h"
We could have used import instead of include for C++ code, too, but they are probably using include just to emphasize that it is C++ code. Looking at SuperpoweredBandpassFilterbank.h, we see that SuperpoweredBandpassFilterbank is a C++ class. It is used in Superpowered.mm by the Superpowered Objective-C++ class, see the filters member, which is a pointer to a SuperpoweredBandpassFilterbank object.
In the project you attempted to build, I see where the Superpowered interface is declared in the bridging header, but I don't see an implementation. The failure to #import SuperpoweredRecorder.h is due to SuperpoweredRecorder.h being a C++ header. The #include should be in your Objective-C++ (.mm) file, it's useless in the bridging header, since Swift can't make sense of C++ anyhow.
Hopefully this is helpful. Welcome to the world of C++.
I'm a bit surprised that you would have to provide prototypes of method.
I used Objective C libraries in my Swift project before and I only had to add the header files in my bridging header.
See example from my project. The bridging header contains just that:
// Use this file to import your target's public headers that you would like to expose to Swift.
//
#import "QTouchposeApplication.h"
#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import <FBSDKLoginKit/FBSDKLoginKit.h>
#import <VungleSDK/VungleSDK.h>

Using C++ in swift using a Objective-C Wrapper

I want to build a little project that utilizes the QR Encoder. I simply used Cocoapods to install it within my project and included the QREncoder.h file within my bridging-header, which works just fine for a couple of other Objective-C resources. My problem occurs because I am including the QREncoder.h file which the includes the QR_Encode.h file. This file uses a pretty standard C++ class definition class CQR_Encode{...};, but when I'm trying to compile my code now, I get the following error /PATH_GOES_HERE/Pods/QR-Code-Encoder-for-Objective-C/QRCodeEncoderObjectiveCAtGithub/QR_Encode.h:81:1: Unknown type name 'class'; did you mean 'Class'?. Looking for that error on the internet got me the information, that I need a Objective-C wrapper class for that file, but I thing QREncoder{.h,.mm} should do the job. I also changed file type within the attributes inspector to either 'objective-c++ source' or 'c++ header' but that did not change the error. I also tried setting the 'compile sources as'-setting in build settings, but of course this didn't work since it sets the compile type for all files and since there are Objective-C classes, it won't work. Does anyone have an answer to help me out?

Cannot use a C++ file in Xcode - Cannot Resolve Libs

I am trying to figure out why XCode cannot resolve a C++ file I imported.
I have changed the file extension to .mm
I have added the .h file contents to the top of my file
I have command line tools installed
The code complete detects the algorithm library:
But when I try compile I get this:
Ok I found the issue.
As it turns out you need to not only use the .mm extension to invoke the objective-c++ compiler, but also anything that REFERENCES the .mm too. So I had a view controller that referenced the c++ header.