【问题标题】:Compile Swift script with static Swift core library使用静态 Swift 核心库编译 Swift 脚本
【发布时间】:2016-09-21 11:10:03
【问题描述】:

我想在没有安装 Swift 的计算机上执行一个已编译的 Swift 脚本。但默认情况下,Swift 编译器似乎使用动态链接库。 是否可以静态包含这些库?

使用 Docker 重现的步骤:

user@host:~# docker run -it swiftdocker/swift
root@16974ad4edb1:/# swift --version
Swift version 3.0-dev (LLVM dffa09ffd8, Clang 9f0d189820, Swift 1c720b8f84)
Target: x86_64-unknown-linux-gnu
root@16974ad4edb1:/# echo 'print("Hello, world")' > helloworld.swift
root@16974ad4edb1:/# swiftc helloworld.swift
root@16974ad4edb1:/# ./helloworld
Hello, world
root@16974ad4edb1:/# exit
exit
user@host:~# docker cp 16974ad4edb1:/helloworld .
user@host:~# file helloworld
helloworld: ELF 64-bit LSB  executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=c4d42f6185b3d53ecf2dfb0c386263c17cb34675, not stripped
user@host:~# ./helloworld
./helloworld: error while loading shared libraries: libswiftCore.so: cannot open shared object file: No such file or directory

【问题讨论】:

    标签: linux swift compilation


    【解决方案1】:

    必须使用参数-static-stdlib

    swiftc -static-stdlib helloworld.swift
    

    对于swift build,将其传递给-Xswiftc

    swift build -Xswiftc -static-stdlib
    

    如果遇到 ICU 依赖错误,请安装它:

    apt-get install libicu-dev
    

    【讨论】:

    • 值得注意的是,这个标志是only available from DEVELOPMENT-SNAPSHOT-2016-05-09-a,即不在早期的 3.0 预览版或 2.x RELEASE 中。
    • 就像@sas 已经提到的那样,这在 swift 2.3 工具链中不起作用:(
    • -static-stdlib 给出错误:ld: warning: directory not found for option '-L/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift_static/macosx'。也许 Swift 5 删除了swift_static
    • 这是跟踪bugs.swift.org/browse/SR-648的错误
    猜你喜欢
    • 1970-01-01
    • 2015-06-22
    • 2016-05-24
    • 2017-03-16
    • 1970-01-01
    • 2014-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多