Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate statically linked executables?

I am trying to create a static executable with Rust. I am not trying to statically link a particular library, I am trying to create a executable which does not use dynamic linking at all. I have the following (otherwise working) test:

$ cat hello.rs fn main()     {     print!("Hello, world!\n");     } $ rustc hello.rs -o hello $ file hello hello: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV),  dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, [etc] 

Note the dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2. Static executables have statically linked instead. (And in my case corrupted section header size, although I would be pleasantly astonished if I can convince Rust to replicate that.)

What options do I need to pass to rustc to get it to generate a actual static executable (for concreteness: one which even file agrees is statically linked).

like image 415
David X Avatar asked Aug 02 '15 09:08

David X


People also ask

How do I create a statically linked executable in Linux?

Create a statically linked application The -I option tells GCC to search for header files listed after it. In this case, you're specifying the current directory, represented by a single dot ( . ). Link mathDemo.o with libmymath. a to create the final executable.

What is a statically linked executable?

Statically-linked files are 'locked' to the executable at link time so they never change. A dynamically linked file referenced by an executable can change just by replacing the file on the disk. This allows updates to functionality without having to re-link the code; the loader re-links every time you run it.

Are rust binaries statically linked?

Rust has supported producing statically linked binaries since RFC #1721 which proposed the target-feature=+crt-static flag to statically link the platform C library into the final binary.

What does it mean to be statically linked?

Static linking means that the code for all routines called by your program becomes part of the executable file. Statically linked programs can be moved to run on systems without the XL Fortran runtime libraries.


2 Answers

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:

  • RFC

  • Pull Request

  • Cargo config documentation

like image 120
arkod Avatar answered Sep 29 '22 18:09

arkod


Rust statically links everything but glibc (and libgcc, iirc) by default.

If you want to get a 100% statically linked binary, you can use MUSL with 1.1. https://github.com/rust-lang/rust/pull/24777 is the initial support, we hope to make it much easier to use in the future.

like image 39
Steve Klabnik Avatar answered Sep 29 '22 18:09

Steve Klabnik