When building a Swift Package library project generating .dylib (.so on Linux or .dll on Windows) you will will find .swiftinterface and .swiftmodule files in the build directory. These two files with the .dylib can be placed somewhere and used from a SwiftPM executable project.
For example, I have build a library named MAXComKit and placed in /usr/local/lib/MAXComKit the 3 files:
libMAXComkit.dylib
MAXComKit.swiftinterface
MAXComKit.swiftmodule
In addition, if you have to link to a C library, you must add a module.modulemap file and a header folder to the 3 previously mentioned files. The module.modulemap file will look like this:
module MXFoundation {
header "headers/MXFoundation.h"
link "maxtmxf"
export *
}
header with the path to the header folder containing the .h files.
link with the name of the C library to link with
And I have a Swift Package executable project like this:
// swift-tools-version: 5.5
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
// MAXComKit module located in /usr/local/lib
let maxcomSwiftSettings : [SwiftSetting] = [ .unsafeFlags(["-I/usr/local/lib/MAXComKit"]) ]
// Link with MAXComKit library
let maxcomLinkerSettings : [LinkerSetting] = [ .unsafeFlags(["-L/usr/local/lib", "-lMAXComKit"]) ]
let package = Package(
name: "MAXComKitDemos",
products: [
.executable(
name: "ClientReceiveSignals",
targets: ["ClientReceiveSignals"])
],
dependencies: [],
targets: [
.executableTarget(
name: "ClientReceiveSignals",
swiftSettings: maxcomSwiftSettings,
linkerSettings: maxcomLinkerSettings)
]
)
The keys are the SwiftSetting and LinkerSetting flags.
In executable project, the main.swift begin with:
//
// MAXCom ClientReceiveSignals
// main.swift
//
import Foundation
import MAXComKit
print("Hello MAXComKit Client Swift API!")
The library is imported and used.
I follow the same pattern to build Swift modules .dll on Windows and .so on Linux then access from executable project with proper SwiftSetting and LinkerSetting.
I hope this can help you.