Rust在Linux中不能编译为可执行文件

3 人关注

在Linux上用 rustc cargo build 编译rust会产生一个共享库,而不是一个可执行文件。
我的文件管理器(thunar)和 file 命令显示该文件类型为共享库。

而编译后的二进制文件只能通过终端的 $ /path/to/file $ cargo run 来执行。
该文件不能像其他可执行文件那样通过双击就能执行。
来自 file 命令的输出。

$ file rust_bin

rust_bin。ELF 64位LSB共享对象,x86_64,版本1(SYSV),动态连接,解释器/lib64/ld-linux-x86-64.so.2,用于GNU/Linux 3.2.0,BuildID[sha1]=cb8cd。,有debug_info,没有被剥离`。

1 个评论
请显示你正在运行的确切命令和它们产生的确切信息。
linux
rust
rust-cargo
AVX-42
AVX-42
发布于 2019-01-29
3 个回答
n. m.
n. m.
发布于 2019-01-29
已采纳
0 人赞同
  • Your compiler produces an executable file. There is no big difference between a shared library and a dynamically linked executable file. They follow the same basic format. The string interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0 indicates that this is an executable and not a library. Libraries don't normally have an interpreter set. Try running file on some files you know are executables, and some other files you know are libraries, and see for yourself. An interpreter is usually a small system program that loads and executes a shared object. A file can actually serve as both a library and an executable at the same time (the most common example is your libc.so.6 or whatever it is called on your system; try running it).
  • If you can run this executable from your shell but not from your file manager, the problem is with the file manager, not with the executable. You may have to specifically instruct the file manager that your program should run in a terminal. This usually can be done by creating a .desktop file that describes your program. In addition, desktop tools may mis-recognise modern executables as shared libraries. This is a common problem. It too can be remedied by creating a .desktop file for your executable. It is not specific to rust in any way.
  • 一句话, rustc cargo 或你运行它们的方式没有问题。

    它是可执行的,可以在终端 ./file 中执行,但不能从文件管理器(thunar xfce4)中执行,似乎文件管理器不能识别共享库,不能执行它们,因为其他可执行文件可以从文件管理器中执行,并从 file 中产生相同的输出。 我在launchpad.net上发现了一些相关的东西 共享库没有被文件管理程序执行
    @Amiy "似乎文件管理器不能识别共享库"。不,这不是这里发生的事情。该文件是一个可执行文件,而不是一个共享库。该错误报告谈到了GUI中的一个错误/错误配置。它认为该文件是一个共享库,而它不是。这与Rustc没有关系。
    @Amiy 显然,这是一个常见的错误。在我的机器上,我不能运行 任何 从文件管理器中获取可执行文件。我从来不使用文件管理器,所以我不会自己发现它。
    替换代码0】产生一个可执行文件。而 gcc -no-pie src.c 为gcc。我一般使用vim或nano,但我今天试着从文件管理器中执行它,但它没有。我在想,如果我把二进制文件给别人,他们不打开终端就无法执行。无论我是否使用 -no-pie 选项,用GCC编译的程序也会发生这种情况。所以问题出在GUI文件管理器上。
    @Amiy 如果你把你的程序给别人,你希望他们使用GUI,无论如何你也应该给他们一个.desktop文件和一个图标。
    Marcel Blanck
    Marcel Blanck
    发布于 2019-01-29
    0 人赞同

    当你最初创建你的项目时,你可以简单地使用good new(或init)来获得正确的类型

    cargo new my_project_name
    # OR create a lib project
    cargo new --lib my_library_name
    

    当你使用rustc时,你可以使用一个命令行选项

    rustc lib.rs
    # lib.rs has to contain a main function
    # OR to build a lib
    rustc --crate-type=lib lib.rs
    

    你对共享对象的发现误导了你的错误查询。https://askubuntu.com/questions/690631/executables-vs-shared-objects- 这不是一个问题,可执行文件可以是一个共享对象。

    我认为在你的情况下,问题是另一个。你的二进制程序是做什么的?基本上只是通过stdout打印东西,仅此而已?也许这就是为什么在gui文件浏览器中双击不显示任何东西的原因,它只运行一毫秒,在你知道之前就结束了。

    你有没有试过在主函数的最后等待输入?只是为了让用户能读到输出并按下返回键。

    use std::io;
    fn main() {
        // do and print stuff
        // Wait for return key
        let mut input = String::new();
        match io::stdin().read_line(&mut input);
    

    不知道thunar会如何处理,但最终他会打开一个终端,显示结果,并在按下回车键后关闭终端。

    Daedalus
    Daedalus
    发布于 2019-01-29
    0 人赞同
    cargo build
    

    在target/debug/rust_bin创建一个可执行文件。

    ./target/debug/hello_cargo # or .\target\debug\hello_cargo.exe on Windows
    

    to execute, or just