Skip to content

Commit e8ae3f7

Browse files
committed
[cxx-interop][docs] Update getting started guide
* Do not use `Cxx` as a user module name, since we now ship a built-in module called `Cxx`. * Describe creating an Xcode target that builds the C++ module. * Do not provide libc++ include paths manually to the Swift compiler, Swift should detect the C++ stdlib automatically.
1 parent 54993b7 commit e8ae3f7

File tree

1 file changed

+28
-32
lines changed

1 file changed

+28
-32
lines changed

docs/CppInteroperability/GettingStartedWithC++Interop.md

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,20 @@ This document is designed to get you started with bidirectional API-level intero
1111

1212
## Creating a Module to contain your C++ source code
1313

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.
1617
- 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).
1718

1819
```
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"
2323
requires cplusplus
2424
}
2525
```
2626

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-
<img width="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">
27+
<img width="127" alt="cxx-interop-ctest" src="https://user-images.githubusercontent.com/3801618/192995602-f37137f3-ec15-4fdd-bf2c-591728945a68.png">
3028

3129
## Adding C++ to an Xcode project
3230
- 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:
3533
- Navigate to your project directory
3634
- In `Project` navigate to `Build Settings` -> `Swift Compiler`
3735
- 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`
3937

4038
```
4139
//Add to Other Swift Flags and Import Paths respectively
4240
-enable-experimental-cxx-interop
43-
-I./ProjectName/Cxx
41+
-I./ProjectName/CxxTest
4442
```
4543

4644
- This should now allow your to import your C++ Module into any `.swift` file.
4745

4846
```
4947
//In ContentView.swift
5048
import SwiftUI
51-
import Cxx
49+
import CxxTest
5250
5351
struct ContentView: View {
5452
var body: some View {
55-
Text("Cxx function result: \(cxxFunction(7))")
53+
Text("CxxTest function result: \(cxxFunction(7))")
5654
.padding()
5755
}
5856
}
5957
```
6058

6159
```
62-
//In Cxx.hpp
60+
// In CxxTest.hpp
6361
64-
#ifndef Cxx_hpp
65-
#define Cxx_hpp
62+
#ifndef CxxTest_hpp
63+
#define CxxTest_hpp
6664
6765
int cxxFunction(int n);
6866
6967
#endif
7068
```
7169

7270
```
73-
//In Cxx.cpp
71+
// In CxxTest.cpp
7472
75-
#include "Cxx.hpp"
73+
#include "CxxTest.hpp"
7674
7775
int cxxFunction(int n) {
7876
return n;
@@ -101,24 +99,24 @@ let package = Package(
10199
platforms: [.macOS(.v12)],
102100
products: [
103101
.library(
104-
name: "Cxx",
105-
targets: ["Cxx"]),
102+
name: "CxxTest",
103+
targets: ["CxxTest"]),
106104
.library(
107105
name: "CxxInterop",
108106
targets: ["CxxInterop"]),
109107
],
110108
targets: [
111109
.target(
112-
name: "Cxx",
110+
name: "CxxTest",
113111
dependencies: []
114112
),
115113
.executableTarget(
116114
name: "CxxInterop",
117-
dependencies: ["Cxx"],
115+
dependencies: ["CxxTest"],
118116
path: "./Sources/CxxInterop",
119117
sources: [ "main.swift" ],
120118
swiftSettings: [.unsafeFlags([
121-
"-I", "Sources/Cxx",
119+
"-I", "Sources/CxxTest",
122120
"-enable-experimental-cxx-interop",
123121
])]
124122
),
@@ -132,7 +130,7 @@ let package = Package(
132130
```
133131
//In main.swift
134132
135-
import Cxx
133+
import CxxTest
136134
137135
public struct CxxInterop {
138136
@@ -151,12 +149,12 @@ After creating your project follow the steps [Creating a Module to contain your
151149

152150
- Create a `CMakeLists.txt` file and configure for your project
153151
- 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`
155153
- Add the `add_executable` to the specific files/directory you would like to generate source, with`SHELL:-enable-experimental-cxx-interop`.
156154
- In the example below we will be following the file structure used in [Creating a Swift Package](#Creating-a-Swift-Package)
157155

158156
```
159-
//In CMakeLists.txt
157+
// In CMakeLists.txt
160158
161159
cmake_minimum_required(VERSION 3.18)
162160
@@ -166,14 +164,12 @@ set(CMAKE_CXX_STANDARD 11)
166164
set(CMAKE_CXX_STANDARD_REQUIRED YES)
167165
set(CMAKE_CXX_EXTENSIONS OFF)
168166
169-
add_library(cxx-support ./Sources/Cxx/Cxx.cpp)
167+
add_library(cxx-support ./Sources/CxxTest/CxxTest.cpp)
170168
target_compile_options(cxx-support PRIVATE
171-
-I${SWIFT_CXX_TOOLCHAIN}/usr/include/c++/v1
172169
-fno-exceptions
173-
-fignore-exceptions
174-
-nostdinc++)
170+
-fignore-exceptions)
175171
target_include_directories(cxx-support PUBLIC
176-
${CMAKE_SOURCE_DIR}/Sources/Cxx)
172+
${CMAKE_SOURCE_DIR}/Sources/CxxTest)
177173
178174
add_executable(CxxInterop ./Sources/CxxInterop/main.swift)
179175
target_compile_options(CxxInterop PRIVATE
@@ -185,7 +181,7 @@ target_link_libraries(CxxInterop PRIVATE cxx-support)
185181
```
186182
//In main.swift
187183
188-
import Cxx
184+
import CxxTest
189185
190186
public struct CxxInterop {
191187
public static func main() {

0 commit comments

Comments
 (0)