How to works with c++ interop?

I've three modules and one of them contains c++ code. That how they looks like:

 // AppKit App
.executableTarget(name: "Test", dependency: ["LibWrapperSwift"]),
 // Swift Module
.target(
    name: "LibWrapperSwift",
    dependency: ["LibCXX"],
    swiftSettings: [ .unsafeFlags(["-Xfrontend", "-enable-cxx-interop"]) ]
),
// C++ code
.target(
    name: "LibCXX"
)

When I import LibWrapperSwift to my executable target, then I get an error could not build Objective-C module LibCXX. But, If I copy swiftSettings to executable target, then LibCXX will compile, but I will get an error when compile Apple Obj-C based frameworks like the AppKit.

The Package.swift:

Test main.swift

That is a huge problem for me, because I should use c++ in my project and don't lose normal ObjC compiler, because It's helps me to work with AppKit or UIKit frameworks.

How can I fix that?

Search for "ModifierFlags" across all sources, there is some clash. Might give you a better idea where to go from there.

Should that be either -enable-experimental-cxx-interop or -enable-cxx-interop?

enable-experimental-cxx-interop doesn’t work for me in Swift 5.6 and Xcode 13.2:(

I think what Ben is saying is that you need to write enable, not enabled.

I got the mistake in my source example, but in my attached screenshot is all ok.
My main point that -enable-cxx-interop not works with ObjectiveC based frameworks. I've no idea why :man_shrugging:

I can link spm example for you

Have you tried -enable-objc-interop as well?

In my (bash) scripts for dumping out Foundation and UIKit to observe the difference in output when enabling cxx-interop I use both flags:

1 Like

Is this reproducible with a recent nightly toolchain (5.8)? I recall there were some issues with enums that were fixed in the last couple of months, this might be one of them.

1 Like

A drive by reminder: C++ interop is an unstable, experimental project. Things will break and be re-named in the future. I do not recommend using it in production.

4 Likes