How to build with no_core

Hello,
I am trying to build the following code as a (sample)[Compile rust code with no_core failed, `start` is a generic function? - Stack Overflow] but not successful. Can someone help?
The code is:

#![feature(no_core)]
#![feature(lang_items)]
#![no_core]

#[cfg(target_os = "linux")]
#[link(name = "c")]
extern {}
#[cfg(target_os = "linux")]
#[link(name = "System")]
extern {}

#[lang = "sized"]
pub trait Sized {}
#[lang = "copy"]
pub trait Copy {}

pub trait Termination {
    fn report(self) -> i32;
}

#[lang = "start"]
fn start<T>(_main: fn() -> T, _argc: isize, _argv: *const *const u8, _: u8) -> isize {
    42
}

fn main() {}

The compile command is:
cargo +nightly rustc -Z unstable-options --bin main_0 -- -C link-arg=nostartfiles
The error is:

warning: `/home/trinhtuan/Rust/Generic/before-main/.cargo/config` is deprecated in favor of `config.toml`
note: if you need to support cargo 1.38 or earlier, you can symlink `config` to `config.toml`
   Compiling before-main v0.1.0 (/home/trinhtuan/Rust/Generic/before-main)
warning: the feature `lang_items` is internal to the compiler or standard library
 --> src/main_0.rs:2:12
  |
2 | #![feature(lang_items)]
  |            ^^^^^^^^^^
  |
  = note: using it is strongly discouraged
  = note: `#[warn(internal_features)]` on by default

error: linking with `cc` failed: exit status: 1
  |
  = note: LC_ALL="C" PATH="/home/trinhtuan/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/bin:/home/trinhtuan/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/bin/self-contained:/home/trinhtuan/.rustup/toolchains/esp_1.82.0/xtensa-esp-elf/esp-13.2.0_20230928/xtensa-esp-elf/bin:/home/trinhtuan/.rustup/toolchains/esp_1.79.0/xtensa-esp-elf/esp-13.2.0_20230928/xtensa-esp-elf/bin:/home/trinhtuan/.rustup/toolchains/esp_1.81.0/xtensa-esp-elf/esp-13.2.0_20230928/xtensa-esp-elf/bin:/home/trinhtuan/.rustup/toolchains/esp_1.80.0/xtensa-esp-elf/esp-13.2.0_20230928/xtensa-esp-elf/bin:/home/trinhtuan/.cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/wsl/lib:/mnt/c/Windows/system32:/mnt/c/Windows:/mnt/c/Windows/System32/Wbem:/mnt/c/Windows/System32/WindowsPowerShell/v1.0/:/mnt/c/Windows/System32/OpenSSH/:/mnt/c/Program Files (x86)/NVIDIA Corporation/PhysX/Common:/mnt/c/Program Files/NVIDIA Corporation/NVIDIA NvDLISR:/mnt/c/Program Files/usbipd-win/:/mnt/c/Users/ADMIN/AppData/Local/Microsoft/WindowsApps:/mnt/c/Users/ADMIN/AppData/Local/GitHubDesktop/bin:/snap/bin:/opt/nvim-linux64/bin" VSLANG="1033" "cc" "-m64" "/tmp/rustccCBWEs/symbols.o" "/home/trinhtuan/Rust/Generic/before-main/target/debug/deps/main_0-73c1f55cbcb3c12f.7du9joo2h4k326jyul7tjsnxr.rcgu.o" "/home/trinhtuan/Rust/Generic/before-main/target/debug/deps/main_0-73c1f55cbcb3c12f.87m676mfprmg4iw6iih0butwd.rcgu.o" "-Wl,--as-needed" "-Wl,-Bdynamic" "-lc" "-lSystem" "-B/home/trinhtuan/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/bin/gcc-ld" "-fuse-ld=lld" "-Wl,--eh-frame-hdr" "-Wl,-z,noexecstack" "-L" "/home/trinhtuan/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-o" "/home/trinhtuan/Rust/Generic/before-main/target/debug/deps/main_0-73c1f55cbcb3c12f" "-Wl,--gc-sections" "-pie" "-Wl,-z,relro,-z,now" "-nodefaultlibs" "nostartfiles" "-lc"
  = note: rust-lld: error: unable to find library -lSystem
          rust-lld: error: cannot open nostartfiles: No such file or directory
          collect2: error: ld returned 1 exit status


warning: `before-main` (bin "main_0") generated 1 warning
error: could not compile `before-main` (bin "main_0") due to 1 previous error; 1 warning emitted
1 Like

I spotted the typo, just change #[link(name = "System")] to #[link(name = "c")].

1 Like

Or, more correctly, change

#[cfg(target_os = "linux")]
#[link(name = "System")]

to

#[cfg(target_os = "macos")]
#[link(name = "System")]

since for Linux you already have the necessary linking attributes two lines before.

1 Like