c - 如何在 swift 3 中使用我的静态库 (.a)

标签 c swift static-libraries

我想在 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/

相关文章:

c - 我正在尝试编写一个 C 程序来打印整数的字符串形式。

swift - JSQMessagesCollectionView(消息气泡大小全部相同..)

c++ - 未使用的链接器输入文件 c++ g++ make 文件

c++ - 为什么链接到 LIB 会显着增加二进制文件的大小

c - 从文件读取并存储到结构数组中

c++ - 如何返回复杂的返回值?

ios - 从 MKPolyline 创建 MKMapView 区域

c++ - g++,创建用于分发的静态库

我可以在 C 中将 0.16、0.32 等正确转换为整数(16、32 等)吗?

php - 我需要使用 PHP 在电子邮件中从我的应用程序中获取一个数组