How to call C++ code from swift

VertoStudio3D · 18K views · 2014 · 8:08

Open on YouTube

C, C++, Rust, Go & systems languagesSoftware (other)tutorialdated

Why Stack Overflow recommends it

Stack Overflow users link it when answering how to mix Swift with C++ or Objective-C++, as a concrete walkthrough of the bridging header and wrapper approach.

Recommended by 4 people across 4 different months, 2015 to 2020.

What it is

An 8-minute screencast from 2014 showing how to wrap modern C++ code so it can be called from Swift, using an Objective-C bridging header and extern "C" declarations. It is a short hands-on tutorial by the channel VertoStudio3D.

What people said

Recommends James Mart · 2015-03 · Stack Overflow · on “Can I mix Swift with C++? Like the Objective-C .mm files”

No, not in a single file. However, you may use C++ in Swift Projects without needing a static library or framework. Like others have said, the key is to make an Objective-C bridging header that #includes C-compatible C++ headers that are marked as C compatible with the extern "C" {} trick. Video tutorial: [this video]

Recommends Dejan Skledar · 2015-05 · Stack Overflow · on “Interact C ++ from Objective-C and Swift”

Yes you Swift can interact with C++, but you'll have to define the methods in the `extern "C"`. Heres an excelent tutorial on how to make it: LINK [this video] Also, if you'll need to pass Strings to C++, heres my question a [LINK]

Recommends Dan Beaulieu · 2015-12 · Stack Overflow · on “Xcode 7.1 and C++”

Although Swift isn't designed to work with C++ yet, there are people who are able to do it by creating a wrapper in Objective-C. Here's a video example: [this video]

Recommends Ian · 2020-03 · Stack Overflow · on “Xcode Import Objective-C, Objective-C++ project into Swift Project”

I have solved the issue. I was mistaken and believed that the repository was Objective-C making calls to Objective-C++. It was actually all Objective-C++. I was able to make direct calls to the Objective-C++ functions by following this video guide [this video] .

Cites Dejan Skledar · 2015-04 · Stack Overflow · on “How to define string parameters in C++ header file - Bridged with Swift?”

I have it in extern "C" because of Swift - [this video] , wont work without it. Anyway, how else should I return an int array?

Cites John · 2017-12 · Stack Overflow · on “How to call C from Swift?”

In the case of c++, there is this error that pops up: "_getInput", referenced from: You need a c++ header file too. Add c-linkage [LINK] to your function, then include the header file in the bridge-header: Swift 3 UserInput.h #ifndef USERINPUT_H #define USERINPUT_H #ifdef __cplusplus extern "C"{ #endif getInput(int *output); #ifdef __cplusplus } #endif UserInput.c #include <stdio.h> void getInput(int *output) { scanf("%i", output); } main.swift import Foundation var output: CInt = 0 getInput(&output) print(output) cliinput-Bridging-Header.h #include "UserInput.h" Here is the original video explaining this [this video]

Cites Ian · 2020-03 · Stack Overflow · on “Is there a way in Swift to call an Objective-C++ function that's been defined inside of @implementation?”

I have been struggling to implement kalman-ios [LINK] into my Swift project. The kalman-ios project is written entirely in Objective-C++. I recently learned how to call Objective-C++ functions from this video guide [this video] Issue My issue is that I can make calls to normal functions, but none that are within an @implementation. Ex: I cannot successfully bridge Swift to didReadAccelerometer @interface KFAppDelegate () ... - (void)didReadAccelerometer:(CMAccelerometerData *)data; @implementation KFAppDelegate ... - (void)didReadAccelerometer:(CMAccelerometerData *)data { // gyro or compass data not available yet, wait until next update if (!self.gyroData || !self.magnetometerData) return; self.