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/CppInteroperability/GettingStartedWithC++Interop.md
+28-32Lines changed: 28 additions & 32 deletions
Original file line number
Diff line number
Diff line change
@@ -11,22 +11,20 @@ This document is designed to get you started with bidirectional API-level intero
11
11
12
12
## Creating a Module to contain your C++ source code
13
13
14
-
- Create a new C++ implementation and header file
15
-
- For this example we will call the files Cxx, so we should have a Cxx.cpp and Cxx.hpp.
14
+
- Create a new target in Xcode via _File_ | _New_ | _Target_, select _Library_.
15
+
- Within the directory of the newly created target, create a new C++ implementation and header file
16
+
- For this example we will call the files CxxTest, so we should have a CxxTest.cpp and CxxTest.hpp.
16
17
- Next create an empty file and call it `module.modulemap`, in this file create the module for your source code, and define your C++ header (`requires cplusplus` isn't required but it's convention for C++ modules, especially if they use C++ features).
17
18
18
19
```
19
-
//In module.modulemap
20
-
module Cxx {
21
-
//note that your header should be the file that contains your method implementations
22
-
header "Cxx.hpp"
20
+
// In module.modulemap
21
+
module CxxTest {
22
+
header "CxxTest.hpp"
23
23
requires cplusplus
24
24
}
25
25
```
26
26
27
-
- Move the newly created files (Cxx.cpp, Cxx.hpp, module.modulemap) into a separate directory (this should remain in your project directory)
28
-
29
-
<imgwidth="252"alt="Screen Shot 2022-02-26 at 9 14 06 PM"src="https://user-images.githubusercontent.com/62521716/155867937-9d9d6c62-4418-414d-bc4e-5d12c2055022.png">
- In your xcode project, follow the steps [Creating a Module to contain your C++ source code](#creating-a-module-to-contain-your-c-source-code) in your project directory
@@ -35,44 +33,44 @@ Add the C++ module to the include path and enable C++ interop:
35
33
- Navigate to your project directory
36
34
- In `Project` navigate to `Build Settings` -> `Swift Compiler`
37
35
- Under `Custom Flags` -> `Other Swift Flags` add`-enable-experimental-cxx-interop`
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`
36
+
- Under `Search Paths` -> `Import Paths` add your search path to the C++ module (i.e, `./ProjectName/CxxTest`). Repeat this step in `Other Swift Flags`
39
37
40
38
```
41
39
//Add to Other Swift Flags and Import Paths respectively
42
40
-enable-experimental-cxx-interop
43
-
-I./ProjectName/Cxx
41
+
-I./ProjectName/CxxTest
44
42
```
45
43
46
44
- This should now allow your to import your C++ Module into any `.swift` file.
47
45
48
46
```
49
47
//In ContentView.swift
50
48
import SwiftUI
51
-
import Cxx
49
+
import CxxTest
52
50
53
51
struct ContentView: View {
54
52
var body: some View {
55
-
Text("Cxx function result: \(cxxFunction(7))")
53
+
Text("CxxTest function result: \(cxxFunction(7))")
56
54
.padding()
57
55
}
58
56
}
59
57
```
60
58
61
59
```
62
-
//In Cxx.hpp
60
+
//In CxxTest.hpp
63
61
64
-
#ifndef Cxx_hpp
65
-
#define Cxx_hpp
62
+
#ifndef CxxTest_hpp
63
+
#define CxxTest_hpp
66
64
67
65
int cxxFunction(int n);
68
66
69
67
#endif
70
68
```
71
69
72
70
```
73
-
//In Cxx.cpp
71
+
//In CxxTest.cpp
74
72
75
-
#include "Cxx.hpp"
73
+
#include "CxxTest.hpp"
76
74
77
75
int cxxFunction(int n) {
78
76
return n;
@@ -101,24 +99,24 @@ let package = Package(
101
99
platforms: [.macOS(.v12)],
102
100
products: [
103
101
.library(
104
-
name: "Cxx",
105
-
targets: ["Cxx"]),
102
+
name: "CxxTest",
103
+
targets: ["CxxTest"]),
106
104
.library(
107
105
name: "CxxInterop",
108
106
targets: ["CxxInterop"]),
109
107
],
110
108
targets: [
111
109
.target(
112
-
name: "Cxx",
110
+
name: "CxxTest",
113
111
dependencies: []
114
112
),
115
113
.executableTarget(
116
114
name: "CxxInterop",
117
-
dependencies: ["Cxx"],
115
+
dependencies: ["CxxTest"],
118
116
path: "./Sources/CxxInterop",
119
117
sources: [ "main.swift" ],
120
118
swiftSettings: [.unsafeFlags([
121
-
"-I", "Sources/Cxx",
119
+
"-I", "Sources/CxxTest",
122
120
"-enable-experimental-cxx-interop",
123
121
])]
124
122
),
@@ -132,7 +130,7 @@ let package = Package(
132
130
```
133
131
//In main.swift
134
132
135
-
import Cxx
133
+
import CxxTest
136
134
137
135
public struct CxxInterop {
138
136
@@ -151,12 +149,12 @@ After creating your project follow the steps [Creating a Module to contain your
151
149
152
150
- Create a `CMakeLists.txt` file and configure for your project
153
151
- In`add_library` invoke `cxx-support` with the path to the C++ implementation file
154
-
- Add the `target_include_directories` with `cxx-support` and path to the C++ Module `${CMAKE_SOURCE_DIR}/Sources/Cxx`
152
+
- Add the `target_include_directories` with `cxx-support` and path to the C++ Module `${CMAKE_SOURCE_DIR}/Sources/CxxTest`
155
153
- Add the `add_executable` to the specific files/directory you would like to generate source, with`SHELL:-enable-experimental-cxx-interop`.
156
154
- In the example below we will be following the file structure used in [Creating a Swift Package](#Creating-a-Swift-Package)
0 commit comments