xcrun swift在命令行上生成<unknown>:0:错误:无法加载共享库

 让生活洒满阳光_622 发布于 2022-12-21 11:21

我的目标是尝试像脚本一样运行我的Swift程序.如果整个程序是自包含的,你可以运行它:

% xcrun swift hello.swift

hello.swift在哪里

import Cocoa
println("hello")

但是,我想超越一步,包括swift模块,我可以导入其他类,函数等.

所以我们假设我们想要在GoodClass.swift中使用一个非常好的类

public class GoodClass {
    public init() {}
    public func sayHello() {
        println("hello")
    }
}

我现在想把这个好东西导入我的hello.swift:

import Cocoa
import GoodClass

let myGoodClass = GoodClass()
myGoodClass.sayHello()

我首先通过运行以下命令生成.o,lib <>.a,.swiftmodule:

% xcrun swiftc -emit-library -emit-object GoodClass.swift -sdk $(xcrun --show-sdk-path --sdk macosx) -module-name GoodClass
% ar rcs libGoodClass.a GoodClass.o
% xcrun swiftc -emit-module GoodClass.swift -sdk $(xcrun --show-sdk-path --sdk macosx) -module-name GoodClass

最后,我准备运行我的hello.swift(好像它是一个脚本):

% xcrun swift -I "./" -L "./" -lGoodClass -sdk $(xcrun --show-sdk-path --sdk macosx) hello.swift 

但我得到了这个错误:

:0:错误:无法加载共享库'libGoodClass'

这是什么意思?我错过了什么 如果我继续,并做链接/编译的东西类似于你为C/C++做的事情:

% xcrun swiftc -o hello -I "./" -L "./" -lGoodClass -sdk $(xcrun --show-sdk-path --sdk macosx) hello.swift
% ./hello

一切都很开心.我想我可以忍受,但仍然想了解共享库错误.

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有