Exposing Swift APIs to C++ using SwiftPM

I have a SwiftPM package that contains most of my app code as a library (and it's added to the app's Xcode project). The package includes both Swift and C++ files with C++ interoperability enabled. I'm successfully able to access and use C++ APIs from Swift. However, I have yet to access Swift APIs in C++ code. I had assumed SwiftPM would auto-generate a bridging header for C++, but I tried MyLibrary-Swift.h and MyLibrary/MyLibrary-Swift.h, and neither works.

Update: After re-reading the Setting Up Mixed-Language Swift and C++ Projects document, I saw that SwiftPM doesn't support using Swift APIs in C++.

Is there any workaround for this?

In regards to generating c++ header from swift, do this to any target you want to export to c++

.target(name: "MyLibrary", 
     swiftSettings: [

        .unsafeFlags([
              "-module-name", "MyLibrary",
               "-emit-clang-header-path", "MyLibrary-Swift.h"
         ])
      ]

Hope this helps :innocent:

1 Like

Thanks for the code. I had a feeling there would be something like that that would solve my issue. Unfortunately, I get this type of error: MyLibrary/MyLibrary-Swift.h file not found. I’ll reread the article maybe I missed something.

I suggest you read the last section of that article