【问题标题】:Unable to execute a hello world project when using prefer-dynamic使用 prefer-dynamic 时无法执行 hello world 项目
【发布时间】:2017-10-16 04:54:58
【问题描述】:

我正在一个非常简单的项目中使用 Rust 的编译选项,hello world:

fn main() {
    println!("Hello, world!");
}

我正在使用这一行进行编译,prefer-dynamic 是唯一值得注意的选项:

rustc main.rs -o ./build/rci -C prefer-dynamic

在我进行一些更改之前它工作正常,然后它没有。现在,如果我尝试完全按照上面的方式编译代码,我会得到以下输出:

./build/rci: error while loading shared libraries: libstd-2ddb28df747fcb8c.so: cannot open shared object file: No such file or directory

ldd的输出是:

linux-vdso.so.1 =>  (0x00007ffd321a4000)
libstd-2ddb28df747fcb8c.so => not found
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f52eaef3000)
/lib64/ld-linux-x86-64.so.2 (0x000055f0f6251000)

这是在带有 Rust 1.15.1 的 Ubuntu 17.04 上。

【问题讨论】:

    标签: ubuntu rust dynamic-linking


    【解决方案1】:

    您应该查看您的术语;编程时,单词意味着特定的事物。您可以编译您的代码,正如调用 Rust 编译器(又名rustc)没有任何错误的事实所示。

    您的问题发生在执行程序时。这些是非常不同的概念,您可以很好地理解其中的区别。

    “问题”是您...正在使用动态链接,就像您要求的那样。这不是 Rust 问题,只是一般编程问题。我确信有许多 SO 问题,例如 Linux error while loading shared libraries: cannot open shared object file: No such file or directory500 other questions with that error message 之一,可以为您提供更多信息。

    您正在动态链接 Rust 标准库,但您的系统不知道该库,因为它没有安装在您的系统知道的位置。很可能,您是通过 rustup 安装的,因此该库位于您的主目录中。例如,我在/home/ubuntu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libstd-f4594d3e53dcb114.so

    您会发现许多个可能的解决方案。最容易演示的是使用LD_LIBRARY_PATH 变量:

    $ ./example
    ./example: error while loading shared libraries: libstd-f4594d3e53dcb114.so: cannot open shared object file: No such file or directory
    $ LD_LIBRARY_PATH=/home/ubuntu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/ ./example
    Hello, world!
    

    您也可以查看Linking Rust application with a dynamic library not in the runtime linker search path


    为了帮助发现这一点,您可以制作 Rust print out the linker invocation it uses

    $ rustc +nightly -Cprefer-dynamic -Z print-link-args hello.rs
    "cc" "-m64" "-L" "/Users/shep/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib" "hello.hello0.rcgu.o" "hello.hello1.rcgu.o" "hello.hello2.rcgu.o" "hello.hello3.rcgu.o" "hello.hello4.rcgu.o" "hello.hello5.rcgu.o" "-o" "hello" "hello.crate.allocator.rcgu.o" "-Wl,-dead_strip" "-nodefaultlibs" "-L" "/Users/shep/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib" "-L" "/Users/shep/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib" "-l" "std-834fbefe8dbe98b5" "/Users/shep/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libcompiler_builtins-b4312e2f1496a4e4.rlib" "-l" "System" "-l" "resolv" "-l" "pthread" "-l" "c" "-l" "m"
    

    可以看到"-L" "/Users/shep/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib"被添加到了链接器路径中,然后"-l" "std-834fbefe8dbe98b5"链接了标准库。

    【讨论】:

    • 啊,大概就是这样,以前的 rust/cargo 安装的遗留物。
    • @flukus 你是什么意思?将这些库安装到主目录是经过深思熟虑的位置,主目录很少(从不?)系统可执行加载器的搜索路径的一部分。什么是“剩余”?
    • 我的意思是文件夹本身和路径修改来自以前使用 rustup 安装。我正在尝试使用 ubuntu 包编译和运行。
    猜你喜欢
    • 2014-11-29
    • 2015-05-22
    • 2014-09-25
    • 2016-10-25
    • 1970-01-01
    • 1970-01-01
    • 2020-08-07
    • 1970-01-01
    • 2012-08-22
    相关资源
    最近更新 更多