我想在 swift 3 中构建和使用静态库 (.a)。示例:我构建了一个 lib helloLib.a,并使用它。
你好.c
#include <stdio.h>
#include "hello.h"
int printHello()
{
printf("hello wourl");
return 0;
}
你好.h
#include <stdio.h>
int printHello();
构建为:libHello.a 并复制到/usr/local/lib
快速代码
模块.模块映射
module hello [system] {
header "hello.h"
link "libhello"
export *
}
Package.swift
import PackageDescription
let package = Package(
name: "hello",
dependencies: []
)
使用模块 hello
主.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
最佳答案
我认为你遗漏了很多关于你所做的事情的信息,这使得很难提供肯定的答案。您是否按照 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
选项。
希望这对您有所帮助。
关于c - 如何在 swift 3 中使用我的静态库 (.a),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39818489/