Jomy10
(Jonas Everaert)
1
I want to link a library built with the Swift Package Manager to a C program.
I have the following Package.swift
// swift-tools-version: 5.7
import PackageDescription
let package = Package(
name: "TestPackage",
products: [
.library(
name: "TestPackage",
type: .static,
targets: ["TestPackage"])
],
dependencies: [],
targets: [
.target(
name: "TestPackage",
dependencies: []),
]
)
I can build TestPackage into a static library.
I then use it in a C executable and link the library:
clang main.c -LTestPackge/.build/debug -lTestPackage
This gave me a lot of errors about undefined symbols, so I added the swift libraries location:
clang main.c -LTestPackage/.build/debug -lTestPackage \
-L/Library/Developer/Toolchains/swift-latest.xctoolchain/usr/lib/swift/macosx
This compiled and I could run the executable, great!
Now I want this in a github action, but it uses a different path. Is there a cross-platform way of detemining this path? (i.e. for both macOS and Linux)
Jomy10
(Jonas Everaert)
2
I found how to do this mostly; you can use swiftc -print-target-info.
Here's a little ruby script that will print out all the flags you need:
puts target_info["paths"]["runtimeLibraryPaths"]
.map { |path| "-L#{path}" }.join(" ") + " " +
target_info["paths"]["runtimeLibraryImportPaths"]
.map { |path| "-L#{path}" }.join(" ")
However, on linux I'm still getting a few errors:
/usr/bin/ld: /home/runner/work/beaver/beaver/tests/swift-project/TestPackage/.build/debug/libTestPackage.a(TestPackage.swift.o): in function `$s11TestPackage10test_swiftSPys4Int8VGyF':
/home/runner/work/beaver/beaver/tests/swift-project/TestPackage/Sources/TestPackage/TestPackage.swift:4: undefined reference to `$sSS21_builtinStringLiteral17utf8CodeUnitCount7isASCIISSBp_BwBi1_tcfC'
/usr/bin/ld: /home/runner/work/beaver/beaver/tests/swift-project/TestPackage/Sources/TestPackage/TestPackage.swift:4: undefined reference to `$sSS11withCStringyxxSPys4Int8VGKXEKlF'
/usr/bin/ld: /home/runner/work/beaver/beaver/tests/swift-project/TestPackage/Sources/TestPackage/TestPackage.swift:4: undefined reference to `swift_bridgeObjectRelease'
/usr/bin/ld: /home/runner/work/beaver/beaver/tests/swift-project/TestPackage/.build/debug/libTestPackage.a(TestPackage.swift.o): in function `__swift_instantiateConcreteTypeFromMangledName':
TestPackage.swift.o:(.text+0x103): undefined reference to `swift_getTypeByMangledNameInContext2'
/usr/bin/ld: /home/runner/work/beaver/beaver/tests/swift-project/TestPackage/.build/debug/libTestPackage.a(TestPackage.swift.o):(.data.rel.ro+0x0): undefined reference to `$ss4Int8VMn'
collect2: error: ld returned 1 exit status
Anyone have any ideas how to fix these last linking errors?