0

I wanted to start a Rust no_std project, so I simply created a new cargo package and wrote in main.rs the following lines:

#![feature(lang_items, start)]
#![no_std]

extern crate libc;

#[start]
fn main(_argc: isize, _argv: *const *const u8) -> isize{

    1
}

#[panic_handler]
fn panic(_: &core::panic::PanicInfo) -> !{
    loop{}
}
#[lang = "eh_personality"] extern fn eh_personality() {}

The Cargo.toml file looks like that:

[dependencies]
libc = "0.2.71"

[profile.dev]
panic = "abort"

[profile.release]
panic = "abort"

I ran cargo build and the linker printed:

LINK : error LNK2001: unresolved external symbol _mainCRTStartup

What could be the reason behind that error ?

1 Answers1

0

Default build target on windows uses mscv toolchain, which includes a dynamically-linked libc. This ones is a C-runtime, which, in particularly, includes an undefined main symbol (which assumed to be defined by a programmer), therefore at linking stage the linker cannot find this missing symbol. You need explicitly specify that you don't need it via #![no_main] at the beginning of main.rs.

I also would suggest you reading A Freestanding Rust Binary which explain much more in details.

Kitsu
  • 3,166
  • 14
  • 28