diff --git a/docs/CppInteroperability/GettingStartedWithC++Interop.md b/docs/CppInteroperability/GettingStartedWithC++Interop.md index 5235379cce1..dc40c3e1284 100644 --- a/docs/CppInteroperability/GettingStartedWithC++Interop.md +++ b/docs/CppInteroperability/GettingStartedWithC++Interop.md @@ -11,22 +11,20 @@ This document is designed to get you started with bidirectional API-level intero ## Creating a Module to contain your C++ source code -- Create a new C++ implementation and header file -- For this example we will call the files Cxx, so we should have a Cxx.cpp and Cxx.hpp. +- Create a new target in Xcode via _File_ | _New_ | _Target_, select _Library_. +- Within the directory of the newly created target, create a new C++ implementation and header file +- For this example we will call the files CxxTest, so we should have a CxxTest.cpp and CxxTest.hpp. - 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). ``` -//In module.modulemap -module Cxx { - //note that your header should be the file that contains your method implementations - header "Cxx.hpp" +// In module.modulemap +module CxxTest { + header "CxxTest.hpp" requires cplusplus } ``` -- Move the newly created files (Cxx.cpp, Cxx.hpp, module.modulemap) into a separate directory (this should remain in your project directory) - -Screen Shot 2022-02-26 at 9 14 06 PM +cxx-interop-ctest ## Adding C++ to an Xcode project - 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,12 +33,12 @@ Add the C++ module to the include path and enable C++ interop: - Navigate to your project directory - In `Project` navigate to `Build Settings` -> `Swift Compiler` - Under `Custom Flags` -> `Other Swift Flags` add`-enable-experimental-cxx-interop` -- Under `Search Paths` -> `Import Paths` add your search path to the C++ module (i.e, `./ProjectName/Cxx`). Repeat this step in `Other Swift Flags` +- Under `Search Paths` -> `Import Paths` add your search path to the C++ module (i.e, `./ProjectName/CxxTest`). Repeat this step in `Other Swift Flags` ``` //Add to Other Swift Flags and Import Paths respectively -enable-experimental-cxx-interop --I./ProjectName/Cxx +-I./ProjectName/CxxTest ``` - This should now allow your to import your C++ Module into any `.swift` file. @@ -48,21 +46,21 @@ Add the C++ module to the include path and enable C++ interop: ``` //In ContentView.swift import SwiftUI -import Cxx +import CxxTest struct ContentView: View { var body: some View { - Text("Cxx function result: \(cxxFunction(7))") + Text("CxxTest function result: \(cxxFunction(7))") .padding() } } ``` ``` -//In Cxx.hpp +// In CxxTest.hpp -#ifndef Cxx_hpp -#define Cxx_hpp +#ifndef CxxTest_hpp +#define CxxTest_hpp int cxxFunction(int n); @@ -70,9 +68,9 @@ int cxxFunction(int n); ``` ``` -//In Cxx.cpp +// In CxxTest.cpp -#include "Cxx.hpp" +#include "CxxTest.hpp" int cxxFunction(int n) { return n; @@ -101,24 +99,24 @@ let package = Package( platforms: [.macOS(.v12)], products: [ .library( - name: "Cxx", - targets: ["Cxx"]), + name: "CxxTest", + targets: ["CxxTest"]), .library( name: "CxxInterop", targets: ["CxxInterop"]), ], targets: [ .target( - name: "Cxx", + name: "CxxTest", dependencies: [] ), .executableTarget( name: "CxxInterop", - dependencies: ["Cxx"], + dependencies: ["CxxTest"], path: "./Sources/CxxInterop", sources: [ "main.swift" ], swiftSettings: [.unsafeFlags([ - "-I", "Sources/Cxx", + "-I", "Sources/CxxTest", "-enable-experimental-cxx-interop", ])] ), @@ -132,7 +130,7 @@ let package = Package( ``` //In main.swift -import Cxx +import CxxTest public struct CxxInterop { @@ -151,12 +149,12 @@ After creating your project follow the steps [Creating a Module to contain your - Create a `CMakeLists.txt` file and configure for your project - In`add_library` invoke `cxx-support` with the path to the C++ implementation file -- Add the `target_include_directories` with `cxx-support` and path to the C++ Module `${CMAKE_SOURCE_DIR}/Sources/Cxx` +- Add the `target_include_directories` with `cxx-support` and path to the C++ Module `${CMAKE_SOURCE_DIR}/Sources/CxxTest` - Add the `add_executable` to the specific files/directory you would like to generate source, with`SHELL:-enable-experimental-cxx-interop`. - In the example below we will be following the file structure used in [Creating a Swift Package](#Creating-a-Swift-Package) ``` -//In CMakeLists.txt +// In CMakeLists.txt cmake_minimum_required(VERSION 3.18) @@ -166,14 +164,12 @@ set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED YES) set(CMAKE_CXX_EXTENSIONS OFF) -add_library(cxx-support ./Sources/Cxx/Cxx.cpp) +add_library(cxx-support ./Sources/CxxTest/CxxTest.cpp) target_compile_options(cxx-support PRIVATE - -I${SWIFT_CXX_TOOLCHAIN}/usr/include/c++/v1 -fno-exceptions - -fignore-exceptions - -nostdinc++) + -fignore-exceptions) target_include_directories(cxx-support PUBLIC - ${CMAKE_SOURCE_DIR}/Sources/Cxx) + ${CMAKE_SOURCE_DIR}/Sources/CxxTest) add_executable(CxxInterop ./Sources/CxxInterop/main.swift) target_compile_options(CxxInterop PRIVATE @@ -185,7 +181,7 @@ target_link_libraries(CxxInterop PRIVATE cxx-support) ``` //In main.swift -import Cxx +import CxxTest public struct CxxInterop { public static func main() {