Initial C++ interoperability documentation is published on swift.org

Hi everyone,

The initial set of documentation pages that describe how to mix Swift and C++ has now been published on swift.org, following the announcement that Swift 5.9 will include support for C++ interoperability.

The main reference guide describes how to mix Swift and C++.

The status page describes the currently supported feature set and lists some limitations and constraints.

The build and project page describes how to setup a mixed Swift and C++ project in Swift Package Manager or Xcode, and also describes how to configure C++ interoperability in a custom build configuration.

We will add additional contents related to CMake project setup later this month. Note that a CMake sample that shows how to use bidirectional interoperability is already available on GitHub.

The @cxx-interop-workgroup is excited to hear your feedback! Please post any feedback you have here on the forums, or file an issue on GitHub.

12 Likes

I suggest that it would be helpful to have something like the following specific steps added to the documentation. These will help those who new to Swift and or C++ interoperability to get started with a project that runs. These steps can serve as a check-list for getting an up and running project.

My list of steps has // and /* ... */ comments to show how I include it in my projects. i insert into every project that uses Swift and CPP, just to make sure it won't get lost.

I appreciate the post from SoundBudies that got me started, and am passing along what I learned so others may benefit as well.

// I Insert the following documentation into every Swift_CPP project.

// =======================================================================
// Instructions Follow
// =======================================================================
/*
Instructions for creating a Swift program that runs C++ code.
Adapted from Sound Buddies post to forum.swift.org
November 11, 2023

The example project for these steps is ...

Swift_CPP_Test.xcodeproj

In my notation, the "Swift" indicates it is a Swift project,
and the "CPP" indicates it incorporates C++
I use this notation so that in the XCode directory, all CPP files will be shown
sequentially, together, as will all Swift files, and now all Swift_CPP files.

Following are the steps I use.

Create a new XCode project for a command-line tool

Change build settings for the project.

It is easier to find what you want to change by using the search tool in the project.

 Project -> Build Settings -> Search for “header search paths”
 Expand “Header Search Paths”  -> Debug
 In space available add this text: $(PROJECT_DIR)
 Note that the text converts to the current project path.

Change the project language.

 Project -> Build Settings -> Search for “Swift Compiler - Language”
 In line “C++ and Objective-C Interoperability” change the settings in
 columns 2 and 3 on the right to “C++ / Objective-C++”.

Create new .cpp and .hpp files. The examples are...

 Swift_CPP_Test.cpp and Swift_CPP_Test.hpp
 File -> New-> File Swift_CPP_Test add header .hpp file
 Click on “Create Bridging Header”

In the .cpp file add a simple function...

  void helloFromCPP()
  {
  printf("\nHello From CPP\n");
  }

In the HPP file add…

 void helloFromCPP();

In the bridging header file, add...

   #import <Swift_CPP_Test.hpp>
   
    or
   
   #import "Swift_CPP_Test.hpp"
    
   whichever works

In the bridging header file add..

   #import <Swift_CPP_Test.hpp>

Go to main.swift and add the function that is to be used.

 helloFromCPP()

Run -- It works on December 8, 2023

*/