简体   繁体   English

Rust无法在Linux中编译为可执行文件

[英]Rust not compiling to executable in Linux

Compiling rust on Linux with rustc or cargo build produces a shared library instead of an executable file. 在Linux上使用rustccargo build编译rust会生成一个共享库,而不是一个可执行文件。
My file manager (thunar) and file command show that file type as shared library. 我的文件管理器(thunar)和file命令将该文件类型显示为共享库。

And the compiled binary can only be executed via terminal by $ /path/to/file or $ cargo run . 而且编译的二进制文件只能通过$ /path/to/file$ cargo run通过终端$ cargo run
That file cannot be executed just by double clicking as other executables can be. 该文件不能像其他可执行文件一样通过双击执行。
Output from file command: file命令的输出:

$ file rust_bin

rust_bin: ELF 64-bit LSB shared object, x86_64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=cb8cd... , with debug_info, not stripped` rust_bin:用于GNU / Linux 3.2.0的ELF 64位LSB共享对象,x86_64,版本1(SYSV),动态链接,解释器/lib64/ld-linux-x86-64.so.2,BuildID[sha1]= cb8cd ...,带有debug_info,未剥离

  1. 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. interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0字符串interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0指示这是可执行文件,而不是库。 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. 尝试在一些您知道是可执行file上运行file在您知道一些其他文件是库的file上运行file ,然后自己看看。 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). 文件实际上可以同时充当库和可执行文件(最常见的示例是libc.so.6或在系统上调用的任何文件;请尝试运行它)。
  2. 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. 通常可以通过创建一个描述程序的.desktop文件来完成此操作。 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. 也可以通过为可执行文件创建一个.desktop文件来补救。 It is not specific to rust in any way. 它并非以任何方式专门针对锈蚀。

Bottom line, there's nothing wrong with rustc or cargo or the way you are running them. 最重要的是, rustccargo或它们的运行方式都没有错。

When you create your project initially you can simply use cargo new (or init) to get the right type 最初创建项目时,您可以简单地使用cargo new(或init)来获取正确的类型

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

when you use rustc you can use a command-line option 使用rustc时,可以使用命令行选项

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

Your finding about shared object is misleading your error hunt: https://askubuntu.com/questions/690631/executables-vs-shared-objects - it's not a problem, an executable can be a shared object. 关于共享库的发现误导了您的错误搜寻: https : //askubuntu.com/questions/690631/executables-vs-shared-objects-这不是问题,可执行文件可以是共享库。

I think in your case the problem is another. 我认为您的问题是另一个问题。 What does you binary do? 你二进制做什么? Does basically just print something via stdout and that's it? 基本上是否只是通过stdout打印内容,仅此而已? Maybe this is the reason why double clicking in the gui file browser does not show you anything, it runs a millisecond and is over before you know it. 也许这就是为什么在gui文件浏览器中双击不会显示任何内容的原因,它运行了毫秒,并且在您知道之前就结束了。

Have you tried waiting for input at the end of the main function? 您是否尝试过在主要功能结束时等待输入? Just so that the user can read the output and hit Return key. 只是为了使用户可以读取输出并按Return键。

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);
}

Not sure how thunar will deal with it but eventually he will open a terminal and show the result and close the terminal when enter is pressed. 不确定thunar将如何处理它,但最终他将打开一个终端并显示结果,并在按enter键时关闭终端。

cargo build

creates an executable file in target/debug/rust_bin Then just 在target / debug / rust_bin中创建一个可执行文件,然后

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

to execute, or just 执行,或只是

cargo run

PS: you need to create a Cargo.toml file with the appropriate data inside. PS:您需要创建一个Cargo.toml文件,其中包含适当的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 运行/编译可执行Linux与Solaris - Running/compiling executable Linux vs Solaris 在Linux上编译一个fortran程序并将可执行文件移至另一台Linux机器 - Compiling a fortran program on linux and moving the executable to another linux machine Haskell ghc编译/链接错误,不创建可执行文件。 (Linux)的 - Haskell ghc compiling/linking error, not creating executable. (linux) 在Windows上交叉编译Linux(链接器输出文件不会在linux上作为可执行文件运行并且具有未定义的符号) - Cross compiling for Linux on Windows (linker output file won't run as executable on linux and has undefined symbols) 错误:使用cargo从windows交叉编译一个rust项目到linux时“找不到链接器‘cc’” - Error: “linker 'cc' not found” when cross compiling a rust project from windows to linux using cargo 使用 boost.asio 在 linux 系统上编译 mingw32 可执行文件的问题 - Problem with compiling a mingw32 executable on linux system with boost.asio 将busybox编译为BFLT可执行文件 - Compiling busybox as BFLT executable 使用gcc编译fat可执行文件 - Compiling a fat executable with gcc 在 Linux 可执行文件中嵌入图标 - Embedding an icon in a Linux executable 在Linux上远程执行可执行文件? - Executing executable remotely on Linux?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM