为什么不编译此代码? (x86_64,Ubuntu Linux)
main.rs
#![feature(lang_items)]
#![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 {}
}
#[lang = "eh_personality"]
extern "C" fn eh_personality() {}
Cargo.toml
[package]
name = "decaddr"
version = "0.1.0"
authors = ["niko"]
edition = "2018"
[dependencies]
libc = { version = "0.2" , features = ["extra_traits"] }
[profile.dev]
panic = "abort"
[profile.release]
panic = "abort"
错误是:
niko@lap:~/rustcode/decaddr$ cargo build --verbose
Fresh libc v0.2.55
Compiling decaddr v0.1.0 (/home/niko/rustcode/decaddr)
Running `rustc --edition=2018 --crate-name decaddr src/main.rs --color always --crate-type bin --emit=dep-info,link -C panic=abort -C debuginfo=2 -C metadata=df8e75f6f5ad2fb8 -C extra-filename=-df8e75f6f5ad2fb8 --out-dir /home/niko/rustcode/decaddr/target/debug/deps -C incremental=/home/niko/rustcode/decaddr/target/debug/incremental -L dependency=/home/niko/rustcode/decaddr/target/debug/deps --extern libc=/home/niko/rustcode/decaddr/target/debug/deps/liblibc-5f6fa16b68152311.rlib`
error: linking with `cc` failed: exit code: 1
|
= note: "cc" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-m64" "-L" "/home/niko/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "/home/niko/rustcode/decaddr/target/debug/deps/decaddr-df8e75f6f5ad2fb8.2eeyjncolvpqxm5x.rcgu.o" "/home/niko/rustcode/decaddr/target/debug/deps/decaddr-df8e75f6f5ad2fb8.31phrjq5za9cdsbr.rcgu.o" "-o" "/home/niko/rustcode/decaddr/target/debug/deps/decaddr-df8e75f6f5ad2fb8" "-Wl,--gc-sections" "-pie" "-Wl,-zrelro" "-Wl,-znow" "-nodefaultlibs" "-L" "/home/niko/rustcode/decaddr/target/debug/deps" "-L" "/home/niko/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-Wl,-Bstatic" "/home/niko/rustcode/decaddr/target/debug/deps/liblibc-5f6fa16b68152311.rlib" "/home/niko/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_core-29eb95682c6c4d0d.rlib" "/home/niko/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-3204fb057f1d714e.rlib" "/home/niko/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-ed9475e351b9a592.rlib" "-Wl,-Bdynamic" "-lutil" "-lutil"
= note: /usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/Scrt1.o: In function `_start':
(.text+0x12): undefined reference to `__libc_csu_fini'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/Scrt1.o: In function `_start':
(.text+0x19): undefined reference to `__libc_csu_init'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/Scrt1.o: In function `_start':
(.text+0x25): undefined reference to `__libc_start_main'
/home/niko/rustcode/decaddr/target/debug/deps/decaddr-df8e75f6f5ad2fb8.31phrjq5za9cdsbr.rcgu.o: In function `main':
/home/niko/rustcode/decaddr/src/main.rs:11: undefined reference to `printf'
collect2: error: ld returned 1 exit status
error: aborting due to previous error
error: Could not compile `decaddr`.
Caused by:
process didn't exit successfully: `rustc --edition=2018 --crate-name decaddr src/main.rs --color always --crate-type bin --emit=dep-info,link -C panic=abort -C debuginfo=2 -C metadata=df8e75f6f5ad2fb8 -C extra-filename=-df8e75f6f5ad2fb8 --out-dir /home/niko/rustcode/decaddr/target/debug/deps -C incremental=/home/niko/rustcode/decaddr/target/debug/incremental -L dependency=/home/niko/rustcode/decaddr/target/debug/deps --extern libc=/home/niko/rustcode/decaddr/target/debug/deps/liblibc-5f6fa16b68152311.rlib` (exit code: 1)
niko@lap:~/rustcode/decaddr$
答案 0 :(得分:1)
解决了添加
rustflags = ["-C", "link-args=-lc"]
到包根目录下的.cargo/config
。