欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 焦点 > Linux内核中编译RUST

Linux内核中编译RUST

2025/9/26 14:13:32 来源:https://blog.csdn.net/qq_37057338/article/details/148743016  浏览:    关键词:Linux内核中编译RUST

首先,需要搭建rust编译开发环境

1. 安装rustup

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

2. 配置环境变量

source /root/.cargo/env

3. 安装bindgen

bindgen 是一个自动为 Rust 生成 FFI(Foreign Function Interface)绑定的工具,它能够处理 C 及部分 C++ 库。通过分析给定的头文件,bindgen 创建 Rust 代码,允许你在 Rust 项目中无缝地调用这些外部库的函数并使用其数据类型。例如,当提供一个 C 语言的头文件时,bindgen 能生成对应的 Rust 结构体和函数声明,使得 Rust 开发者可以轻松集成跨语言的库。执行:

cargo install bindgen

报错:

  Updating crates.io index
  Downloaded bindgen v0.72.0
  Downloaded 1 crate (240.3KiB) in 0.80s
error: there is nothing to install in `bindgen v0.72.0`, because it has no binaries
`cargo install` is only for installing programs, and can't be used with libraries.
To use a library crate, add it as a dependency to a Cargo project with `cargo add`.

解决方法:使用cargo install bindgen-cli

4. 下载Linux源码

git clone https://github.com/Rust-for-Linux/linux -b rust-dev

如果速度较慢,可以使用国内的镜像源

git clone https://gitcode.com/gh_mirrors/linu/linux.git -b rust-dev

5. 检查房当前Linux环境是否支持rust

源码的根目录下执行:

make LLVM=1 rustavailable

报错:

*** libclang (used by the Rust bindings generator 'bindgen') is too old.
*** Your version: 10.0.0
*** Minimum version: 13.0.1

*** Please see Documentation/rust/quick-start.rst for details
*** on how to set up the Rust support.

make[1]: *** [/home/wenxu/share/source_code/linux/Makefile:1736: rustavailable] Error 1
make: *** [Makefile:224: __sub-make] Error 2

原因是libclang版本过低,需要安装高版本的clang,执行

 apt-get install wget curl gnupg
curl -sSL https://apt.llvm.org/llvm.sh | bash

apt install -y clang-18 llvm-18 lld-18 libclang-18-dev llvm-18-dev clangd-18

好像执行了apt-get install wget curl gnupg
curl -sSL https://apt.llvm.org/llvm.sh | bash
后,就安装了最新的clang-19了,也就是可以不用再安装clang-18

安装高版本的clang后,执行:

update-alternatives --install /usr/bin/clang clang /usr/bin/clang-19 190 \
>   --slave /usr/bin/clang++ clang++ /usr/bin/clang++-19 \
>   --slave /usr/bin/clang-cpp clang-cpp /usr/bin/clang-cpp-19

update-alternatives --install /usr/bin/ld.lld ld.lld /usr/bin/ld.lld-19 190

这样clang默认就会选择高版本的clang-19

以上步骤都结束后,再执行make LLVM=1 rustavailable,显示:

Rust is available!

表示支持rust了。

6. 执行编译内核

make ARCH=arm64 LLVM=1 O=build defconfig

make ARCH=arm64 LLVM=1 O=build menuconfig

进入到可视化界面,选择General setup -------> Rust Support,点击空格选择,然后save保存。

如果想将rust sample的代码编译进内核,则在开启Rust Support后,再次进入config的可视化界面,找到Kernel hacking -------> Sample kernel code -------> Rust samples,空格选择对应的模块即可。

执行编译:

cd build && make ARCH=arm64 LLVM=1 -j8

生成build/arch/arm64/boot/Image,如果使用qemu模拟运行的时候,在运行阶段显示以下内容:

[    1.368729] rust_minimal: Rust minimal sample (init)
[    1.368986] rust_minimal: Am I built-in? true
[    1.369582] rust_print: Rust printing macros sample (init)
[    1.369721] rust_print: Emergency message (level 0) without args
[    1.369856] rust_print: Alert message (level 1) without args
[    1.369981] rust_print: Critical message (level 2) without args
[    1.370111] rust_print: Error message (level 3) without args
[    1.370237] rust_print: Warning message (level 4) without args
[    1.370362] rust_print: Notice message (level 5) without args
[    1.370486] rust_print: Info message (level 6) without args
[    1.370609] rust_print: A line that is continued without args
[    1.371208] rust_print: Emergency message (level 0) with args
[    1.371341] rust_print: Alert message (level 1) with args
[    1.371464] rust_print: Critical message (level 2) with args
[    1.371594] rust_print: Error message (level 3) with args
[    1.371717] rust_print: Warning message (level 4) with args
[    1.371841] rust_print: Notice message (level 5) with args
[    1.371967] rust_print: Info message (level 6) with args
[    1.372131] rust_print: A line that is continued with args
[    1.372349] rust_print: 1
[    1.372526] rust_print: "hello, world"
[    1.372822] rust_print: [../samples/rust/rust_print.rs:35:5] c = "hello, world"
[    1.373200] rust_print: "hello, world"
表示rust正常在内核中运行!

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词