简体   繁体   中英

Swift package wrap a C library for macOS and Linux

I create a Firebird Swift client library for macOS and Linux, but I need to link the Firebird C library, depending of the system target.

Currently, my library bundle the Firebird Framework for macOS using a xcframework, but I'm unable to do it for Linux, since xcframework only support macOS

I have the following directories:

CFirebird
|- Package.swift
|- Headers
|  |- ibase.h
|  |- iberror.h
|
|- Libraries
|  |- macos
|  |  |- Firebird.xcframework
|  |- linux
|     |- libfbclient.so (and the others libs from firebird)
|
|- Sources
   |- CFirebird
      |- module.modulemap

I have a modulemap for exporting the library

module CFirebird {
    umbrella "../../Headers"
    export *
    link "fbclient"
}

And my Package.swift

let package = Package(
    name: "CFirebird",
    platforms: [
        .macOS(.v10_15),
    ],
    products: [
        .executable(
            name: "Firebird",
            targets: ["Firebird"])
    ],
    targets: [
        .binaryTarget(name: "CFirebird-macos", path: "Libraries/macos/Firebird.xcframework"),
        .systemLibrary(name: "CFirebird"),
        .target(
            name: "Firebird",
            dependencies: [
                "CFirebird",
                .target(name: "CFirebird-macos", condition: .when(platforms: [.macOS])),
            ],
            linkerSettings: [
                .linkedLibrary("fbclient", .when(platforms: [.linux])),
            ]),
    ]
)

The only way I founded to make it work on Linux is install the Firebird library on the system, but I want to bundle it with the framework.

Is that possible? Did I make some mistakes?

Ok, the approach I took was to rely on the user to link the Firebird library, as "Arioch 'The" said. The correct way to do it (in my opinion) is to specify the pkg-config file used for swift. In my case, I would have been libfbclient.pc .

To make swift use pkg-config, replace the following in the package description target:

.systemLibrary(name: "CFirebird", pkgConfig: "libfbclient")

Swift will retrieve the flags from pkg-config to make compilation possible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM