使用Preferred-dynamic时无法执行Hello World项目 [英] Unable to execute a hello world project when using prefer-dynamic
问题描述
我在一个非常简单的项目Hello World中使用Rust的编译选项:
I'm playing with Rust's compilation options on an extremely simple project, hello world:
fn main() {
println!("Hello, world!");
}
我正在编译此行,prefer-dynamic是唯一值得注意的选择:
I'm compiling with this line, prefer-dynamic is the only notable option:
rustc main.rs -o ./build/rci -C prefer-dynamic
工作正常,直到我进行了一些更改,然后才进行了更改。现在,如果我尝试完全像上面那样编译代码,我将得到以下输出:
It was working fine until I made some changes and then it didn't. Now if I try to compile the code exactly as above I get this output:
./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上。
This is on Ubuntu 17.04 with Rust 1.15.1.
推荐答案
您应该查看术语;编程时,单词表示特定的事物。如您所见,调用Rust编译器(又名 rustc
)没有任何错误,就可以很好地编译代码。
You should review your terminology; words mean specific things when programming. You are able to compile your code just fine, as shown by the fact that invoking the Rust compiler (a.k.a. rustc
) doesn't have any errors.
您的问题是在执行程序时发生的。这些是非常不同的概念,它将使您很好地理解它们之间的区别。
Your problem occurs when executing the program. These are very different concepts and it will serve you well to understand the difference.
问题是您...正在使用动态链接,就像您ve要求。这不是不是 Rust问题,只是一般的编程问题。我确定有很多SO问题,例如加载共享库时发生Linux错误:无法打开共享对象文件:没有此类文件或目录或该错误消息还有500个其他问题,可以为您提供更多信息。
The "problem" is that you... are using dynamic linking, just like you've asked for. This is not a Rust issue, just a general programming issue. I'm sure there are numerous SO questions, such as Linux error while loading shared libraries: cannot open shared object file: No such file or directory or one of the 500 other questions with that error message that can provide you with more information.
您正在动态链接到Rust标准库,但是您的系统不知道该库,因为它没有安装在系统知道的位置。您很可能是通过rustup安装的,因此该库位于主目录中。例如,我的位于 /home/ubuntu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libstd-f4594d3e53dcb114.so
。
You are dynamically linking against the Rust standard library, but your system doesn't know about that library because it's not installed in a location your system knows about. Most likely, you've installed via rustup, so the library is in your home directory. For example, mine is in /home/ubuntu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libstd-f4594d3e53dcb114.so
.
您会找到许多可能的解决方案。最简单的示例是使用 LD_LIBRARY_PATH
变量:
You will find many possible solutions. The easiest to demonstrate is using the LD_LIBRARY_PATH
variable:
$ ./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!
您也可以查看将Rust应用程序与不在运行时链接程序搜索路径中的动态库链接
发现这一点,您可以使Rust 打印出它使用的链接器调用:
To help discover this, you can make 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
链接标准库。
You can see that "-L" "/Users/shep/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib"
was added to the linker path and then "-l" "std-834fbefe8dbe98b5"
links the standard library.
这篇关于使用Preferred-dynamic时无法执行Hello World项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!