斯坦福的新操作系统课程CS140e,使用Rust 2018语法全线更新

543 阅读2分钟

前言

本系列教程使用Rust 2018语法全线更新,编译器版本号为rustc 1.32.0-nightly (4a45578bc 2018-12-07)

Phase 4: Rusting Away

在配置完环境之后,我们将工作目录切换至phase4,不编写代码,我们直接make看看会出现什么错误。
我这里输出如下

+ Building target/aarch64-none-elf/release/libblinky.a [xargo --release]
warning: `panic` setting is ignored for `test` profile
   Compiling blinky v0.1.0 (/home/standbyme/Documents/CS140e/0-blinky/phase4)
error[E0259]: the name `compiler_builtins` is defined multiple times
 --> src/lib.rs:5:1
  |
5 | extern crate compiler_builtins;
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `compiler_builtins` reimported here
  |
  = note: `compiler_builtins` must be defined only once in the type namespace of this module
help: you can use `as` to change the binding name of the import
  |
5 | extern crate compiler_builtins as other_compiler_builtins;
  |

error[E0522]: definition of an unknown language item: `panic_fmt`
 --> src/lang_items.rs:3:1
  |
3 | #[lang = "panic_fmt"] #[no_mangle] pub extern fn panic_fmt() -> ! { loop{} }
  | ^^^^^^^^^^^^^^^^^^^^^ definition of unknown language item `panic_fmt`

error: `#[panic_handler]` function required, but not found

error: aborting due to 3 previous errors

Some errors occurred: E0259, E0522.
For more information about an error, try `rustc --explain E0259`.
error: Could not compile `blinky`.

To learn more, run the command again with --verbose.
Makefile:35: recipe for target 'target/aarch64-none-elf/release/libblinky.a' failed
make: *** [target/aarch64-none-elf/release/libblinky.a] Error 101

可以看到系统提示出现了3个错误,将错误信息提取如下

error[E0259]: the name `compiler_builtins` is defined multiple times
error[E0522]: definition of an unknown language item: `panic_fmt`
error: `#[panic_handler]` function required, but not found

解决方案如下

Error:(5, 1) Can't find crate for `compiler_builtins` [E0463]

lib.rs

extern crate compiler_builtins;

删去即可

error[E0522]: definition of an unknown language item: `panic_fmt`
error: `#[panic_handler]` function required, but not found

这两行错误可同时解决
根据A Freestanding Rust Binary,在lang_items.rs中,引入PanicInfo

use core::panic::PanicInfo;

将这行删去

#[lang = "panic_fmt"] #[no_mangle] pub extern fn panic_fmt() -> ! { loop{} }

替换为

#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
    loop {}
}

就可以成功make了,但是会给出一个warning

warning: the feature `pointer_methods` has been stable since 1.26.0 and no longer requires an attribute to enable

lib.rs

#![feature(compiler_builtins_lib, lang_items, asm, pointer_methods)]

这行中的pointer_methods删去即可,到现在为止,make已经不报逻辑无关错误了,可以开始写代码。