从LLVM位代码生成Rust可执行文件

列昂纳多·马克斯(Leonardo Marques)

如何生成用Rust编写的应用程序的可执行文件,该应用程序已编译为LLVM-IR位码?

如果我尝试用rustc编译.bc文件,它会告诉我,stream did not contain valid UTF-8而且我似乎无法弄清楚rustc中是否有为此特定的选项。

基本上我想实现这一点:program.rs-> program.bc-> programprogram最终的可执行文件在哪里我应该采取什么步骤来实现这一目标?

谢泼玛特

从此源代码开始:

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

您可以创建LLVM中间表示(IR)或位(BC):

# IR in hello.ll
rustc hello.rs --emit=llvm-ir
# BC in hello.bc
rustc hello.rs --emit=llvm-bc

然后,LLVM可以进一步处理这些文件,以产生程序集目标文件

# Assembly in hello.s
llc hello.bc
# Object in hello.o
llc hello.bc --filetype=obj

然后,您需要链接文件以生成可执行文件。这需要链接到Rust标准库。该路径取决于平台和版本:

cc -L/path/to/stage2/lib/rustlib/x86_64-apple-darwin/lib/ -lstd-2f39a9bd -o hello2 hello.o

然后,您可以运行该程序:

DYLD_LIBRARY_PATH=/path/to/stage2/lib/rustlib/x86_64-apple-darwin/lib/ ./hello2

该答案具有OS X特定的解决方案,但一般概念可扩展到Linux和Windows。Linux的实现会略有不同,而Windows的实现可能会有所不同。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章