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
*/