I have a single file Swift command line script (it dumps the contents of the menu bar given a process id).
我有一个单独的文件Swift命令行脚本(它转储给定进程ID的菜单栏的内容)。
To give an idea of the APIs I am using, here's a few relevant lines:
要了解我正在使用的API,这里有几个相关的行:
import Foundation
import Cocoa
// ...
func getAttribute(element: AXUIElement, name: String) -> CFTypeRef? {
var value: CFTypeRef? = nil
AXUIElementCopyAttributeValue(element, name as CFString, &value)
return value
}
// ...
var app: NSRunningApplication? = nil
if pid == -1 {
app = NSWorkspace.shared().menuBarOwningApplication
}
else {
app = NSRunningApplication(processIdentifier: pid)
}
// ...
let axApp = AXUIElementCreateApplication(app.processIdentifier)
The whole file is available here.
整个文件在这里可用。
When I compile this using swiftc menu.swift
, I can run it just fine in my system which has Swift installed.
当我使用swiftc menu.swift编译它时,我可以在安装了Swift的系统中运行它。
When I share the menu
executable to somebody who does not have Swift, they get the following error when running it via Terminal:
当我将菜单可执行文件共享给没有Swift的人时,他们通过终端运行时会出现以下错误:
Code 6: dyld: Library not loaded: @rpath/libswiftAppKit.dylib
Referenced from: ./menu
Reason: image not found
I figure I need to statically link something, but I am not sure. I cannot test this easily since I do not have access to a macOS build without Swift.
我想我需要静态链接一些东西,但我不确定。我无法轻松测试,因为没有Swift,我无法访问macOS构建。
How would I use swiftc
so that I can compile my script such that it can be run on any macOS system?
我如何使用swiftc以便我可以编译我的脚本以便它可以在任何macOS系统上运行?
1 个解决方案
#1
2
You can solve this for the cases where you only use the standard library using -static-stdlib
.
对于仅使用-static-stdlib使用标准库的情况,可以解决此问题。
When you compile a script with no options, the final executable contains rpaths to the various Swift standard libs, which you can verify using otool
.
当您编译没有选项的脚本时,最终的可执行文件包含各种Swift标准库的rpath,您可以使用otool进行验证。
> swiftc menu.swift
> otool -L menu
menu:
/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 1348.28.0)
/usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1238.0.0)
/System/Library/Frameworks/AppKit.framework/Versions/C/AppKit (compatibility version 45.0.0, current version 1504.75.0)
/System/Library/Frameworks/ApplicationServices.framework/Versions/A/ApplicationServices (compatibility version 1.0.0, current version 48.0.0)
/System/Library/Frameworks/Foundation.framework/Versions/C/Foundation (compatibility version 300.0.0, current version 1349.25.0)
@rpath/libswiftAppKit.dylib (compatibility version 1.0.0, current version 800.0.63)
@rpath/libswiftCore.dylib (compatibility version 1.0.0, current version 800.0.63)
@rpath/libswiftCoreData.dylib (compatibility version 1.0.0, current version 800.0.63)
@rpath/libswiftCoreGraphics.dylib (compatibility version 1.0.0, current version 800.0.63)
@rpath/libswiftCoreImage.dylib (compatibility version 1.0.0, current version 800.0.63)
@rpath/libswiftDarwin.dylib (compatibility version 1.0.0, current version 800.0.63)
@rpath/libswiftDispatch.dylib (compatibility version 1.0.0, current version 800.0.63)
@rpath/libswiftFoundation.dylib (compatibility version 1.0.0, current version 800.0.63)
@rpath/libswiftIOKit.dylib (compatibility version 1.0.0, current version 800.0.63)
@rpath/libswiftObjectiveC.dylib (compatibility version 1.0.0, current version 800.0.63)
@rpath/libswiftQuartzCore.dylib (compatibility version 1.0.0, current version 800.0.63)
@rpath/libswiftSwiftOnoneSupport.dylib (compatibility version 1.0.0, current version 800.0.63)
@rpath/libswiftXPC.dylib (compatibility version 1.0.0, current version 800.0.63)
Using -static-stdlib
ensures that the standard libraries are linked to as required.
使用-static-stdlib可确保根据需要链接标准库。
> swiftc -static-stdlib menu.swift
> otool -L menu
menu:
/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 1348.28.0)
/usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1238.0.0)
/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 307.4.0)
/System/Library/Frameworks/Foundation.framework/Versions/C/Foundation (compatibility version 300.0.0, current version 1349.25.0)
/System/Library/Frameworks/AppKit.framework/Versions/C/AppKit (compatibility version 45.0.0, current version 1504.75.0)
/System/Library/Frameworks/ApplicationServices.framework/Versions/A/ApplicationServices (compatibility version 1.0.0, current version 48.0.0)
/System/Library/Frameworks/CoreData.framework/Versions/A/CoreData (compatibility version 1.0.0, current version 752.8.0)
/System/Library/Frameworks/CoreGraphics.framework/Versions/A/CoreGraphics (compatibility version 64.0.0, current version 1070.13.0)
>
I still do not know how to link 3rd party frameworks - but the above steps solves my original problem.
我仍然不知道如何链接第三方框架 - 但上述步骤解决了我原来的问题。
Related Linux Question - Compile Swift script with static Swift core library
相关Linux问题 - 使用静态Swift核心库编译Swift脚本
#1
2
You can solve this for the cases where you only use the standard library using -static-stdlib
.
对于仅使用-static-stdlib使用标准库的情况,可以解决此问题。
When you compile a script with no options, the final executable contains rpaths to the various Swift standard libs, which you can verify using otool
.
当您编译没有选项的脚本时,最终的可执行文件包含各种Swift标准库的rpath,您可以使用otool进行验证。
> swiftc menu.swift
> otool -L menu
menu:
/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 1348.28.0)
/usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1238.0.0)
/System/Library/Frameworks/AppKit.framework/Versions/C/AppKit (compatibility version 45.0.0, current version 1504.75.0)
/System/Library/Frameworks/ApplicationServices.framework/Versions/A/ApplicationServices (compatibility version 1.0.0, current version 48.0.0)
/System/Library/Frameworks/Foundation.framework/Versions/C/Foundation (compatibility version 300.0.0, current version 1349.25.0)
@rpath/libswiftAppKit.dylib (compatibility version 1.0.0, current version 800.0.63)
@rpath/libswiftCore.dylib (compatibility version 1.0.0, current version 800.0.63)
@rpath/libswiftCoreData.dylib (compatibility version 1.0.0, current version 800.0.63)
@rpath/libswiftCoreGraphics.dylib (compatibility version 1.0.0, current version 800.0.63)
@rpath/libswiftCoreImage.dylib (compatibility version 1.0.0, current version 800.0.63)
@rpath/libswiftDarwin.dylib (compatibility version 1.0.0, current version 800.0.63)
@rpath/libswiftDispatch.dylib (compatibility version 1.0.0, current version 800.0.63)
@rpath/libswiftFoundation.dylib (compatibility version 1.0.0, current version 800.0.63)
@rpath/libswiftIOKit.dylib (compatibility version 1.0.0, current version 800.0.63)
@rpath/libswiftObjectiveC.dylib (compatibility version 1.0.0, current version 800.0.63)
@rpath/libswiftQuartzCore.dylib (compatibility version 1.0.0, current version 800.0.63)
@rpath/libswiftSwiftOnoneSupport.dylib (compatibility version 1.0.0, current version 800.0.63)
@rpath/libswiftXPC.dylib (compatibility version 1.0.0, current version 800.0.63)
Using -static-stdlib
ensures that the standard libraries are linked to as required.
使用-static-stdlib可确保根据需要链接标准库。
> swiftc -static-stdlib menu.swift
> otool -L menu
menu:
/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 1348.28.0)
/usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1238.0.0)
/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 307.4.0)
/System/Library/Frameworks/Foundation.framework/Versions/C/Foundation (compatibility version 300.0.0, current version 1349.25.0)
/System/Library/Frameworks/AppKit.framework/Versions/C/AppKit (compatibility version 45.0.0, current version 1504.75.0)
/System/Library/Frameworks/ApplicationServices.framework/Versions/A/ApplicationServices (compatibility version 1.0.0, current version 48.0.0)
/System/Library/Frameworks/CoreData.framework/Versions/A/CoreData (compatibility version 1.0.0, current version 752.8.0)
/System/Library/Frameworks/CoreGraphics.framework/Versions/A/CoreGraphics (compatibility version 64.0.0, current version 1070.13.0)
>
I still do not know how to link 3rd party frameworks - but the above steps solves my original problem.
我仍然不知道如何链接第三方框架 - 但上述步骤解决了我原来的问题。
Related Linux Question - Compile Swift script with static Swift core library
相关Linux问题 - 使用静态Swift核心库编译Swift脚本