如何构建没有共享库的 Rust 应用程序?
How to build a Rust app free of shared libraries?
如何在完全不必加载共享库的情况下编译 Rust 应用程序?
我尝试了什么:
ex.rs
fn main() {
println!("Try to compile me statically!");
}
根据 https://rust-lang.github.io/rfcs/1721-crt-static.html 我执行了以下操作:
$ rustc -C target-feature=+crt-static ./ex.rs
$ ldd ./ex
linux-vdso.so.1 (0x00007ffc4e3ef000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fc30b8c4000)
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007fc30b6bc000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fc30b49d000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fc30b285000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fc30ae94000)
/lib64/ld-linux-x86-64.so.2 (0x00007fc30bcfb000)
为什么 libc
不是静态编译的?
以及如何摆脱所有其他共享库的链接?
MUSL support for fully static binaries:
默认情况下,Rust 将静态 link 所有 Rust 代码。但是,如果你使用标准库,它会动态地link到系统的libc实现。
如果您想要 100% 静态二进制文件,可以在 Linux:
上使用 MUSL libc
rustup target add x86_64-unknown-linux-musl
RUSTFLAGS='-C link-arg=-s' cargo build --release --target x86_64-unknown-linux-musl
main.rs
文件:
use actix_web::{web, App, HttpServer, Responder};
async fn index(info: web::Path<(String, u32)>) -> impl Responder {
format!("Hello {}! id:{}", info.0, info.1)
}
#[actix_rt::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| App::new().service(web::resource("/{name}/{id}/index.html").to(index)))
.bind("127.0.0.1:8080")?
.run()
.await
}
Cargo.toml
文件:
[profile.release]
opt-level = 's' # Optimize for size.
lto = true # Link Time Optimization (LTO)
# codegen-units = 1 # Set this to 1 to allow for maximum size reduction optimizations:
# panic = 'abort' # removes the need for this extra unwinding code.
[dependencies]
actix-web = "2.0.0" # Actix web is a simple, pragmatic and extremely fast web framework for Rust.
actix-rt = "1.0.0" # Actix runtime
如何在完全不必加载共享库的情况下编译 Rust 应用程序?
我尝试了什么:
ex.rs
fn main() {
println!("Try to compile me statically!");
}
根据 https://rust-lang.github.io/rfcs/1721-crt-static.html 我执行了以下操作:
$ rustc -C target-feature=+crt-static ./ex.rs
$ ldd ./ex
linux-vdso.so.1 (0x00007ffc4e3ef000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fc30b8c4000)
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007fc30b6bc000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fc30b49d000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fc30b285000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fc30ae94000)
/lib64/ld-linux-x86-64.so.2 (0x00007fc30bcfb000)
为什么 libc
不是静态编译的?
以及如何摆脱所有其他共享库的链接?
MUSL support for fully static binaries:
默认情况下,Rust 将静态 link 所有 Rust 代码。但是,如果你使用标准库,它会动态地link到系统的libc实现。
如果您想要 100% 静态二进制文件,可以在 Linux:
上使用 MUSL libcrustup target add x86_64-unknown-linux-musl
RUSTFLAGS='-C link-arg=-s' cargo build --release --target x86_64-unknown-linux-musl
main.rs
文件:
use actix_web::{web, App, HttpServer, Responder};
async fn index(info: web::Path<(String, u32)>) -> impl Responder {
format!("Hello {}! id:{}", info.0, info.1)
}
#[actix_rt::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| App::new().service(web::resource("/{name}/{id}/index.html").to(index)))
.bind("127.0.0.1:8080")?
.run()
.await
}
Cargo.toml
文件:
[profile.release]
opt-level = 's' # Optimize for size.
lto = true # Link Time Optimization (LTO)
# codegen-units = 1 # Set this to 1 to allow for maximum size reduction optimizations:
# panic = 'abort' # removes the need for this extra unwinding code.
[dependencies]
actix-web = "2.0.0" # Actix web is a simple, pragmatic and extremely fast web framework for Rust.
actix-rt = "1.0.0" # Actix runtime