如果我使用带有一些crate依赖项的Cargo构建一个Rust应用程序,那么我的应用程序未使用的那些依赖项中的任何代码是否会从最终的可执行文件中删除?
@H_404_1@
@H_404_1@
它看起来像.我并排测试了lib和bin箱子:
// hellobin/src/main.rs extern crate hellolib; fn main() { hellolib::func1(); }
对于lib:
// hellolib/src/main.rs pub fn func1() { println!("Hello,world!"); } pub fn func2() { println!("Hello,other world!"); }
构建我的二进制文件,然后用nm检查符号:
$nm target/debug/helloworld | grep hello 0000000100001360 t __ZN10helloworld4main17h749f61fb726f0a10E 00000001000014b0 T __ZN8hellolib5func117hec0b5301559d46f6E
您可以使用货物rustc – -C link-dead-code编译,但您会看到两个符号都存在,包括未使用的符号:
$nm target/debug/helloworld | grep hello 0000000100001270 t __ZN10helloworld4main17h3104b73b00fdd798E 00000001000013d0 T __ZN8hellolib5func117hec0b5301559d46f6E 0000000100001420 T __ZN8hellolib5func217hc9d0886874057b84E
我相信(但我不确定)它是链接器删除死代码,所以它可能仍然被编译,然后在链接期间被删除.
@H_404_1@