如何在 swift 3 中使用我的静态库 (.a)
How to use my static library (.a) in swift 3
我想在 swift 中构建和使用静态库 (.a) 3. 例如:我构建了一个
lib helloLib.a,并使用它。
hello.c
#include <stdio.h>
#include "hello.h"
int printHello()
{
printf("hello wourl");
return 0;
}
hello.h
#include <stdio.h>
int printHello();
生成:libHello.a 并复制到 /usr/local/lib
代码swift
module.modulemap
module hello [system] {
header "hello.h"
link "libhello"
export *
}
Package.swift
import PackageDescription
let package = Package(
name: "hello",
dependencies: []
)
使用模块 hello
main.swift
import hello
printHello()
使用 swift 构建(命令):swift 构建
得到一个错误:
Compile Swift Module 'usehello' (1 sources)
Linking ./.build/debug/usehello
ld: library not found for -llibhello for architecture x86_64
:0: error: link command failed with exit code 1 (use -v to see invocation)
:0: error: build had 1 command failures
我认为它没有在 /usr/local/lib 中找到您的静态库。您应该使用编译器标志进行构建,例如:
swift build -Xcc -I/usr/local/include -Xlinker -L/usr/local/lib
我认为你遗漏了很多关于你所做的事情的信息,这使得很难提供肯定的答案。您是否按照 https://github.com/apple/swift-package-manager/blob/master/Documentation/Usage.md 的思路做了什么?你的目录结构是什么? hello.h在哪里?
总之,从报错信息来看,有一个问题就是你使用了
link "libhello"
在 module.modulemap
中。不清楚静态库的名称是什么。它不能被称为helloLib.a
,它的名字必须以lib
开头。如果它被称为 libhelloLib.a
,那么在模块映射中它必须是
link "helloLib"
您可能还想按照另一个答案中的建议添加 -Xlinker -L/usr/local/lib
选项。
希望对您有所帮助。
我想在 swift 中构建和使用静态库 (.a) 3. 例如:我构建了一个 lib helloLib.a,并使用它。
hello.c
#include <stdio.h>
#include "hello.h"
int printHello()
{
printf("hello wourl");
return 0;
}
hello.h
#include <stdio.h>
int printHello();
生成:libHello.a 并复制到 /usr/local/lib
代码swift
module.modulemap
module hello [system] {
header "hello.h"
link "libhello"
export *
}
Package.swift
import PackageDescription
let package = Package(
name: "hello",
dependencies: []
)
使用模块 hello
main.swift
import hello
printHello()
使用 swift 构建(命令):swift 构建
得到一个错误:
Compile Swift Module 'usehello' (1 sources)
Linking ./.build/debug/usehello
ld: library not found for -llibhello for architecture x86_64
:0: error: link command failed with exit code 1 (use -v to see invocation)
:0: error: build had 1 command failures
我认为它没有在 /usr/local/lib 中找到您的静态库。您应该使用编译器标志进行构建,例如:
swift build -Xcc -I/usr/local/include -Xlinker -L/usr/local/lib
我认为你遗漏了很多关于你所做的事情的信息,这使得很难提供肯定的答案。您是否按照 https://github.com/apple/swift-package-manager/blob/master/Documentation/Usage.md 的思路做了什么?你的目录结构是什么? hello.h在哪里?
总之,从报错信息来看,有一个问题就是你使用了
link "libhello"
在 module.modulemap
中。不清楚静态库的名称是什么。它不能被称为helloLib.a
,它的名字必须以lib
开头。如果它被称为 libhelloLib.a
,那么在模块映射中它必须是
link "helloLib"
您可能还想按照另一个答案中的建议添加 -Xlinker -L/usr/local/lib
选项。
希望对您有所帮助。