You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rust内核编译报asm宏未找到错误,是否需回退版本?

Rust内核编译cargo bootimage时出现asm宏找不到的大量错误

错误现象

执行cargo bootimage编译时,编译x86_64 v0.7.7依赖包出现大量cannot find macro 'asm' in this scope错误,示例错误如下:

Building kernel
   Compiling x86_64 v0.7.7

error: cannot find macro `asm` in this scope
  --> /home/ubuntu/.cargo/registry/src/mirrors.ustc.edu.cn-61ef6e0cd06fb9b8/x86_64-0.7.7/src/registers/rflags.rs:96:18
   |
96 |         unsafe { asm!("pushq $0; popfq" :: "r"(val) : "memory" "flags") };
   |                  ^^^
   |
help: consider importing this macro
   |
67 +     use core::arch::asm;
   |

error: cannot find macro `asm` in this scope
  --> /home/ubuntu/.cargo/registry/src/mirrors.ustc.edu.cn-61ef6e0cd06fb9b8/x86_64-0.7.7/src/registers/rflags.rs:79:18
   |
79 |         unsafe { asm!("pushfq; popq $0" : "=r"(r) :: "memory") };
   |                  ^^^
   |
help: consider importing this macro
   |
67 +     use core::arch::asm;
   |

此类错误还有数十个,不再一一列举。

运行环境

  • 系统:Ubuntu Server 18.04 LTS 64bit
  • Rust版本:
rustc 1.73.0-nightly (03a119b0b 2023-08-07)

项目配置代码

src/main.rs

#![no_std] // 不链接Rust标准库
#![no_main] // 禁用所有Rust层级的入口点
use core::panic::PanicInfo;
static HELLO: &[u8] = b"Hello World!";
#[no_mangle] // 不重整函数名
pub extern "C" fn _start() -> ! {
    // 因为编译器会寻找一个名为`_start`的函数,所以这个函数就是入口点
    // 默认命名为`_start`
    let vga_buffer = 0xb8000 as *mut u8;
    for (i, &byte) in HELLO.iter().enumerate() {
        unsafe {
            *vga_buffer.offset(i as isize * 2) = byte;
            *vga_buffer.offset(i as isize * 2 + 1) = 0xb;
        }
    }    
    loop {}
}
/// 这个函数将在panic时被调用
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
    loop {}
}

Cargo.toml

[package]
name = "menghan_os"
version = "0.1.0"
# 使用`cargo build`编译时需要的配置
[profile.dev]
panic = "abort" # 禁用panic时栈展开
# 使用`cargo build --release`编译时需要的配置
[profile.release]
panic = "abort" # 禁用panic时栈展开
[dependencies]
bootloader = "0.6.0"

x86_64-menghan_os.json

{
  "llvm-target": "x86_64-unknown-none",
  "data-layout": "e-m:e-i64:64-f80:128-n8:16:32:64-S128",
  "arch": "x86_64",
  "target-endian": "little",
  "target-pointer-width": "64",
  "target-c-int-width": "32",
  "os": "none",
  "executables": true,
  "linker-flavor": "ld.lld",
  "linker": "rust-lld",
  "panic-strategy": "abort",
  "disable-redzone": true,
  "features": "-mmx,-sse,+soft-float"
}

rust-toolchain

nightly

问题询问

是否需要回退Rust版本?若需要应回退到哪个版本?还是存在其他与版本无关的错误?


内容的提问来源于stack exchange,提问作者StudioMenghan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 17:24:49