Large Windows binaries for Rust/Cargo

When trying to deploy a Rust/Cargo project for Windows, I get binaries much larger then when building locally.

E.g. the primary .exe file (chrono-photo.exe) is 9.4MB, compared to 3.1MB for the local build.
I have no comparison for OSX and Linux binaries, as I didn’t set up these builds locally.

The issue arises for the .travis.yml in the commit linked below, and also for other repositories:

Is there anything I am missing, or any way to reduce binary size to what I get locally?

Many thanks in advance!

This is a relevant-looking tell (emphasis mine):

Finished test [unoptimized + debuginfo] target(s) in 2m 21s

Many thanks for the hint.

However, release builds (cargo build --release) go to folder target/release, where I take the binaries for deployment from. Builds for tests go to folder target/debug.

Also, changing the order to execute tests before the release build did not change the excutable’s size.

There is a much simpler repo with the same problem:
https://travis-ci.com/github/mlange-42/travis-test

Here, Travis builds for Windows are 3.5MB, while the local (release) build is 143kB! Local test builds are 146kB, which also shows that unoptimized can’t be the cause of the problem.

Thanks again!

In my test, both Windows and Linux builds are ~3M, so it’s not Windows-specific.

They don’t have import from non-system libs and almost all the space is taken up by the .text section. So my guess is they are statically linked.

According to rust - How to generate statically linked executables? - Stack Overflow, this is controlled by the .cargo/config file:

Since Rust 1.19, you can statically link the C runtime (CRT) to avoid this very common situation on Windows:

The program can’t start because VCRUNTIME140.dll is missing from your computer. Try reinstalling the program to fix this problem.

Add this to your .cargo/config file, using the appropriate target triple for your platform:

[target.x86_64-pc-windows-msvc]
rustflags = ["-C", "target-feature=+crt-static"]

An alternative to editing .cargo/config is to pass -C target-feature=+crt-static to rustc by hand.

See also: