{"id":20395288,"url":"https://github.com/kodraus/rust-cross-compile","last_synced_at":"2026-03-11T19:31:47.876Z","repository":{"id":41990760,"uuid":"181595319","full_name":"KodrAus/rust-cross-compile","owner":"KodrAus","description":"An example of cross compiling Rust apps to Linux on Windows, with only rustup","archived":false,"fork":false,"pushed_at":"2022-04-20T04:52:27.000Z","size":14,"stargazers_count":91,"open_issues_count":0,"forks_count":4,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-12T12:31:12.763Z","etag":null,"topics":["cross-compilation","example","rust"],"latest_commit_sha":null,"homepage":"","language":"PowerShell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/KodrAus.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-04-16T01:57:09.000Z","updated_at":"2025-02-22T09:54:47.000Z","dependencies_parsed_at":"2022-08-12T01:40:23.056Z","dependency_job_id":null,"html_url":"https://github.com/KodrAus/rust-cross-compile","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/KodrAus/rust-cross-compile","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KodrAus%2Frust-cross-compile","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KodrAus%2Frust-cross-compile/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KodrAus%2Frust-cross-compile/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KodrAus%2Frust-cross-compile/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/KodrAus","download_url":"https://codeload.github.com/KodrAus/rust-cross-compile/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KodrAus%2Frust-cross-compile/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30395597,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-11T18:46:22.935Z","status":"ssl_error","status_checked_at":"2026-03-11T18:46:17.045Z","response_time":84,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cross-compilation","example","rust"],"created_at":"2024-11-15T03:55:56.039Z","updated_at":"2026-03-11T19:31:47.847Z","avatar_url":"https://github.com/KodrAus.png","language":"PowerShell","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Statically linking and cross-compiling Rust apps on Windows [![Build status](https://ci.appveyor.com/api/projects/status/ku7y70c7jwp8x7i8?svg=true)](https://ci.appveyor.com/project/KodrAus/cross-compile-example)\n\nThis example demonstrates how you can use just the tools that are readily accessible through [`rustup`](https://rustup.rs) to statically link and cross-compile Rust apps on Windows.\n\n-----\n\nRust is compiled ahead-of-time to machine code that runs directly on an end-user's machine. That means you have to know upfront what platforms you're going to target and have the right build tools and libraries available for each of them.\n\nEven when you do have compiled binaries, you can run into problems distributing them if you find yourself depending on the availability of C runtime libraries on the end-user's machine.\n\nWhen your build environment is Windows and you also need to target Linux, it turns out we can solve both our cross-compilation and distribution problems at once by statically linking MSVCRT for Windows and by cross-compiling our Linux builds to target musl instead of glibc.\n\nLet's start with a fresh _Hello World_ Rust app:\n\n```shell\ncargo new --bin cross-compile-sample\ncd cross-compile-sample\n```\n\n## Statically linking MSVCRT\n\nIf we build our library for the MSVC target (which is the default for Windows) now, it'll dynamically link to MSVCRT:\n\n```shell\ncargo build --target x86_64-pc-windows-msvc\n```\n\n```shell\nls target/x86_64-pc-windows-msvc/debug\n```\n\n```shell\nLength Name\n------ ----\n144384 cross-compile-sample.exe\n```\n\nIn order to tell the Rust compiler to statically link MSVCRT, we need to add some configuration to a `.cargo/config.toml` file:\n\n```toml\n[target.x86_64-pc-windows-msvc]\nrustflags = [\"-C\", \"target-feature=+crt-static\"]\n```\n\nThe [`crt-static` target feature](https://github.com/rust-lang/rfcs/blob/master/text/1721-crt-static.md) is a code generation option that's only available for targets that are suitable for both static and dynamic linkage. MSVC is one of those targets. When `crt-static` is specified, the C runtime libraries will be linked statically instead of dynamically.\n\nBuilding our app again results in a different binary:\n\n```shell\ncargo build --target x86_64-pc-windows-msvc\n```\n\n```shell\nls target/x86_64-pc-windows-msvc/debug\n```\n\n```shell\nLength Name\n------ ----\n241152 cross-compile-sample.exe\n```\n\nIt's bigger than before because we have the relevant pieces of MSVCRT included.\n\n## Cross-compiling to Linux\n\nFor Windows, we statically link MSVCRT because it's more convenient for end-users. The `crt-static` feature solves our distribution problem. For Linux, we're going to statically link the [musl](https://www.musl-libc.org/intro.html) libc because it's more convenient for us at build time (and is more portable). musl is a complete, self-contained Linux libc with no system dependencies. That means we don't have to provide Linux system libraries to dynamically link to in our Windows build environment. musl solves our cross-compilation problem.\n\nWe can install musl as a target for Rust using `rustup`:\n\n```shell\nrustup target add x86_64-unknown-linux-musl\n```\n\nAttempting to build right now probably won't work though:\n\n```shell\ncargo build --target x86_64-unknown-linux-musl\n```\n\n```shell\nerror: linker `cc` not found\nerror: could not compile `cross-compile-sample`\n```\n\nWe've got the runtime we need, but not the build tools to link up our final Linux binary. Well, actually we do have the build tools we need. We're just not using them yet. Rust [embeds LLVM's linker](https://github.com/rust-lang/rust/issues/39915), `lld`, which we can use instead of the unavailable `cc` to link our Linux binary on Windows.\n\nAdding `rust-lld` as the linker for our musl target in our `.cargo/config.toml` file will switch from `cc` to Rust's `lld`:\n\n```toml\n[target.x86_64-unknown-linux-musl]\nlinker = \"rust-lld\"\n```\n\nWe should now be able to cross-compile a Linux binary from our Windows host:\n\n```shell\ncargo build --target x86_64-unknown-linux-musl\n```\n\n```shell\nls target/x86_64-unknown-linux-musl/debug\n```\n\n```shell\n Length Name\n ------ ----\n3041624 cross-compile-sample\n```\n\n### Limitations\n\n- You can't directly or transitively depend on any libraries that need to compile C code. That includes the `failure` crate with its `backtrace` dependency. You can build some reasonably complex projects though, including [this UDP server](https://github.com/datalust/sqelf) that depends on `tokio`.\n- musl binaries linked using LLD don't seem to be able to recover from panics (there's been [problems with LLVM's `libunwind` port that's used in Rust's musl target in the past](https://github.com/rust-lang/rust/issues/35599)).\n\n### Other approaches for cross-compilation\n\nThis example uses a combination of musl and LLD to cross-compile a Linux binary from Windows without needing any tools that aren't readily available through `rustup`. Other approaches include:\n\n- Use [LCOW](https://docs.microsoft.com/en-us/virtualization/windowscontainers/deploy-containers/linux-containers) to build your Linux binaries natively.\n- Use a separate Linux build agent.\n\nThey're probably more robust, but depend on the availability of those features, or additional build complexity to coordinate the bundling of artifacts produced in separate environments. Azure Pipelines makes this coordination fairly straightforward though. I've got an example of building a native library (in this case LLVM itself) in Azure Pipelines on several platforms and packaging their artifacts together at the end [here](https://github.com/KodrAus/libllvm).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkodraus%2Frust-cross-compile","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkodraus%2Frust-cross-compile","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkodraus%2Frust-cross-compile/lists"}