Skip to content

Commit 23ba70e

Browse files
authored
[cxx-interop] Update GettingStartedWithC++Interop documentation (#41597)
1 parent da0e1da commit 23ba70e

File tree

1 file changed

+23
-24
lines changed

1 file changed

+23
-24
lines changed

docs/CppInteropability/GettingStartedWithC++Interop.md

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Getting started with C++ Interoperability
22

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++.
44

55
## Table of Contents
66

@@ -35,7 +35,7 @@ Add the C++ module to the include path and enable C++ interop:
3535
- Navigate to your project directory
3636
- In `Project` navigate to `Build Settings` -> `Swift Compiler`
3737
- 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`
3939

4040
```
4141
//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:
4646
- This should now allow your to import your C++ Module into any `.swift` file.
4747

4848
```
49-
//In ViewController.swift
50-
import UIKit
49+
//In ContentView.swift
50+
import SwiftUI
5151
import Cxx
5252
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()
5857
}
5958
}
6059
```
@@ -86,8 +85,8 @@ int cxxFunction(int n);
8685
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
8786

8887
- 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`
9190
- 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)
9291
- Under targets, add the name of your C++ module and the directory containing the Swift code as a target.
9392
- 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
9897
import PackageDescription
9998
10099
let package = Package(
101-
name: "Cxx_Interop",
100+
name: "CxxInterop",
102101
platforms: [.macOS(.v12)],
103102
products: [
104103
.library(
105104
name: "Cxx",
106105
targets: ["Cxx"]),
107106
.library(
108-
name: "Cxx_Interop",
109-
targets: ["Cxx_Interop"]),
107+
name: "CxxInterop",
108+
targets: ["CxxInterop"]),
110109
],
111110
targets: [
112111
.target(
113112
name: "Cxx",
114113
dependencies: []
115114
),
116115
.executableTarget(
117-
name: "Cxx_Interop",
116+
name: "CxxInterop",
118117
dependencies: ["Cxx"],
119-
path: "./Sources/Cxx_Interop",
118+
path: "./Sources/CxxInterop",
120119
sources: [ "main.swift" ],
121120
swiftSettings: [.unsafeFlags([
122121
"-I", "Sources/Cxx",
@@ -135,14 +134,14 @@ let package = Package(
135134
136135
import Cxx
137136
138-
public struct Cxx_Interop {
137+
public struct CxxInterop {
139138
140139
public func callCxxFunction(n: Int32) -> Int32 {
141140
return cxxFunction(n: n)
142141
}
143142
}
144143
145-
print(Cxx_Interop().callCxxFunction(n: 7))
144+
print(CxxInterop().callCxxFunction(n: 7))
146145
//outputs: 7
147146
148147
```
@@ -161,7 +160,7 @@ After creating your project follow the steps [Creating a Module to contain your
161160
162161
cmake_minimum_required(VERSION 3.18)
163162
164-
project(Cxx_Interop LANGUAGES CXX Swift)
163+
project(CxxInterop LANGUAGES CXX Swift)
165164
166165
set(CMAKE_CXX_STANDARD 11)
167166
set(CMAKE_CXX_STANDARD_REQUIRED YES)
@@ -176,10 +175,10 @@ target_compile_options(cxx-support PRIVATE
176175
target_include_directories(cxx-support PUBLIC
177176
${CMAKE_SOURCE_DIR}/Sources/Cxx)
178177
179-
add_executable(Cxx_Interop ./Sources/Cxx_Interop/main.swift)
180-
target_compile_options(Cxx_Interop PRIVATE
178+
add_executable(CxxInterop ./Sources/CxxInterop/main.swift)
179+
target_compile_options(CxxInterop PRIVATE
181180
"SHELL:-Xfrontend -enable-cxx-interop"
182-
target_link_libraries(Cxx_Interop PRIVATE cxx-support)
181+
target_link_libraries(CxxInterop PRIVATE cxx-support)
183182
184183
```
185184

@@ -188,14 +187,14 @@ target_link_libraries(Cxx_Interop PRIVATE cxx-support)
188187
189188
import Cxx
190189
191-
public struct Cxx_Interop {
190+
public struct CxxInterop {
192191
public static func main() {
193192
let result = cxxFunction(7)
194193
print(result)
195194
}
196195
}
197196
198-
Cxx_Interop.main()
197+
CxxInterop.main()
199198
200199
```
201200

0 commit comments

Comments
 (0)