使用#![no_std]时的链接器错误与生锈错误

发布于 2025-02-02 04:57:59 字数 1545 浏览 2 评论 0原文

我有一个用Rust制作的小程序,它几乎什么也没做,但它不使用STD库。看起来像这样:

#![no_std]
#![no_main]

use core::panic::PanicInfo;

#[panic_handler]
fn panic(_panic: &PanicInfo<'_>) -> ! {
    loop {}
}

我还必须将其添加到我的cargo.toml中,否则它会抱怨eh_personality,不确定它是什么。

[package]
name = "hello-i686"
version = "0.1.0"
edition = "2021"

[dependencies]

[profile.dev]
panic = "abort"

[profile.release]
panic = "abort"

但是,每当我尝试使用货物运行进行编译时,我会得到错误:与CC链接到失败:退出状态:1

note: /usr/bin/ld: /usr/lib/gcc/x86_64-pc-linux-gnu/12.1.0/../../../../lib/Scrt1.o: in function `_start':
      /build/glibc/src/glibc/csu/../sysdeps/x86_64/start.S:103: undefined reference to `main'
      /usr/bin/ld: /build/glibc/src/glibc/csu/../sysdeps/x86_64/start.S:115: undefined reference to `__libc_start_main'
      collect2: error: ld returned 1 exit status

= help: some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified
= note: use the `-l` flag to specify native libraries to link
= note: use the `cargo:rustc-link-lib` directive to specify the native libraries to link with Cargo (see https://doc.rust-lang.org/cargo/reference/build-scripts.html#cargorustc-link-libkindname)

为了了解问题到底是什么,我试图使用C ++制作一个简单的Hello World程序,并自行链接它,当我不得不链接对象文件时,我意识到我从未触摸过C ++,我没有知道我在做什么。我发现crt0.o在我的系统中找不到任何地方,我从来没有真正让我的C ++程序工作。我觉得问题是我的开发环境,但是我已经重新安装了大约3次基础,我不知道问题所在。我在Artix Linux上是否有帮助。当我使用性标准库时,一切都没有挂断。

I have a little program that I made with Rust, it does literally nothing, but it does not use the std library. It looks like this:

#![no_std]
#![no_main]

use core::panic::PanicInfo;

#[panic_handler]
fn panic(_panic: &PanicInfo<'_>) -> ! {
    loop {}
}

I also had to add this to my cargo.toml otherwise it complains about eh_personality, not sure what it is.

[package]
name = "hello-i686"
version = "0.1.0"
edition = "2021"

[dependencies]

[profile.dev]
panic = "abort"

[profile.release]
panic = "abort"

But whenever I try to compile it with cargo run, I get a error: linking with cc failed: exit status: 1.

note: /usr/bin/ld: /usr/lib/gcc/x86_64-pc-linux-gnu/12.1.0/../../../../lib/Scrt1.o: in function `_start':
      /build/glibc/src/glibc/csu/../sysdeps/x86_64/start.S:103: undefined reference to `main'
      /usr/bin/ld: /build/glibc/src/glibc/csu/../sysdeps/x86_64/start.S:115: undefined reference to `__libc_start_main'
      collect2: error: ld returned 1 exit status

= help: some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified
= note: use the `-l` flag to specify native libraries to link
= note: use the `cargo:rustc-link-lib` directive to specify the native libraries to link with Cargo (see https://doc.rust-lang.org/cargo/reference/build-scripts.html#cargorustc-link-libkindname)

In pursuit to understand what exactly the problem is, I tried to make a simple hello world program with C++ and link it on my own, by the time I had to link my object files, I realized I have never touched C++ and I have no clue what I'm doing. I found out crt0.o is nowhere to be found in my system, I never actually got my C++ program working. I feel like the problem is my development environment, but I've reinstalled base-devel about 3 times now, and I dont have a clue where the issue is. I'm on Artix Linux if that helps at all. When I do use the std library, everything goes off without a hitch.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花伊自在美 2025-02-09 04:57:59

您需要链接到libc以使其工作,因此请添加以下代码:

#[link(name="c")]
extern "C" {
}

这将解决有关丢失的libc_start_main的错误,但是您会收到有关错误的错误。缺少main函数,您可以通过添加:

#[no_mangle]
fn main(argc: isize, _argv: *const *const u8) -> i32 {
    0
}

You need to link to libc in order for this to work, so add the following code:

#[link(name="c")]
extern "C" {
}

This will fix the error about the missing __libc_start_main, but then you get an error about a missing main function which you can fix by adding:

#[no_mangle]
fn main(argc: isize, _argv: *const *const u8) -> i32 {
    0
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文