Swift for OS X编译Linux?

时间:2022-07-12 02:38:32

I'm confused by the build process for Swift on other platforms.

我对其他平台上Swift的构建过程感到困惑。

Does Swift allow me to build a Linux project on OS X, or do I need to use Swift specifically on Linux to to build anything I plan on using there?

Swift是否允许我在OS X上构建Linux项目,或者我是否需要在Linux上专门使用Swift来构建我计划在那里使用的任何东西?

I looked at the documentation, but it's not really clear on this topic...

我查看了文档,但这个主题并不是很清楚......

2 个解决方案

#1


6  

A pure Swift application which is not importing any framework can now be compiled for iOS, OS X and for Linux.

现在可以为iOS,OS X和Linux编译不导入任何框架的纯Swift应用程序。

You will generate different executables, because it's different platforms, but the code source can be the same, it just has to be compiled for the respective platform.

您将生成不同的可执行文件,因为它是不同的平台,但代码源可以是相同的,它只需要为各自的平台编译。

The difference is when you import frameworks.

不同之处在于导入框架时。

If you import UIKit to make an iOS application, obviously you won't be able to compile this on Linux, because Linux doesn't have those iOS UIKit libraries, they're only available on a Mac via Xcode.

如果您导入UIKit来制作iOS应用程序,显然您将无法在Linux上编译它,因为Linux没有那些iOS UIKit库,它们只能通过Xcode在Mac上使用。

If you import Cocoa to make an OS X application, the same logic applies, it works only for OS X.

如果导入Cocoa以生成OS X应用程序,则应用相同的逻辑,它仅适用于OS X.

The same way, you can make Swift code that only works on Linux if you import specific Linux libraries, like Glibc, that won't work on the Mac.

同样,如果导入特定的Linux库(如Glibc),那么只能在Linux上运行Swift代码才能在Linux上运行。

Etc, etc.

等等

What you need to remember is that the programming language isn't that relevant when it comes to make applications for a platform, it's the frameworks and libraries that you're linked to that are decisive.

您需要记住的是,编程语言在为平台创建应用程序时并不相关,而是您链接到的框架和库是决定性的。

Different Swift versions come with different compilers (different toolchains, actually) so to answer directly, no you can't compile for Linux Swift with the normal OS X Swift compiler, you have to use the Swift.org's one.

不同的Swift版本带有不同的编译器(实际上是不同的工具链),所以要直接回答,不能用普通的OS X Swift编译器编译Linux Swift,你必须使用Swift.org的编译器。

#2


3  

The Swift Package Manager got custom toolchain support via PR-1098 end of April 2017.

Swift Package Manager在2017年4月底通过PR-1098获得了定制工具链支持。

This allows you to setup cross compiler toolchains (macOS to Ubuntu, even the reverse). The SwiftPM repo contains an example script which sets up a Intel-macOS => Intel-Ubuntu toolchain.

这允许您设置交叉编译器工具链(macOS到Ubuntu,甚至相反)。 SwiftPM repo包含一个示例脚本,用于设置Intel-macOS => Intel-Ubuntu工具链。

As Eric says, you can of course only use frameworks available on the target platform. But yes, you can now build a Linux Swift project on macOS. (as mentioned by joels in the comment you can also do this via Docker very easily)

正如Eric所说,您当然只能使用目标平台上提供的框架。但是,是的,您现在可以在macOS上构建Linux Swift项目。 (正如评论中的joels所提到的,你也可以通过Docker很容易地做到这一点)

Related: (Cross-)Compiling Swift for Raspberry PI

相关:(Cross-)为Raspberry PI编译Swift

#1


6  

A pure Swift application which is not importing any framework can now be compiled for iOS, OS X and for Linux.

现在可以为iOS,OS X和Linux编译不导入任何框架的纯Swift应用程序。

You will generate different executables, because it's different platforms, but the code source can be the same, it just has to be compiled for the respective platform.

您将生成不同的可执行文件,因为它是不同的平台,但代码源可以是相同的,它只需要为各自的平台编译。

The difference is when you import frameworks.

不同之处在于导入框架时。

If you import UIKit to make an iOS application, obviously you won't be able to compile this on Linux, because Linux doesn't have those iOS UIKit libraries, they're only available on a Mac via Xcode.

如果您导入UIKit来制作iOS应用程序,显然您将无法在Linux上编译它,因为Linux没有那些iOS UIKit库,它们只能通过Xcode在Mac上使用。

If you import Cocoa to make an OS X application, the same logic applies, it works only for OS X.

如果导入Cocoa以生成OS X应用程序,则应用相同的逻辑,它仅适用于OS X.

The same way, you can make Swift code that only works on Linux if you import specific Linux libraries, like Glibc, that won't work on the Mac.

同样,如果导入特定的Linux库(如Glibc),那么只能在Linux上运行Swift代码才能在Linux上运行。

Etc, etc.

等等

What you need to remember is that the programming language isn't that relevant when it comes to make applications for a platform, it's the frameworks and libraries that you're linked to that are decisive.

您需要记住的是,编程语言在为平台创建应用程序时并不相关,而是您链接到的框架和库是决定性的。

Different Swift versions come with different compilers (different toolchains, actually) so to answer directly, no you can't compile for Linux Swift with the normal OS X Swift compiler, you have to use the Swift.org's one.

不同的Swift版本带有不同的编译器(实际上是不同的工具链),所以要直接回答,不能用普通的OS X Swift编译器编译Linux Swift,你必须使用Swift.org的编译器。

#2


3  

The Swift Package Manager got custom toolchain support via PR-1098 end of April 2017.

Swift Package Manager在2017年4月底通过PR-1098获得了定制工具链支持。

This allows you to setup cross compiler toolchains (macOS to Ubuntu, even the reverse). The SwiftPM repo contains an example script which sets up a Intel-macOS => Intel-Ubuntu toolchain.

这允许您设置交叉编译器工具链(macOS到Ubuntu,甚至相反)。 SwiftPM repo包含一个示例脚本,用于设置Intel-macOS => Intel-Ubuntu工具链。

As Eric says, you can of course only use frameworks available on the target platform. But yes, you can now build a Linux Swift project on macOS. (as mentioned by joels in the comment you can also do this via Docker very easily)

正如Eric所说,您当然只能使用目标平台上提供的框架。但是,是的,您现在可以在macOS上构建Linux Swift项目。 (正如评论中的joels所提到的,你也可以通过Docker很容易地做到这一点)

Related: (Cross-)Compiling Swift for Raspberry PI

相关:(Cross-)为Raspberry PI编译Swift