# Cross-compiling for Linux

**URL:** <https://forums.swift.org/t/cross-compiling-for-linux/83523>\
**Category:** Using Swift\
**Tags:** cross-compilation\
**Created:** [December 2, 2025, 7:14pm UTC](https://forums.swift.org/t/cross-compiling-for-linux/83523 "2025-12-02T19:14:14Z")\
**Posts on this page:** 1\
**Showing post:** 12

<div class="post-metadata">

**Author:** ![Finagolfin](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/finagolfin/32/34388_2.png) [@Finagolfin](https://forums.swift.org/u/Finagolfin)\
**Post date:** [December 3, 2025, 10:15pm UTC](https://forums.swift.org/t/cross-compiling-for-linux/83523/12 "2025-12-03T22:15:21Z")

</div>

> [@awwright](#):
>
> Swift 6.2.1 won't [compile my code in release mode](https://github.com/swiftlang/swift/issues/84692), and I'm not sure when this fix is going to be merged.

That issue appears to have been fixed months ago, but you can only check that with the trunk snapshots that have the fix. Unfortunately, there haven't been any trunk Static Linux SDK snapshots shipped since then for you to use with that trunk compiler, so you could only verify it by compiling for macOS or directly on linux, ie without cross-compiling using the Static Linux SDK.

> [@awwright](#):
>
> On a Linux host, I've been able to get `swift build` to work and `swift build --swift-sdk x86_64-swift-linux-musl` but sometimes I can't switch between the two. If I try running both, I get an error until I delete the whole `.build` directory and try again. Swift is definitely not tracking all of the build dependencies somewhere. But sometimes it works and I can compile both!

What you may be seeing if you downloaded snapshot SDKs too is that the version of the compiler and the SDK must exactly match, and the tools currently don't track that for you. Otherwise, if you are seeing random flakes, try running `swift package clean` and starting over: report an issue if you see an error fairly consistently.

> [@awwright](#):
>
> How do I build for i386, armv6 (e.g. Raspberry Pi), or arm64? How do I know what architectures are supported targets? How do I build for a particular version of glibc, if I want to target a particular Debian?

You build for the different architectures by supplying different target triples, that is clear in the doc. The tools do a bad job of telling you what architectures are supported by each SDK: I have an open pull to fix that. Swift SDKs do not currently support fine-grained versioning of Glibc and Debian and so on, at least if they use the same target triple and you want some way to specify those other versions to the compiler.

> [@awwright](#):
>
> And if I call `swift build` by itself, it builds for the host platform as I would expect, and puts it into a directory called `x86_64-unknown-linux-gnu`. But if I try to run `swift build --swift-sdk x86_64-unknown-linux-gnu` that errors (even if I `rm -rf .build` first).

The tool currently assumes that `--swift-sdk` is only invoked when cross-compiling, not when natively compiling.

> [@awwright](#):
>
> For releasing builds, how do I ensure I get an x86\_64 glibc build, regardless of host architecture?

Based on that above assumption, there isn't a single command you could use: you'd have to factor in the host arch and pass in different flags based on that.

> [@awwright](#):
>
> This isn't even valid Swift. The `error` directive will cause a parse error and fail the build.

Good point, if you replace that error with `#error("Unknown platform")`, it will work.

> [@awwright](#):
>
> Is this even a good idiom? Literally all I need is the `socket` function. It would be nice if all of these interfaces implemented a common protocol, and then I select a concrete implementation at link time.

It isn't, but it was the easiest to implement. 😉 I believe most cross-platform languages that expose the platform C library punt on this problem and do not provide a cross-platform C API or wrapper, because it's a lot of work to do otherwise. There has [been some discussion about providing something like what you want](https://forums.swift.org/t/using-the-c-library-from-swift-thoughts/77648), but it hasn't led to anything you can use.

> [@awwright](#):
>
> I'm still unclear what is meant by "toolchain" in this context?

Swift has its own terminology, that [you can read in the SDK spec](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0387-cross-compilation-destinations.md), if you're interested.

Some of this stuff you list is clear in the tutorial, some of it is bugs and errors, some of it is specific to Swift's insular world. Swift is still going cross-platform, and you'll have to pardon our dust while we make these SDKs more robust.

Keep reporting your problems in this forum and on GitHub: they will help us improve these SDKs and tools. And since most of it is open source, you can always pitch in and help fix them, of course. 😃

---

_[View the full topic](https://forums.swift.org/t/cross-compiling-for-linux/83523)._
