如何使用 rustc 生成可执行文件? [英] How to produce executable with rustc?
问题描述
如果我编译这个简单的程序 fn main() { println!("Hello");}
和 rustc test.rs -o test
然后我可以用 ./test
运行它,但是在文件管理器中双击它会出现这个错误:<代码>无法显示测试".没有为共享库"文件安装应用程序. 运行 file test
似乎同意:test: ELF 64-bit LSB shared object...
.
If I compile this simple program fn main() { println!("Hello"); }
with rustc test.rs -o test
then I can run it with ./test
, but double clicking it in the file manager gives this error: Could not display "test". There is no application installed for "shared library" file.
Running file test
seems to agree: test: ELF 64-bit LSB shared object...
.
我如何获得 rustc 以及使用它的工具(例如货物)来生成可执行文件而不是共享对象?
How can I get rustc, and also tools that use it such as cargo, to produce executables rather than shared objects?
我使用的是 64 位 Linux (Ubuntu 14.10).
I am using 64-bit Linux (Ubuntu 14.10).
I have posted on the Rust forum about this.
EDIT@:所以事实证明这是 file
可执行文件的问题.
EDIT@: So it turns out this is an issue with the file
executable.
推荐答案
你所描述的与我观察到的并不完全一致:
What you have described is not entirely consistent with what I observe:
$ rustc - -o test <<< 'fn main() { println!("Hello"); }'
$ ./test
Hello
$ file test
test: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=99e97fcdc41eb2d36c950b8b6794816b05cf854c, not stripped
除非你告诉 rustc 生成一个库,例如#![crate_type = "lib"]
或 --crate-type lib
,它将产生一个可执行文件.
Unless you tell rustc to produce a library with e.g. #![crate_type = "lib"]
or --crate-type lib
, it will produce an executable.
听起来您的文件管理器可能太聪明了.它应该只是信任可执行位并执行它.
It sounds like your file manager may be being too smart for its own good. It should just be trusting the executable bit and executing it.
这篇关于如何使用 rustc 生成可执行文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!