Embedding a C library in a package (using SPM)
After having read a ton of articles and random discussions about this, I'm as confused as ever at how to integrate a dylib (SDL2) into a package, writing a little wrapper code in Swift, and then include that package in my app as a module.
If I just build my app in Xcode and don't worry about separating things into modules, it's easy. Drag the dylib in, add its header path, a bridging header, and start using it. But SPM is a completely different confusing beast...
I tried taking the SDL2 and converting it into an xcframework, since there are a couple of SDL2 Swift libraries out there I could use as guides. I've tried both converting the SDL2.framework and using Homebrew's SDL2 install as a basis (both for dylib and static .a). For example, here's how I created an xcframework from the original SDL2.framework:
xcodebuild -create-xcframework -framework SDL2.framework -output SDL2.xcframework
Then I created this Package.swift:
let package = Package(
name: "SDL",
platforms: [
.macOS(.v10_14)
],
products: [
.library(
name: "SDL",
targets: ["SDL"]),
],
targets: [
.target(
name: "SDL",
dependencies: ["SDL2"]),
.binaryTarget(
name: "SDL2",
path: "SDL2.xcframework"),
.testTarget(
name: "SDLTests",
dependencies: ["SDL"]),
]
)
But if I try this in my SDL package's SDL.swift
file:
import SDL2
I get a "no such module" error.
Any pointers as how to properly do this? I feel like this should be a simple thing at this point, given how easy it was in Xcode. But every example and every article describes a different way to do things, most of which I can't get to work.
Thanks.