(XCode 15 beta 6, MacOS 14.0 Beta (23A5301h))
I have two projects:
-
Swift + C++ together in an XCode project. Swift can see C++ fine, compiles and runs. No problem.
-
Swift Package with C++ library target and Swift library target with a Testing target. It uses an explicit module map (because I couldn't get the umbrella header style example to work, actually). It does not run tests.
module cxxLibrary {
header "SimpleCxxFunctions.hpp"
export *
}
Building for debugging...
ld: Undefined symbols:
_myFavoriteNumber, referenced from:
InteropLibrary.NumberMaker.currentNumber() -> Swift.Int in NumberMaker.swift.o
clang: error: linker command failed with exit code 1 (use -v to see invocation)
[5/6] Linking CxxInteropLibraryPa…
error: fatalError
Package file:
// swift-tools-version: 5.9
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "CxxInteropLibrary",
products: [
.library(
name: "cxxLibrary",
targets: ["cxxLibrary"]),
.library(
name: "InteropLibrary",
targets: ["InteropLibrary"])
],
targets: [
.target(
name: "cxxLibrary"),
.target(
name: "InteropLibrary",
dependencies: ["cxxLibrary"]),
//swiftSettings: [.interoperabilityMode(.Cxx)]), have tried it both ways.
.testTarget(
name: "CxxInteropLibraryTests",
dependencies: ["InteropLibrary", "cxxLibrary"],
swiftSettings: [.interoperabilityMode(.Cxx)])
]
)
Neither XCode nor the VScode extension flag any problems before the build.
Before I keep banging my head against this, should I even expect this to work with XCTests? If so is there something obvious I've done wrong?
Thank you.
Alex_L
(Alex Lorenz)
2
It appears your Swift InteropLibrary target is not enabling interop and thus it thinks it is calling a C function, not a C++ function. This modified package manifest works for me locally and fixes the link issue:
let package = Package(
name: "CxxInteropLibrary",
products: [
.library(
name: "cxxLibrary",
targets: ["cxxLibrary"]),
.library(
name: "InteropLibrary",
targets: ["InteropLibrary"])
],
targets: [
.target(
name: "cxxLibrary"),
.target(
name: "InteropLibrary",
dependencies: ["cxxLibrary"],
swiftSettings: [.interoperabilityMode(.Cxx)]),
.testTarget(
name: "CxxInteropLibraryTests",
dependencies: ["InteropLibrary", "cxxLibrary"],
swiftSettings: [.interoperabilityMode(.Cxx)])
]
)
2 Likes
Thank you. Returning that swiftSettings line to the Interop library worked!
I could have sworn I had tried that both ways but I must not have cleaned out the build folder.