Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
utterances-bot opened this issue Feb 18, 2021 · 32 comments
Open

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

utterances-bot opened this issue Feb 18, 2021 · 32 comments
Labels
comments An area where readers can discuss related topics after every article.

Comments

@utterances-bot
Copy link

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

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

Copy link

Dyqer commented Feb 18, 2021

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

@wyfcyx wyfcyx added the comments An area where readers can discuss related topics after every article. label Feb 20, 2021
Copy link

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

Copy link

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

Copy link

leonhxx commented Apr 27, 2021

使用清华网盘镜像里的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

@wyfcyx
Copy link
Collaborator

wyfcyx commented Apr 27, 2021

@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

Copy link

leonhxx commented Apr 27, 2021

@wyfcyx 多谢呀!!

Copy link

error: language item required, but not found: eh_personality

error: aborting due to previous error

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

@wyfcyx
Copy link
Collaborator

wyfcyx commented May 4, 2021

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

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

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

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

@storage-db
Copy link

storage-db commented May 4, 2021 via email

@storage-db
Copy link

storage-db commented May 4, 2021 via email

Copy link

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

main.rs:

#![no_std]

mod lang_items;

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

然后就可以继续执行了

Copy link

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

@wyfcyx
Copy link
Collaborator

wyfcyx commented May 10, 2021

@wei-huan 差不多吧。

Copy link

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>

Copy link

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

rust-lang/rust-analyzer#3801

Copy link
Member

chyyuu commented Feb 25, 2022

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!宏。

Copy link

iruhh commented Feb 26, 2022

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

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

@wyfcyx
Copy link
Collaborator

wyfcyx commented Feb 28, 2022

@iruhh 好主意。

@pluveto
Copy link
Contributor

pluveto commented Mar 6, 2022

补充: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`

@yilozt
Copy link

yilozt commented Apr 4, 2022

@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 就没有报错了。

@SnowWarri0r
Copy link

SnowWarri0r commented Apr 8, 2022

补充: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)

@MrZLeo
Copy link

MrZLeo commented Apr 8, 2022

补充: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,

可避免问题。

Copy link

An-n-ya commented Apr 15, 2022

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

Copy link

依照在本章最后,换回之前默认的 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

Copy link

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

Copy link

zhuiYeah commented Jul 2, 2022

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)

那个反汇编工具用不了啊

Copy link

如果你在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:的冒号报错了

@Unik-lif
Copy link

依照在本章最后,换回之前默认的 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

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

@jklincn
Copy link

jklincn commented Jan 27, 2023

对于 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",

@lwb-2021
Copy link

lwb-2021 commented Dec 2, 2023

补充: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插件

Copy link

我取消了config中的[build]设置,也就是切换到了x86_64-unknown-linux-gnu,按照blogOS的教程在cargo.toml中添加了如下代码,禁用多处 panic! 栈展开

[profile.dev]
panic = "abort"

[profile.release]
panic = "abort"

在这之后遇到了linker cc error
原因大概是在这样的三元组链接条件下,linux上的c库需要找到入口函数
但是我们没有:(
因此使用如下命令 成功完成build

cargo rustc -- -C link-arg=-nostartfiles

Done!

Copy link

添加#![no_std]后报错,配置 VSCode 无效的,也可以添加以下配置解决(亲测有效):

# Cargo.toml

[package]
name = "xxx"
version = "0.1.0"
edition = "2021"

[[bin]]
name = "xxx"
test = false
bench = false

[dependencies]

详见:rust-lang/vscode-rust#729 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comments An area where readers can discuss related topics after every article.
Projects
None yet
Development

No branches or pull requests