"Hello World" no_std linker error

Hi,

I've been trying to get this "Hello World" to build under the latest stable (1.46.0), and it won't work.

I copied the source from the linked post and get the following error:

error: linking with `cc` failed: exit code: 1
  |
  = note: "cc" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-m64" "-Wl,--eh-frame-hdr" "-L" "/home/ambiso/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "/home/ambiso/Documents/Projects/echo/target/debug/deps/echo-8931c6e21c0c4d8a.4gijvs203q71b4l8.rcgu.o" "/home/ambiso/Documents/Projects/echo/target/debug/deps/echo-8931c6e21c0c4d8a.r3qi88wqf9d15wl.rcgu.o" "-o" "/home/ambiso/Documents/Projects/echo/target/debug/deps/echo-8931c6e21c0c4d8a" "-Wl,--gc-sections" "-pie" "-Wl,-zrelro" "-Wl,-znow" "-nodefaultlibs" "-L" "/home/ambiso/Documents/Projects/echo/target/debug/deps" "-L" "/home/ambiso/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-Wl,-Bstatic" "/home/ambiso/Documents/Projects/echo/target/debug/deps/liblibc-fe6f0c4fc5d79e24.rlib" "/home/ambiso/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_core-cbfb51de52131460.rlib" "/home/ambiso/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-97497c26fddb7882.rlib" "/home/ambiso/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-f1a9d8c443e20b5e.rlib" "-Wl,-Bdynamic" "-lutil" "-ldl" "-lutil"
  = note: /usr/bin/ld: /usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../lib/Scrt1.o: in function `_start':
          (.text+0x16): undefined reference to `__libc_csu_fini'
          /usr/bin/ld: (.text+0x1d): undefined reference to `__libc_csu_init'
          /usr/bin/ld: (.text+0x2a): undefined reference to `__libc_start_main'
          /usr/bin/ld: /home/ambiso/Documents/Projects/echo/target/debug/deps/echo-8931c6e21c0c4d8a.4gijvs203q71b4l8.rcgu.o: in function `main':
          /home/ambiso/Documents/Projects/echo/src/main.rs:11: undefined reference to `printf'
          collect2: error: ld returned 1 exit status


error: aborting due to previous error

Here's the main.rs I'm using:

#![no_std]
#![no_main]

extern crate libc;

#[no_mangle]
pub extern "C" fn main(_argc: isize, _argv: *const *const u8) -> isize {
    // Since we are passing a C string the final null character is mandatory
    const HELLO: &'static str = "Hello, world!\n\0";
    unsafe {
        libc::printf(HELLO.as_ptr() as *const _);
    }
    0
}

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

Here is my Cargo.toml:

[package]
name = "echo"
version = "0.1.0"
authors = ["ambiso <ambiso@invalid>"]
edition = "2018"

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

[dependencies]

libc = { version = "*", features = [] }

[profile.dev]
panic = 'abort'

[profile.release]
# opt-level = 'z'  # Optimize for size.
# lto = true
# codegen-units = 1
panic = 'abort'

I've found this related issue but I couldn't make it work.

Thanks for any help!

Try deleting the target folder.

Unfortunately that did not help, I get the exact same error.

I've fixed it by adding -lc to the rustc command.
This can also be achieved by adding the following file in .cargo/config

[build]
rustflags = ["-C", "link-args=-lc"]

I'm not sure why seemingly nobody has encountered this error before.
Why isn't rust linking with libc by itself?

It only links libc when it is told to do so. Normally this is done by the libc crate used as dependency of libstd.

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.