-
Notifications
You must be signed in to change notification settings - Fork 21
feat: Add C++ to Swift to Android integration example #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
Thanks, we'll take a look. 👍 |
|
Very neat! Out of curiosity, why do you have the C++ project use a separate CMakeLists.txt and build-android-static.sh script, rather than just including the C++ in a Swift module and letting SwiftPM build it? I was able to successfully do this in another sample (in a separate repository) at https://github.com/swift-android-sdk/swift-android-samples/tree/main/cpp-demo |
|
Thanks for taking a look @marcprux
That's good question. Many existing C++ projects already use CMake to manage targets and library dependencies, and retrofitting a Package.swift into them can be quite challenging. That's why we've opted for a setup where we pre-build the C++ library as a binary. In fact, I think this approach is more common in practice than having Swift PM build the C++ code directly. |
| #define CALCULATOR_H | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For now although I used extern "C" to wrap C++ function since the build while enabling C++ interop feature is failed, once following PR is merged, Im going to remove this and to enable C++ interop in other PR!
This is an area I'm investigating for SwiftPM, how to integrate CMake built libraries into the SwiftPM build more naturally and more directly. It would be awesome if SwiftPM could call out to an external build system to produce artifacts that it can then integrate into its own build. Someone even suggested SwiftPM could load up the CMake metadata and build those targets itself. But the main point is that this is a use case we really want to support. This is another great example. |
This PR adds the
hello-cpp-swiftexample, demonstrating how to call C++ code from Android through Swift.The example shows:
artifactbundlefor Swift consumption.binaryTargetof Swift Package ManagerThis pattern is useful for integrating existing C++ libraries into Android applications with Swift as an intermediate
layer, leveraging swift-java's automatic bridging capabilities.