Building static binaries with Go on Linux
The article explains how to build static binaries with Go on Linux, noting that while possible, it requires specific configurations and tools, especially when using C code via cgo.
Read original articleThe article discusses the process of building static binaries with Go on Linux, highlighting that while Go can produce statically-linked binaries, it does not do so by default in all cases. The author conducts experiments with simple Go programs to determine whether the resulting binaries are statically or dynamically linked. For instance, a basic "hello, world" program is confirmed to be statically linked, while programs that utilize the Go standard library for DNS and user lookups are dynamically linked unless specific build tags or environment variables are set to enforce static linking.
The article explains that when C code is included in a Go program via cgo, the resulting binary will typically be dynamically linked due to dependencies on the C runtime library (libc). To create a statically linked binary in such cases, the author suggests using an alternative libc implementation, such as musl, or employing the Zig toolchain, which simplifies the process of static linking.
The author notes that achieving static linking requires additional effort and mentions a proposal for a `-static` flag in the Go build process to streamline this. The article concludes by stating that while building static binaries in Go on Linux can be complex, it is feasible with the right tools and configurations. The code for the experiments is available on GitHub for further exploration.
Related
At the Mountains of Madness
The blog discusses challenges in building executables for NixOS and Linux, focusing on ELF interpretation, glibc versions, and dynamic linking. It explores container use, NixOS dependency management, and proposes patchelf for cross-compatibility.
Golang is evil on shitty networks (2022)
The impact of Golang's default setting disabling Nagle's algorithm on network performance is discussed. Concerns include slow uploads, increased latency, and network saturation, questioning the decision's efficiency and suggesting considerations for optimization.
How to build highly-debuggable C++ binaries
David Hashe's article offers guidance on configuring C++ toolchains for better debuggability, emphasizing practices like enabling sanitizers, using debug modes, and balancing performance with debuggability in large projects.
Driving Compilers
The article outlines the author's journey learning C and C++, focusing on the compilation process often overlooked in programming literature. It introduces a series to clarify executable creation in a Linux environment.
Better Firmware with LLVM/Clang
LLVM and Clang are gaining traction in embedded software development, particularly for ARM Cortex-M devices. The article advocates integrating Clang for better static analysis, error detection, and dual compiler usage.
- Using specific build tags can optimize SQLite for static binaries, as noted by a user.
- There are alternative methods to use SQLite without cgo, such as through WASM, which some users find more efficient.
- Concerns about performance when using static binaries with SQLite, particularly with different compilers like Zig.
- Potential for integrating Go with Cosmopolitan libc to create cross-platform static binaries.
- Discussion on including the Go runtime within the program itself, rather than embedding or downloading it.
This is explained in https://www.arp242.net/static-go.html
Performance so far has been better than the modernc transpile and it’s probably sufficient for a lot of use cases.
(Needless to say, we have better load testing tooling now)
I forget the details, but something about the libc allocator used by SQLite-with-Zig-libc being ... not good.
$ go build -ldflags '-linkmode external -s -w -extldflags "--static-pie"' -buildmode=pie -tags 'osusergo,netgo,static_build' main.go
For anyone curious to delve into what is this and why: https://www.leviathansecurity.com/blog/aslr-protection-for-s...The ability to build a single static binary that works on Linux, Mac and Windows using Go would be life changing for the internal tools I develop at work.
Related
At the Mountains of Madness
The blog discusses challenges in building executables for NixOS and Linux, focusing on ELF interpretation, glibc versions, and dynamic linking. It explores container use, NixOS dependency management, and proposes patchelf for cross-compatibility.
Golang is evil on shitty networks (2022)
The impact of Golang's default setting disabling Nagle's algorithm on network performance is discussed. Concerns include slow uploads, increased latency, and network saturation, questioning the decision's efficiency and suggesting considerations for optimization.
How to build highly-debuggable C++ binaries
David Hashe's article offers guidance on configuring C++ toolchains for better debuggability, emphasizing practices like enabling sanitizers, using debug modes, and balancing performance with debuggability in large projects.
Driving Compilers
The article outlines the author's journey learning C and C++, focusing on the compilation process often overlooked in programming literature. It introduces a series to clarify executable creation in a Linux environment.
Better Firmware with LLVM/Clang
LLVM and Clang are gaining traction in embedded software development, particularly for ARM Cortex-M devices. The article advocates integrating Clang for better static analysis, error detection, and dual compiler usage.