I am trying to figure out the steps necessary to create a Swift program that successfully calls a C++ function or class. I found the page at: https://github.com/apple/swift/blob/main/docs/CppInteroperability/GettingStartedWithC%2B%2BInterop.md is no longer accurate (as the heading link states). The page at: Swift.org - Mixing Swift and C++ discusses the topic with many insights, but nothing that gets me from creating a Swift project to a functional hello world program. Below are the steps that I have figured out so far. Some might suggest I have too much detail in the steps, but I find that when coding, even the slightest most minor oversight may be the one big thing that makes the difference between a working program and endless hours of frustration. Clearly the steps that I listed below are not correct because they donât get me to a working program. Those who are more familiar with interoperability will likely immediately see where I need to make changes. At this time, this list of steps is all I have. The list has gone through several iterations to get to this point. I offer the list as a question of how should I proceed to get a working program, and as a suggestion that somewhere in a prominent place in the documentation, there needs to be a set of steps, in this form, that is always current, and takes a reader from zero to a working Swift program, and conversely from zero to a working C++ program.
Here are the steps that I have.
- Be sure you are using XCode 15, and Swift 5
- Plan ahead by to put all files into the Swift project folder so everything is in one place and easily found and referenced. This can be changed, later, to have the C++ files in another location.
- Create a Swift project
- Check again to make sure this is Swift 5 or greater.
- One way to check the version is to use the following code in a Swift program. The text editor, for this post may change the word âprintâ to âPrintâ. It happens.
In my editing of this post, I found the editor wants to remove the pound sign from the statements in the following code example. When implementing those statements add a pound sign before, if, else if, and endif.
if swift(>=5.9) // see the comments in the text above.
print(âYou are running Swift 5.9 or greater. Youâre good to go ahead.â)
elseif swift(>=5.0)
print(âYou are running Swift 5.0 or greater. Youâre good to go ahead.â)
endif
- After creating the Swift project, all subsequent files are to be created in the project by beginning with the following steps: XCode -> new -> file -> (followed by selecting the appropriate icon for whichever file type is to be created as part of the project)
- Change the project settings for C++ interoperability by taking the following steps: Project -> Build Settings -> All -> C++ and Objective C Interoperability -> Set to C++/Objective C
- Create a C++ .cpp file but no .hpp file (no code added yet) (the name is CPPTest.CPP)
- Create a .h file having the same name as the C++ file, and having the extension .h (wonder if I could have created a .hpp and just changed the extension, or just leave it as hpp and reference it as such where needed). The file name is CPPTest.h.
- Create a module map file having same name as the CPP file, with extension .modulemap the modulemap file name is CPPTest.modulemap.
- Change the module map contents to read as follows:
module CPPTest{
header âCPPTest.hâ
requires cplusplus // this is not mentioned in all documents.
export *
}
-
Go back to main.swift and add the following:
-
Import âCPPTest.hâ
-
Here I get the error message: âExpected identifier in import declarationâ
-
The error appears to be occurring on the quote character â â â ahead of the file name. So I tried various possibilities including leaving off the quote sign, inserting a complete path to the .h file, leaving off the .h, pointing to the module map, and others, with no success.
-
My project includes one .h file, If there eventually is a project with several .h files and several .cpp files and only the .h files are listed in the modulemap and main.swift,, how does each referenced .h file know which .cpp file it is to be associated with? That may be revealed when I have a complete set of working how-to steps. Perhaps that is also at the heart of my current problem.