rCore-Tutorial-Book-v3 icon indicating copy to clipboard operation
rCore-Tutorial-Book-v3 copied to clipboard

rCore-Tutorial-Book-v3/chapter1/2remove-std

Open utterances-bot opened this issue 4 years ago • 34 comments

移除标准库依赖 — rCore-Tutorial-Book-v3 0.1 文档

https://rcore-os.github.io/rCore-Tutorial-Book-v3/chapter1/2remove-std.html

utterances-bot avatar Feb 18 '21 09:02 utterances-bot

通过下面命令添加rust-std rustup target add riscv64gc-unknown-none-elf

Dyqer avatar Feb 18 '21 09:02 Dyqer

请问为何写了lang_items.rs后仍然提示 error: #[panic_handler] function required, but not found呢? (用的是前面配好的docker环境)

DylanYangYuchen avatar Feb 20 '21 07:02 DylanYangYuchen

明白了,前面没有说要加入 mod lang_items

DylanYangYuchen avatar Feb 20 '21 07:02 DylanYangYuchen

使用清华网盘镜像里的Ubuntu时,加上#![no_std] 来告诉 Rust 编译器不使用 Rust 标准库 std后,报的错误和书里的有点不一样,我这儿的异常信息如下,请问还需要安装软件么? error[E0463]: can't find crate for core | = note: the riscv64gc-unknown-none-elf target may not be installed

error: aborting due to previous error

For more information about this error, try rustc --explain E0463. error: could not compile os

leonhxx avatar Apr 27 '21 12:04 leonhxx

@leonhxx 请先输入以下命令安装一下相关软件:

rustup target add riscv64gc-unknown-none-elf
cargo install cargo-binutils --vers ~0.2
rustup component add llvm-tools-preview
rustup component add rust-src

wyfcyx avatar Apr 27 '21 12:04 wyfcyx

@wyfcyx 多谢呀!!

leonhxx avatar Apr 27 '21 13:04 leonhxx

error: language item required, but not found: eh_personality

error: aborting due to previous error

error: could not compile Os 你好我照着做以后出现这个报错是为什么呢?

storage-db avatar May 04 '21 08:05 storage-db

@eulerf 请按照这里所提到的:

我们首先在 os 目录下新建 .cargo 目录,并在这个目录下创建 config 文件,并在里面输入如下内容:

# os/.cargo/config
[build]
target = "riscv64gc-unknown-none-elf"

进行交叉编译,避免编译到x86目标。

wyfcyx avatar May 04 '21 08:05 wyfcyx

你好这个我已经照做了创建了config文件

------------------ 原始邮件 ------------------ 发件人: "Yifan @.>; 发送时间: 2021年5月4日(星期二) 下午4:26 收件人: @.>; 抄送: @.>; @.>; 主题: Re: [rcore-os/rCore-Tutorial-Book-v3] rCore-Tutorial-Book-v3/chapter1/2remove-std (#21)

@eulerf 请按照这里所提到的:

我们首先在 os 目录下新建 .cargo 目录,并在这个目录下创建 config 文件,并在里面输入如下内容:

os/.cargo/config [build] target = "riscv64gc-unknown-none-elf"

进行交叉编译,避免编译到x86目标。

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

storage-db avatar May 04 '21 08:05 storage-db

谢谢已经改正成功了

------------------ 原始邮件 ------------------ 发件人: "Yifan @.>; 发送时间: 2021年5月4日(星期二) 下午4:26 收件人: @.>; 抄送: @.>; @.>; 主题: Re: [rcore-os/rCore-Tutorial-Book-v3] rCore-Tutorial-Book-v3/chapter1/2remove-std (#21)

@eulerf 请按照这里所提到的:

我们首先在 os 目录下新建 .cargo 目录,并在这个目录下创建 config 文件,并在里面输入如下内容:

os/.cargo/config [build] target = "riscv64gc-unknown-none-elf"

进行交叉编译,避免编译到x86目标。

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

storage-db avatar May 04 '21 08:05 storage-db

如果你在src写入了lang_items.rs 仍然会有panic_handler的错误,请在main.rs中写入如下语句 mod lang_items;

main.rs:

#![no_std]

mod lang_items;

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

然后就可以继续执行了

wei-huan avatar May 10 '21 13:05 wei-huan

Rust的mod是不是相当于C语言的include

wei-huan avatar May 10 '21 13:05 wei-huan

@wei-huan 差不多吧。

wyfcyx avatar May 10 '21 13:05 wyfcyx

Entry入口不等于0,反汇编也是有对应代码的,跟教程不一致,请问是什么原因呢

➜  os git:(master) ✗ cat src/main.rs
#![no_std]
#![no_main]

mod lang_items;
➜  os git:(master) ✗ cat src/lang_items.rs
use core::panic::PanicInfo;

#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
    loop {}
}
➜  os git:(master) ✗ rust-readobj -h target/riscv64gc-unknown-none-elf/debug/os

File: target/riscv64gc-unknown-none-elf/debug/os
Format: elf64-littleriscv
Arch: riscv64
AddressSize: 64bit
LoadName: <Not found>
ElfHeader {
  Ident {
    Magic: (7F 45 4C 46)
    Class: 64-bit (0x2)
    DataEncoding: LittleEndian (0x1)
    FileVersion: 1
    OS/ABI: SystemV (0x0)
    ABIVersion: 0
    Unused: (00 00 00 00 00 00 00)
  }
  Type: Executable (0x2)
  Machine: EM_RISCV (0xF3)
  Version: 1
  Entry: 0x11120
  ProgramHeaderOffset: 0x40
  SectionHeaderOffset: 0x11C0
  Flags [ (0x5)
    EF_RISCV_FLOAT_ABI_DOUBLE (0x4)
    EF_RISCV_RVC (0x1)
  ]
  HeaderSize: 64
  ProgramHeaderEntrySize: 56
  ProgramHeaderCount: 4
  SectionHeaderEntrySize: 64
  SectionHeaderCount: 15
  StringTableSectionIndex: 13
}
➜  os git:(master) ✗ rust-objdump -S target/riscv64gc-unknown-none-elf/debug/os

target/riscv64gc-unknown-none-elf/debug/os:     file format elf64-littleriscv


Disassembly of section .text:

0000000000011120 <rust_begin_unwind>:
; fn panic(_info: &PanicInfo) -> ! {
   11120: 41 11         addi    sp, sp, -16
   11122: 2a e4         sd      a0, 8(sp)
   11124: 09 a0         j       0x11126 <rust_begin_unwind+0x6>
;     loop {}
   11126: 01 a0         j       0x11126 <rust_begin_unwind+0x6>

Joeyscat avatar May 22 '21 05:05 Joeyscat

补充:vscode上如果加上#[no_std]提示cann't find crate for test refer

https://github.com/rust-analyzer/rust-analyzer/issues/3801

LimingFang avatar Aug 17 '21 06:08 LimingFang

Q:为何在 main.rs中要加入 mod lang_items; 这条语句? A: 由于os是 no_std程序,所以没法调用panic!等属于rust std库中的宏。从ch1开始,我们就写了 lang_items.rs , 实现了自己的panic!宏,这就是你指出的 mod lang_items; 表示 main.rs 要调用的panic!,将是自己在写的lang_items.rs中的panic!宏。

chyyuu avatar Feb 25 '22 00:02 chyyuu

希望能在“我们创建一个新的子模块 lang_items.rs 实现panic函数......编译器用panic函数来对接 panic! 宏:”的位置添上“并在main.rs #![no_std]的下方加上mod lang_items;来声明模块”

然后把下面的Rust Tips:Rust 模块化编程的链接贴一个在附近。

iruhh avatar Feb 26 '22 03:02 iruhh

@iruhh 好主意。

wyfcyx avatar Feb 28 '22 12:02 wyfcyx

补充:vscode上如果加上#[no_std]提示cann't find crate for test refer

rust-analyzer/rust-analyzer#3801

不起作用。我的配置:

{
    "rust.target": "riscv64gc-unknown-none-elf",
    "rust.all_targets": false,
    "rust-analyzer.cargo-watch.allTargets": false,
    "rust-analyzer.cargo-watch.arguments": [
        "--target",
        "riscv64gc-unknown-none-elf"
    ]
}

使用的插件:两个都试过,都不行。

错误信息:

can't find crate for `test`

pluveto avatar Mar 06 '22 13:03 pluveto

@pluveto ,将 rust-analyzer.checkOnSave.target 设置为 riscv64gc-unknown-none-elf 就可以了:

{
    "rust-analyzer.cargo.target": "riscv64gc-unknown-none-elf",
    "rust-analyzer.checkOnSave.allTargets": false,
    "rust-analyzer.checkOnSave.target": "riscv64gc-unknown-none-elf"
}

之后 vscode 就没有报错了。

yilozt avatar Apr 04 '22 06:04 yilozt

补充:vscode上如果加上#[no_std]提示cann't find crate for test refer

rust-analyzer/rust-analyzer#3801

不起作用。我的配置:

{
    "rust.target": "riscv64gc-unknown-none-elf",
    "rust.all_targets": false,
    "rust-analyzer.cargo-watch.allTargets": false,
    "rust-analyzer.cargo-watch.arguments": [
        "--target",
        "riscv64gc-unknown-none-elf"
    ]
}

使用的插件:两个都试过,都不行。

错误信息:

can't find crate for `test`

对于此报错,还有一种可能是 cargo 插件的自动检查导致的,可在 settings.json 加入以下配置

{
  "cargo.automaticCheck": false
}

对于此错误的猜想,我也是从这里获取的灵感:https://github.com/rust-analyzer/rust-analyzer/issues/3801#issuecomment-1053887259

SnowWarri0r avatar Apr 08 '22 11:04 SnowWarri0r

补充:vscode上如果加上#[no_std]提示cann't find crate for test refer

rust-analyzer/rust-analyzer#3801

不起作用。我的配置:

{
    "rust.target": "riscv64gc-unknown-none-elf",
    "rust.all_targets": false,
    "rust-analyzer.cargo-watch.allTargets": false,
    "rust-analyzer.cargo-watch.arguments": [
        "--target",
        "riscv64gc-unknown-none-elf"
    ]
}

使用的插件:两个都试过,都不行。 错误信息:

can't find crate for `test`

对于此报错,还有一种可能是 cargo 插件的自动检查导致的,可在 settings.json 加入以下配置

{
  "cargo.automaticCheck": false
}

对于此错误的猜想,我也是从这里获取的灵感:rust-analyzer/rust-analyzer#3801 (comment)

nvim.coc 中使用 rust-analyzer 时,在CoC配置中写入:

"rust-analyzer.checkOnSave.allTargets": false,

可避免问题。

MrZLeo avatar Apr 08 '22 13:04 MrZLeo

噗哈哈哈哈, 写了个寂寞, 全删掉了🤣

An-n-ya avatar Apr 15 '22 13:04 An-n-ya

依照在本章最后,换回之前默认的 x86_64-unknown-linux-gnu,cargo build 以后,有一个报错始终出现

error: language item required, but not found: `eh_personality`
  |
  = note: this can occur when a binary crate with `#![no_std]` is compiled for a target where `eh_personality` is defined in the standard library
  = help: you may be able to compile for a target that doesn't need `eh_personality`, specify a target with `--target` or in `.cargo/config`

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

lemon5227 avatar Apr 22 '22 16:04 lemon5227

楼主文档写得很详细也比较容易看懂,但是代码能不能加点注释

DrYaling avatar Jun 22 '22 01:06 DrYaling

he@ubuntu:~/C_TEST/os$ rust-objdump -S target/riscv64gc-unknown-none-elf/debug/os Failed to execute tool: objdump No such file or directory (os error 2) he@ubuntu:~/C_TEST/os$ rust-readobj -h target/riscv64gc-unknown-none-elf/debug/os Failed to execute tool: readobj No such file or directory (os error 2)

那个反汇编工具用不了啊

zhuiYeah avatar Jul 02 '22 03:07 zhuiYeah

如果你在src写入了lang_items.rs 仍然会有panic_handler的错误,请在main.rs中写入如下语句 mod lang_items;

main.rs:

#![no_std]

mod lang_items;

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

然后就可以继续执行了

这里的main.rs:的冒号报错了

Wonderland2333 avatar Jul 04 '22 12:07 Wonderland2333

依照在本章最后,换回之前默认的 x86_64-unknown-linux-gnu,cargo build 以后,有一个报错始终出现

error: language item required, but not found: `eh_personality`
  |
  = note: this can occur when a binary crate with `#![no_std]` is compiled for a target where `eh_personality` is defined in the standard library
  = help: you may be able to compile for a target that doesn't need `eh_personality`, specify a target with `--target` or in `.cargo/config`

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

对照博客做吧,三元组不一样,不是改一个参数就能解决掉的。(虽然改动确实也不大)

Unik-lif avatar Jan 27 '23 12:01 Unik-lif

对于 error[E0463]: can't find crate for test 问题,可以将 rust-analyzer.check.allTargets 设置为 false。在设置界面修改或写入 setting.json 文件都可以。

另外,这是在 os 目录下有 .cargo/config 文件注明了 target,如果没有此文件,可能还需加入 target。 比如,"rust-analyzer.cargo.target": "riscv64imac-unknown-none-elf",

jklincn avatar Jan 27 '23 14:01 jklincn

补充:vscode上如果加上#[no_std]提示cann't find crate for test refer

rust-analyzer/rust-analyzer#3801

不起作用。我的配置:

{
    "rust.target": "riscv64gc-unknown-none-elf",
    "rust.all_targets": false,
    "rust-analyzer.cargo-watch.allTargets": false,
    "rust-analyzer.cargo-watch.arguments": [
        "--target",
        "riscv64gc-unknown-none-elf"
    ]
}

使用的插件:两个都试过,都不行。 错误信息:

can't find crate for `test`

对于此报错,还有一种可能是 cargo 插件的自动检查导致的,可在 settings.json 加入以下配置

{
  "cargo.automaticCheck": false
}

对于此错误的猜想,我也是从这里获取的灵感:rust-lang/rust-analyzer#3801 (comment)

确实是cargo插件的问题,一个巨坑

{
   "cargo.automaticCheck": false
}

这行东西最新版本好像没有用,我的选择是直接删除cargo插件

lwb-2021 avatar Dec 02 '23 06:12 lwb-2021