[RESOLVED] C++ compiler use not correct language standard

A problem

I'm trying to wrap c++ lib box2d in SPM package, but when I try to build a target, then I receive a lot of errors, like this:

and this

As you can see, the main problem is C99 standard, because some syntax features like constructor with default values or bool types not supported in C99. I explicitly set cxxLanguageStandard to .cxx11, but it doesn't works for me. I've no idea what I'm doing wrong, but looks like this is a problem of compiler.

The package you can see here: GitHub - SpectralDragon/box2d-swift: The box2d bridging to Swift

How to reproduce

  1. Download sources from repo
  2. Run target 'box2d-test'

C++ interoperability is currently disabled by default in Swift. Swift tries to import the headers as if they were Objective-C headers, and the syntax that you use is not valid in Objective-C. To actually enable C++ interoperability, you'll need to pass a compiler flag: -Xfrontend -enable-experimental-cxx-interop. You could do that with SwiftPM's unsafe flags mechanism:
unsafeFlags(["-Xfrontend", "-enable-experimental-cxx-interop"]).

2 Likes

If I understand correctly, I should pass flags to swiftSettings, but what minimal version of Swift I should have? Current version is 5.6

I should pass flags to swiftSettings

Yes, that is correct.

I would recommend using a nightly Swift toolchain when experimenting with C++ interop. The project is moving quickly, and you'll likely find issues in release toolchains that are already fixed in trunk.
That said, for Swift 5.6 the correct flag is enable-cxx-interop.

1 Like

enable-cxx-interop is works! Thanks a lot!