You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/CppInteropability/GettingStartedWithC++Interop.md
+23-24Lines changed: 23 additions & 24 deletions
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# Getting started with C++ Interoperability
2
2
3
-
This document is desgined to get you started with bidirectional API-level interoperability between Swift and C++.
3
+
This document is designed to get you started with bidirectional API-level interoperability between Swift and C++.
4
4
5
5
## Table of Contents
6
6
@@ -35,7 +35,7 @@ Add the C++ module to the include path and enable C++ interop:
35
35
- Navigate to your project directory
36
36
- In `Project` navigate to `Build Settings` -> `Swift Compiler`
37
37
- Under `Custom Flags` -> `Other Swift Flags` add`-Xfrontend -enable-cxx-interop`
38
-
- Under `Search Paths` -> `Import Paths` add your search path to the C++ module (i.e, `./ProjectName/Cxx`).
38
+
- Under `Search Paths` -> `Import Paths` add your search path to the C++ module (i.e, `./ProjectName/Cxx`). Repeat this step in `Other Swift Flags`
39
39
40
40
```
41
41
//Add to Other Swift Flags and Import Paths respectively
@@ -46,15 +46,14 @@ Add the C++ module to the include path and enable C++ interop:
46
46
- This should now allow your to import your C++ Module into any `.swift` file.
47
47
48
48
```
49
-
//In ViewController.swift
50
-
import UIKit
49
+
//In ContentView.swift
50
+
import SwiftUI
51
51
import Cxx
52
52
53
-
class ViewController: UIViewController {
54
-
override func viewDidLoad() {
55
-
super.viewDidLoad()
56
-
let result = cxxFunction(7)
57
-
print(result)
53
+
struct ContentView: View {
54
+
var body: some View {
55
+
Text("Cxx function result: \(cxxFunction(7))")
56
+
.padding()
58
57
}
59
58
}
60
59
```
@@ -86,8 +85,8 @@ int cxxFunction(int n);
86
85
After creating your Swift package project, follow the steps [Creating a Module to contain your C++ source code](#creating-a-module-to-contain-your-c-source-code) in your `Source` directory
87
86
88
87
- In your Package Manifest, you need to configure the Swift target's dependencies and compiler flags
89
-
- In this example the name of the package is `Cxx_Interop`
90
-
- Swift code will be in `Sources/Cxx_Interop` called `main.swift`
88
+
- In this example the name of the package is `CxxInterop`
89
+
- Swift code will be in `Sources/CxxInterop` called `main.swift`
91
90
- C++ source code follows the example shown in [Creating a Module to contain your C++ source code](#creating-a-module-to-contain-your-c-source-code)
92
91
- Under targets, add the name of your C++ module and the directory containing the Swift code as a target.
93
92
- In the target defining your Swift target, add a`dependencies` to the C++ Module, the `path`, `source`, and `swiftSettings` with `unsafeFlags` with the source to the C++ Module, and enable `-enable-cxx-interop`
@@ -98,25 +97,25 @@ After creating your Swift package project, follow the steps [Creating a Module t
98
97
import PackageDescription
99
98
100
99
let package = Package(
101
-
name: "Cxx_Interop",
100
+
name: "CxxInterop",
102
101
platforms: [.macOS(.v12)],
103
102
products: [
104
103
.library(
105
104
name: "Cxx",
106
105
targets: ["Cxx"]),
107
106
.library(
108
-
name: "Cxx_Interop",
109
-
targets: ["Cxx_Interop"]),
107
+
name: "CxxInterop",
108
+
targets: ["CxxInterop"]),
110
109
],
111
110
targets: [
112
111
.target(
113
112
name: "Cxx",
114
113
dependencies: []
115
114
),
116
115
.executableTarget(
117
-
name: "Cxx_Interop",
116
+
name: "CxxInterop",
118
117
dependencies: ["Cxx"],
119
-
path: "./Sources/Cxx_Interop",
118
+
path: "./Sources/CxxInterop",
120
119
sources: [ "main.swift" ],
121
120
swiftSettings: [.unsafeFlags([
122
121
"-I", "Sources/Cxx",
@@ -135,14 +134,14 @@ let package = Package(
135
134
136
135
import Cxx
137
136
138
-
public struct Cxx_Interop {
137
+
public struct CxxInterop {
139
138
140
139
public func callCxxFunction(n: Int32) -> Int32 {
141
140
return cxxFunction(n: n)
142
141
}
143
142
}
144
143
145
-
print(Cxx_Interop().callCxxFunction(n: 7))
144
+
print(CxxInterop().callCxxFunction(n: 7))
146
145
//outputs: 7
147
146
148
147
```
@@ -161,7 +160,7 @@ After creating your project follow the steps [Creating a Module to contain your
0 commit comments