Linking errors when building a project targeting i586-pc-windows-msvc with #![no_std]

main.rs:

#![no_std]
#![feature(lang_items, rustc_private)]
#![no_main]

extern crate libc;

use libc_print::std_name::println;

#[no_mangle]
pub extern "C" fn main() -> isize {	
	println!("Hello, world!");
	0
}

#[panic_handler]
fn my_panic(_info: &core::panic::PanicInfo) -> ! {    
	loop {}
}

Cargo.toml:

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

[profile.dev]
panic = "abort"
lto = true

[profile.release]
panic = "abort"
lto = true

[dependencies]
libc = "0.2.137"
libc-print = "0.1.20"

.cargo/config:

[build]
target = "i586-pc-windows-msvc"

[target.i586-pc-windows-msvc]
rustflags = [
	"-C", "target-feature=+crt-static",
	"-C", "link-args=/SUBSYSTEM:CONSOLE,5.01",
	"-C", "link-args=/ENTRY:main"
]

Hello forum members. I'm trying to build a Windows XP compatible application and I almost got it right. I was able to build and run the application on Windows XP without using libc and libc_print. Now I want to force the application to output something to the console, but there are linking errors.
Building with cargo build:

  = note: hello.libc_print-a3642b304a51d155.libc_print.a8aae383-cgu.0.rcgu.o.rcg
u.o : error LNK2019: unresolved external symbol _memset referenced in function _
_ZN79_$LT$core..num..dec2flt..decimal..Decimal$u20$as$u20$core..default..Default
$GT$7default17h03531446058f8c6aE
          hello.libc_print-a3642b304a51d155.libc_print.a8aae383-cgu.0.rcgu.o.rcg
u.o : error LNK2019: unresolved external symbol __aulldiv referenced in function
 __ZN4core3num7dec2flt7decimal7Decimal10left_shift17h6adffb13b51ab8d8E
          libcompiler_builtins-c1e21d78243ec28d.rlib(compiler_builtins-c1e21d782
43ec28d.compiler_builtins.a051bfb8-cgu.5.rcgu.o) : error LNK2001: unresolved ext
ernal symbol __aulldiv
          hello.libc_print-a3642b304a51d155.libc_print.a8aae383-cgu.0.rcgu.o.rcg
u.o : error LNK2019: unresolved external symbol _memcpy referenced in function _
_ZN59_$LT$core..fmt..Arguments$u20$as$u20$core..fmt..Display$GT$3fmt17h1cd5ac113
c5e1981E
          hello.libc_print-a3642b304a51d155.libc_print.a8aae383-cgu.0.rcgu.o.rcg
u.o : error LNK2019: unresolved external symbol _strlen referenced in function _
_ZN4core3ffi5c_str4CStr8from_ptr9strlen_rt17hfd157d679c3581e3E
          hello.libc_print-a3642b304a51d155.libc_print.a8aae383-cgu.0.rcgu.o.rcg
u.o : error LNK2019: unresolved external symbol _memcmp referenced in function _
_ZN63_$LT$core..ffi..c_str..CStr$u20$as$u20$core..cmp..PartialEq$GT$2eq17hc38cf8
c04341dd6eE
          hello.libc_print-a3642b304a51d155.libc_print.a8aae383-cgu.0.rcgu.o.rcg
u.o : error LNK2019: unresolved external symbol __aullrem referenced in function
 __ZN4core3fmt3num14parse_u64_into17h1f7a4fd92fea9b13E
          hello.libc_print-a3642b304a51d155.libc_print.a8aae383-cgu.0.rcgu.o.rcg
u.o : error LNK2019: unresolved external symbol __write referenced in function _
_ZN10libc_print10libc_write17hfe22ac8070ce25c4E
          hello.libc_print-a3642b304a51d155.libc_print.a8aae383-cgu.0.rcgu.o.rcg
u.o : error LNK2001: unresolved external symbol __fltused
          C:\Users\User\Desktop\hello\target\i586-pc-windows-msvc\debug\deps\hel
lo.exe : fatal error LNK1120: 8 unresolved externals

Building with cargo build --release:

  = note: hello.hello.86dd99d8-cgu.1.rcgu.o : error LNK2019: unresolved external
 symbol __write referenced in function __ZN50_$LT$$RF$mut$u20$W$u20$as$u20$core.
.fmt..Write$GT$10write_char17h9ce0c9d54ae0d9a6E
          C:\Users\User\Desktop\hello\target\i586-pc-windows-msvc\release\deps\h
ello.exe : fatal error LNK1120: 1 unresolved externals

Am I doing something wrong, or do I need to include some additional libraries for successful linking? Is it even possible to support libc with #![no_std]? Thanks.

You need extern crate compiler_builtins; too. compiler_builtins provides a lot of functions that the compiler can call when the cpu doesn't have native support for eg 128bit multiplication or copying a chunk of memory.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.