Search code examples
c++swiftuibezierpatharmadillodylib

Using Armadillo C++ library in Swift 5


I'm trying to use Armadillo C++ library in my swift code to create sinusoidal curved arrow. Earlier it worked well with Objective C. But when I'm trying to do the same implementation in Swift, it's showing 'armadillo' file not found error.

I've downloaded the file from https://github.com/gadomski/armadillo/tree/master/branch-5.600/include path and copied both armadillo_bits folder and armadillo file into the project.

I've created a Objective C++ Wrapper around the C++ class too.

Objective C++ Wrapper DrawSinusoidal.h file

#import <Foundation/Foundation.h>

@interface DrawSinusoidal : NSObject

+(NSArray *)bezierPathsForPoints:(NSArray *)points;

Objective C++ Wrapper DrawSinusoidal.mm file

#import "DrawSinusoidal.h"
#import "DrawSinusoidalMath.h"

@implementation DrawSinusoidal

+(NSArray *)bezierPathsForPoints:(NSArray *)points {
 ...
}

C++ file - DrawSinusoidalMath.h

#include "armadillo"

std::vector<std::vector<arma::vec2>> bezierPathsForPoints(const std::vector<arma::vec2> &points);

C++ file - DrawSinusoidalMath.cpp file

#include <iostream>
#include "DrawSinusoidalMath.h"

using namespace arma;

std::vector<arma::vec2> bezierPathsForPoints(const arma::mat &tValues, const std::vector<arma::vec2> &points)
{
... 
...
 return points
}

Solution

  • Finally I found the solution to it. Sharing the steps which I followed.

    • We need to install the pre-built Armadillo packages to macOS which can be installed via MacPorts or HomeBrew
    • I installed using HomeBrew.

    $ brew install armadillo

    Once it is completed, please keep a note on the installed path from the last line. enter image description here

    In my machine, it is installed on path /usr/local/Cellar/armadillo/10.5.1

    • Next, we need to provide the Header Search Paths. Headers are available in below location, so just copy the path and paste in Xcode.

    /usr/local/Cellar/armadillo/10.5.1/include/armadillo_bits

    enter image description here

    • Next, we need to link the libarmadillo.dylib library that is available in the installed path to the sdk path in the Xcode. Open terminal and type following command.
    sudo ln -s /usr/local/Cellar/armadillo/10.5.1/lib/libarmadillo.dylib /Applications/Xcode_12.4.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS14.4.sdk/usr/lib/libarmadillo.dylib
    
    • Once done, now you can go to Targets > Build Phases > Link Binary with Libraries You should see libarmadillo.dylib Select and add it to your project.

    • Final step.. Copy armadillo text file and armadillo_bits folder, from below path to the project. /usr/local/Cellar/armadillo/10.5.1/include enter image description here

    That's it.. The setup is over and you can use the Armadillo library in C++ files inside Objective-C/Swift projects.