# Nim binary size from 160 KB to 150 Bytes

DevFeed: [Nim binary size from 160 KB to 150 Bytes](<https://devfeed.tech/articles/nim-binary-size-from-160-kb-to-150-bytes-30828.md>)

Original publisher: [Read original article](<https://hookrace.net/blog/nim-binary-size/>)

Published: 2015-05-03T22:00:00Z

Content type: tutorial

Language: en

Sources: [Dennis Felsing](<https://devfeed.tech/sources/dennis-felsing.md>)

Topics: [Nim](<https://devfeed.tech/topics/nim.md>), [Programming](<https://devfeed.tech/topics/programming.md>), [Compiler](<https://devfeed.tech/topics/compiler.md>), [Linux](<https://devfeed.tech/topics/linux.md>)

Tags: [c](<https://devfeed.tech/tags/c.md>), [clang](<https://devfeed.tech/tags/clang.md>), [compilation](<https://devfeed.tech/tags/compilation.md>), [compiler](<https://devfeed.tech/tags/compiler.md>), [gcc](<https://devfeed.tech/tags/gcc.md>), [glibc](<https://devfeed.tech/tags/glibc.md>), [linux](<https://devfeed.tech/tags/linux.md>), [measurements](<https://devfeed.tech/tags/measurements.md>), [nim](<https://devfeed.tech/tags/nim.md>), [x86](<https://devfeed.tech/tags/x86.md>)

## AI overview

This tutorial explores how to reduce the size of a simple Nim binary on Linux. It demonstrates builds using GCC and Clang, including a 6 KB binary, a 952-byte binary without the C standard library, and a 150-byte binary using a custom linker script and ELF header.

## Source excerpt

The size of binaries in the Nim programming language seems to be a popular topic recently. Nim's slogan is expressive, efficient, elegant, so let's examine the efficient part in this post by exploring a few ways to reduce the size of a simple Nim Hello World binary on Linux. Along the way we will: Build a regular program into a 6 KB binary Disregard the C standard library and build a 952 byte binary Use a custom linker script and ELF header to build a 150 byte binary (1 byte smaller than in Rust) The full source code of this post can be found in its repository. All measurements are done on Linux x86-64 with GCC 5.1 and Clang 3.6.0. Using the C Standard Library echo "Hello!" By default Nim uses GCC as the backend C compiler on most platforms, and we dynamically link against glibc. We can try optimizing for speed and size, as well as strip unnecessary symbols after the compilation: Command (GCC backend) Binary Size nim c hello 160 KB nim -d:release c hello 61 KB nim -d:release --opt:size c hello 25 KB nim -d:release --opt:size c hello && strip -s hello 19 KB That's pretty nice and can be done with any Nim program to reduce binary size. Now let's try to get rid of glibc, at least temporarily (we will come back to a more permanent solution later). Instead of glibc we're now statically linking against musl libc: $ nim --gcc.exe:/usr/local/musl/bin/musl-gcc \ --gcc.linkerexe:/usr/local/musl/bin/musl-gcc \ -d:release --opt:size --passL:-static c hello $ strip -s hello 30 KB Update: The order of arguments to nim matters, --passL:-static has to be passed after setting the gcc exe so that it isn't overwritten. So that's a statically linked binary in 30 KB, which can be deployed without depending on any glibc version (or any other libraries)! What about using Clang instead of GCC by setting --cc:clang: Command (Clang backend) Binary Size nim --cc:clang c hello 168 KB nim --cc:clang -d:release c hello 33 KB nim --cc:clang -d:release --opt:size c hello 29 KB nim --cc:clang -d:re