Use static C library with Swift package manager

I would like to use the Swift package manager to include a static C library in my build. The Apple documentation shows the process with shared libraries, but I hope the static library will be used there. I can reference static libraries using swiftc, so it seems reasonable to hope that I can do this using swift build.

I have a small problem with an example that uses the C static library containing a function to multiply two numbers. (Note: I am running Swift 3 Preview 1 on Linux)

A directory structure that looks like this:

example/
    Package.swift
    main.swift
    .git/

CMult/
    Package.swift
    module.modulemap
    mult.h
    libmult.a
    .git/

The contents of the example /Package.swift:

import PackageDescription

let package = Package(
    name: "example",
    dependencies: [
        .Package(url: "../CMult", majorVersion: 1)
    ]
)

The contents of the /main.swift example:

import CMult

let n: Int32 = mult2(3, 5) // "int mult2(int,int)" is in mult.h and libmult.a
print("Result = \(n)")

Contents of CMult / Package.swift:

import PackageDescription

let package = Package(
    name: "CMult"
)

CMult/module.modulemap:

module CMult [system] {
    header "./mult.h"
    link "mult"
    export *
}

, example$ swift build, : "/usr/bin/ld: -lmult", , , . , -L , , .a( .a swiftc). , , , LD_LIBRARY_PATH CMult/, .

? .

+4
1

, , :

example$ swift build -Xlinker -L../CMult

,

swift build --help

LD_LIBRARY_PATH, . static LIBRARY_PATH, swift build, . gcc.

+2

Source: https://habr.com/ru/post/1648713/


All Articles