Skip to content

Add example of Swift <--> C++ interop #1198

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

Closed
jiawen opened this issue May 6, 2024 · 6 comments · Fixed by #1280
Closed

Add example of Swift <--> C++ interop #1198

jiawen opened this issue May 6, 2024 · 6 comments · Fixed by #1280

Comments

@jiawen
Copy link

jiawen commented May 6, 2024

In the last year, Swift has gradually gained the ability to interop with C++. rules_swift should add a minimal example on how that would work. It would provide at baseline a means by which we can easily reproduce issues (module naming, static and dynamic linking, ODR violations, etc) as they are discovered.

@gkoreman
Copy link

Yes please!

I just tried this out and was pleasantly surprised at how easy it was. Here are some quick notes based on my experience in case anyone else sees this before the examples come out.

BUILD

swift_library(
    name = "mySwiftLib",
    srcs = [ "lib.swift" ], #note - if you make this main.swift then it will have an extra _main symbol which conficts with your app
    generates_header = True,
    copts = ["-cxx-interoperability-mode=default"], #needed to export c++ bindings
)

cc_binary(
...
    deps = [":mySwiftLib"]
)

SWIFT lib.swift

public func printWelcomeMessage(_ name: String) {
  print("Welcome \(name)")
}

C++

#include "full/path/to/module/mySwiftLib-Swift.h"

int main(int, char**)
{
    // Namespace appears to be the full path to the module
    full_path_to_module::printWelcomeMessage("friend");
    return 0;
}

@davidzchen
Copy link
Member

It would be great to have an example of the other way around: calling C++ code from Swift. This will make it easier for developers to utilize C++ libraries in Swift code.

@kersson
Copy link
Contributor

kersson commented Jun 26, 2024

Just put up a PR to add a basic example to show how to invoke C++ code from Swift: #1280

@brentleyjones brentleyjones linked a pull request Jun 26, 2024 that will close this issue
@jiawen
Copy link
Author

jiawen commented Nov 18, 2024

@brentleyjones I just whipped up a short example of calling Swift from C++ (the first part of the Mountain Peak example). Should I put up a PR to include it in the examples?

@brentleyjones
Copy link
Collaborator

Does it add anything more than 321d2c3? If so, yes.

@jiawen
Copy link
Author

jiawen commented Nov 18, 2024

Does it add anything more than 321d2c3? If so, yes.

Sent #1454.

If there's any interest, I also have a slightly more complex example that has a C++ class conform to a Swift protocol. Nothing that's not in the documentation, but potentially useful since it's a common use case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants