# Large Windows binaries for Rust/Cargo

**URL:** <https://travis-ci.community/t/large-windows-binaries-for-rust-cargo/8356>\
**Category:** Windows\
**Tags:** deployment\
**Created:** [April 26, 2020, 3:49pm UTC](https://travis-ci.community/t/large-windows-binaries-for-rust-cargo/8356 "2020-04-26T15:49:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mlange-42](https://avatars.discourse-cdn.com/v4/letter/m/db5fbb/32.png) [@mlange-42](https://travis-ci.community/u/mlange-42)\
**Post date:** [April 26, 2020, 3:49pm UTC](https://travis-ci.community/t/large-windows-binaries-for-rust-cargo/8356/1 "2020-04-26T15:49:24Z")

</div>

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:

> **[mlange-42/chrono-photo](https://github.com/mlange-42/chrono-photo/tree/8ea8f74943d4cd4e7c9b27b740d5ddf38e09b812)**
>
> Chronophotography command line tool and library in Rust. - mlange-42/chrono-photo

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

Many thanks in advance!

---

<div class="post-metadata">

**Author:** ![native-api](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/native-api/32/430_2.png) [@native-api](https://travis-ci.community/u/native-api)\
**Post date:** [April 27, 2020, 12:18pm UTC](https://travis-ci.community/t/large-windows-binaries-for-rust-cargo/8356/2 "2020-04-27T12:18:22Z")

</div>

[This](https://travis-ci.com/github/mlange-42/chrono-photo/jobs/323516652#L637) is a relevant-looking tell (emphasis mine):

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

---

<div class="post-metadata">

**Author:** ![mlange-42](https://avatars.discourse-cdn.com/v4/letter/m/db5fbb/32.png) [@mlange-42](https://travis-ci.community/u/mlange-42)\
**Post date:** [April 27, 2020, 12:48pm UTC](https://travis-ci.community/t/large-windows-binaries-for-rust-cargo/8356/3 "2020-04-27T12:48:24Z")

</div>

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](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!

---

<div class="post-metadata">

**Author:** ![native-api](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/native-api/32/430_2.png) [@native-api](https://travis-ci.community/u/native-api)\
**Post date:** [December 13, 2020, 12:58pm UTC](https://travis-ci.community/t/large-windows-binaries-for-rust-cargo/8356/4 "2020-12-13T12:58:05Z")

</div>

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](https://stackoverflow.com/questions/31770604/how-to-generate-statically-linked-executables), this is controlled by the `.cargo/config` file:

> [Since Rust 1.19](https://github.com/rust-lang/rust/blob/master/RELEASES.md#version-1190-2017-07-20), 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:
> 
> ```auto
> [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:
> 
> - [RFC](https://github.com/rust-lang/rfcs/blob/master/text/1721-crt-static.md)
> - [Pull Request](https://github.com/rust-lang/rust/pull/37545)
> - [Cargo config documentation](https://doc.rust-lang.org/cargo/reference/config.html)
