Swift C++ Interoperability & Windows DLLs

Ah, sorry, I was reading on a mobile device and didn't realize that the link was back to Miguel's project. I know that others have experimented successfully with building the project on Windows (but does require the latest toolchain).

There is nothing special about C++ here, it is the same as C. All the examples for interoperability with C libraries applies. The complication here is not the C/C++ interop but rather SPM. The XCFramework functionality is limited to macOS., and there is still a hole in SPM support as we do not have support for nuget for the distribution of the prebuilt binaries that is common on Windows (e.g. WinUI and the WinSDK are both distributed as nuget packages).

The example that I have on hand does not use SPM but rather CMake. For SPM, you would have to manually replicate the operations. Effectively, you are responsible for selecting the location of the dependency (e.g. %UserProfile%\SourceCache). Once you have the library, you will need to identify the root of the include tree and implant the module.modulemap for the module. This prepares the module for consumption. At that point, when you build with SPM, you will need to pass along the include path and the linker search path for the associated import libraries, e.g.

swift build -I %UserProfile%\SourceCache\dependency-development\usr\include -Xlinker -L%UserProfile%\SourceCache\dependency-development\usr\lib

assuming that you have built a version of the library with the traditional Unix layout, extracted it to %UserProfile%\SourceCache with the name of dependency-development.

This is the generic means for including a binary dependency for use in a SPM package and apply to any package really. The same mechanism is well documented and used in the build of the swift-driver.

1 Like