#libc linking error

10 messages · Page 1 of 1 (latest)

clear plank
#

Hello, I'm an intermediate rust programmer. I've made no_std projects before, but now for whatever reason, the compiler won't link with libc properly. After searching, I initially thought this was because printf and various stdio functions were inlined by default and couldn't be detected, however this error also applies to any function in libc.
The code I am using is just a basic no_std program.

#![no_std]
#![no_main]

use core::panic::PanicInfo;

#[no_mangle]
pub fn mainCRTStartup() {
    main()
}

#[no_mangle]
fn main() {
    unsafe {
        libc::printf("Hello!\n\0".as_ptr() as _);
        let x = libc::malloc(32);
    }
}

#[panic_handler]
fn panic_handler(info: &PanicInfo) -> ! {
    loop{}
}

(Yes I know I should be changing the settings in the linker so I don't have to define mainCRTstartup, but this is just for demonstration purposes)
This is my cargo.toml

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

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
libc = { version = "0.2.137", default-features = false }

[profile.dev]
panic = "abort"

[profile.release]
panic = "abort"

Attempting to compile gives these errors:
I've been trying to fix this for the past week so any help is appreciated!

lament ibex
#

Hmm, it doesn't find malloc.
Does it work without the malloc?

clear plank
near yarrow
#

libc is a unix like system lib and not a Windows one, the equivalent on windows would be MSVCRT and it has malloc in it : https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/malloc?view=msvc-170. If you want to use an implantation in rust I would recommend the Windows-rs crate : https://github.com/microsoft/windows-rs and as for malloc part you can use : https://docs.rs/windows-sys/latest/windows_sys/Win32/System/Memory/fn.HeapAlloc.html.

GitHub

Rust for Windows. Contribute to microsoft/windows-rs development by creating an account on GitHub.

clear plank
#

Additionally, why does libc work when not using #![no_std]?

near yarrow
near yarrow
rapid pebble
#

Rust's std does this for you but no_std doesn't.