导入使用C库斯威夫特模块 [英] Importing a Swift module using a C library

查看:130
本文介绍了导入使用C库斯威夫特模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过以下的<一个指令写了一个诅咒包装器模块href=\"https://github.com/apple/swift-package-manager/blob/13d682a63ea01246dd119cd4cf5c8d90c030566d/Documentation/SystemModules.md\"相对=nofollow>此页面。

不过,我不知道如何正确地使它成为一个模块中使用它。

However, I do not know how to properly make it work in a module using it.

我有3个平行的目录:CCurses,CursesWrapper,ModuleUsingCursesWrapper

I have 3 parallel directories: CCurses, CursesWrapper, ModuleUsingCursesWrapper.

CCurses包含一个空Package.swift文件,包含module.modulemap文件

CCurses contains an empty Package.swift file and a module.modulemap file containing

module CCurses [system] {
    header "/usr/include/curses.h"
    link "curses"
    export *
}

CursesWrapper包含含有一个Package.swift文件

CursesWrapper contains a Package.swift file containing.

import PackageDescription

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

和含general.swift文件

and a general.swift file containing

import CCurses

public func startCurses() -> Void{
    initscr()}

public func getchar() -> Void{
    getch()}

public func endCurses() -> Void{
    endwin()}

ModuleUsingCursesWrapper包括含有Package.swift文件

ModuleUsingCursesWrapper contains a Package.swift file containing

import PackageDescription

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

和含main.swift文件

and a main.swift file containing

import CursesWrapper

startCurses()
getchar()
endCurses()

当在ModuleUsingCursesWrapper运行迅速构建,我得到以下的输出:

When running swift build in ModuleUsingCursesWrapper, I get the following output:

克隆包/ CursesWrapper

Cloning Packages/CursesWrapper

克隆包/ CCurses

Cloning Packages/CCurses

编译斯威夫特模块CursesWrapper(1来源)

Compiling Swift Module 'CursesWrapper' (1 sources)

链接库:.build /调试/ CursesWrapper.a

Linking Library: .build/debug/CursesWrapper.a

编译斯威夫特模块ModuleUsingCursesWrapper(1来源)

Compiling Swift Module 'ModuleUsingCursesWrapper' (1 sources)

链接可执行文件:.build /调试/ ModuleUsingCursesWrapper

Linking Executable: .build/debug/ModuleUsingCursesWrapper

... / ModuleUsingCursesWrapper / .build /调试/ CursesWrapper.a(general.swift.o):在函数'_TF14CursesWrapper11startCursesFT_T _:

.../ModuleUsingCursesWrapper/.build/debug/CursesWrapper.a(general.swift.o): In function `_TF14CursesWrapper11startCursesFT_T_':

... / ModuleUsingCursesWrapper /封装/ CursesWrapper-1.0.0 / general.swift:5:未定义引用`initscr的

.../ModuleUsingCursesWrapper/Packages/CursesWrapper-1.0.0/general.swift:5: undefined reference to `initscr'

... / ModuleUsingCursesWrapper / .build /调试/ CursesWrapper.a(general.swift.o):在函数'_TF1CursesWrapper7getcharFT_T _:

.../ModuleUsingCursesWrapper/.build/debug/CursesWrapper.a(general.swift.o): In function `_TF1CursesWrapper7getcharFT_T_':

... / ModuleUsingCursesWrapper /封装/ CursesWrapper-1.0.0 / general.swift:8:未定义引用`残培

.../ModuleUsingCursesWrapper/Packages/CursesWrapper-1.0.0/general.swift:8: undefined reference to `getch'

... / ModuleUsingCursesWrapper / .build /调试/ CursesWrapper.a(general.swift.o):在函数'_TF14CursesWrapper9endCursesFT_T _:

.../ModuleUsingCursesWrapper/.build/debug/CursesWrapper.a(general.swift.o): In function `_TF14CursesWrapper9endCursesFT_T_':

... / ModuleUsingCursesWrapper /封装/ CursesWrapper-1.0.0 / general.swift:11:未定义的参考`endwin

.../ModuleUsingCursesWrapper/Packages/CursesWrapper-1.0.0/general.swift:11: undefined reference to `endwin'

铛:错误:连接命令,退出code 1(使用-v看看调用)

clang: error: linker command failed with exit code 1 (use -v to see invocation)

&LT;未知> 0:错误:链接命令,退出code 1失败(使用-v看看调用)

< unknown >:0: error: link command failed with exit code 1 (use -v to see invocation)

&LT;未知0:错误:构建有1命令失败

< unknown :0: error: build had 1 command failures

迅速的构建:出口(1):... /雨燕的/ usr / bin中/快捷的构建工具,F,... / ModuleUsingCursesWrapper / .build /调试/ ModuleUsingCursesWrapper。 O / llbuild.yaml]

swift-build: exit(1): [".../Swift/usr/bin/swift-build-tool", "-f", ".../ModuleUsingCursesWrapper/.build/debug/ModuleUsingCursesWrapper.o/llbuild.yaml"]

似乎编译ModuleUsingCursesWrapper时,诅咒功能无法找到。我怎样才能解决这个问题?

It seems that when compiling ModuleUsingCursesWrapper, the curses functions cannot be found. How can I solve this problem ?

(我在Linux上)

推荐答案

你的榜样玩了一段时间的Ubuntu 14.04框后,我遇到了同样的错误。经过一些额外实验,我可以通过添加以下来解决它 main.swift

After playing with your example for awhile on an Ubuntu 14.04 box, I ran into the same error. After some additional experimentation, I was able to solve it by adding the following to main.swift:

import CCurses

使用包装只引用CursesWrapper模块的 Package.swift 文件,但如果 CCurses 将不导入到 main.swift ,则会发生未定义的引用错误。

The Package.swift file of the module using the wrapper references only CursesWrapper, but if CCurses is not imported into main.swift, then the undefined reference error occurs.

导入进口模块的依赖关系的必要性可以从链接swift.org您提供,其中碧玉/ JPEG例子讨论中可以看出。严格地说,碧玉不在身边JPEG的包装,但只是另一种系统模块,但想法是一样的:如果我们进口依赖模块B模块A,那么我们也应导入模块B

The need to import dependencies of imported modules can be seen from the link to swift.org that you provided, where a JasPer/JPEG example is discussed. Strictly speaking, JasPer is not a wrapper around JPEG, but is just another system module, yet the idea is the same: if we import module A that depends on module B, then we should also import module B.

BTW,我不得不改变公共FUNC的getchar()公共FUNC的getchar()并修改 main.swift 相应。否则,编译时我会得到暧昧使用'的getchar()错误 main.swift 。显然它是在 CCurses 已经定义,这并不奇怪,因为它是一个标准的C函数。

BTW I had to change public func getchar() to public func getChar() and modify main.swift accordingly. Otherwise I would get ambiguous use of 'getchar()' error when compiling main.swift. Evidently it is already defined in CCurses, which is no surprise as it is a standard C function.

这篇关于导入使用C库斯威夫特模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆