Reputation: 787
I have a rust program named hello.rs.
The program is I'm unable to compile it using rustc. I generated the hello.rs using c2rust online transpiler. But if I use cargo run hello.rs the program runs smoothly.
When using rustc it shows the following error. How to fix it?
The original hello.c file:
void main() {
// printf() displays the string inside quotation
printf("Hello, World!");
}
The hello.rs file.
#![allow(dead_code, mutable_transmutes, non_camel_case_types, non_snake_case,
non_upper_case_globals, unused_assignments, unused_mut)]
#![register_tool(c2rust)]
#![feature(main, register_tool)]
extern "C" {
#[no_mangle]
fn printf(_: *const libc::c_char, _: ...) -> libc::c_int;
}
unsafe fn main_0() {
// printf() displays the string inside quotation
printf(b"Hello, World!\x00" as *const u8 as *const libc::c_char);
}
#[main]
pub fn main() { unsafe { main_0() } ::std::process::exit(0i32); }
The error message:
┌──(pegasus㉿pegasus)-[~/Documents/Rust_testing]
└─$ rustc hello.rs -o test2
error[E0557]: feature has been removed
--> hello.rs:4:12
|
4 | #![feature(main, register_tool)]
| ^^^^ feature has been removed
error: cannot find attribute `main` in this scope
--> hello.rs:13:3
|
13 | #[main]
| ^^^^
|
= note: `main` is in scope, but it is a function, not an attribute
error[E0433]: failed to resolve: use of undeclared crate or module `libc`
--> hello.rs:7:21
|
7 | fn printf(_: *const libc::c_char, _: ...) -> libc::c_int;
| ^^^^ use of undeclared crate or module `libc`
error[E0433]: failed to resolve: use of undeclared crate or module `libc`
--> hello.rs:7:46
|
7 | fn printf(_: *const libc::c_char, _: ...) -> libc::c_int;
| ^^^^ use of undeclared crate or module `libc`
error[E0433]: failed to resolve: use of undeclared crate or module `libc`
--> hello.rs:11:52
|
11 | printf(b"Hello, World!\x00" as *const u8 as *const libc::c_char);
| ^^^^ use of undeclared crate or module `libc`
warning: `#[no_mangle]` has no effect on a foreign function
--> hello.rs:6:1
|
6 | #[no_mangle]
| ^^^^^^^^^^^^ help: remove this attribute
7 | fn printf(_: *const libc::c_char, _: ...) -> libc::c_int;
| --------------------------------------------------------- foreign function
|
= note: `#[warn(unused_attributes)]` on by default
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: symbol names in extern blocks are not mangled
error: aborting due to 5 previous errors; 1 warning emitted
Some errors have detailed explanations: E0433, E0557.
For more information about an error, try `rustc --explain E0433`.
When using cargo it compiles and runs perfectly.
└─$ cargo run hello.rs
Compiling Rust_testing v0.1.0 (/home/pegasus/Documents/Rust_testing)
warning: crate `Rust_testing` should have a snake case name
|
= note: `#[warn(non_snake_case)]` on by default
= help: convert the identifier to snake case: `rust_testing`
warning: `Rust_testing` (bin "Rust_testing") generated 1 warning
Finished dev [unoptimized + debuginfo] target(s) in 0.17s
Running `target/debug/Rust_testing hello.rs`
Hello, world!
When removing the #[] this code runs in the playground.
here's the new.rs code:
extern "C" {
fn printf(_: *const libc::c_char, _: ...) -> libc::c_int;
}
unsafe fn main_0() {
// printf() displays the string inside quotation
printf(b"Hello, World!\x00" as *const u8 as *const libc::c_char);
}
pub fn main() { unsafe { main_0() } ::std::process::exit(0i32); }
But it still shows the three error[E0433]: failed to resolve: use of undeclared crate or module libc
.
Here's my Cargo.toml file:
[package]
name = "Rust_testing"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
libc = "0.2"
Upvotes: 0
Views: 846
Reputation: 22818
It doesn't work for me either, even with cargo run
. I'm unsure, maybe this requires an old version of nightly Rust?
Either way, if you remove the questionable code, it works without a problem:
extern "C" {
fn printf(_: *const libc::c_char, _: ...) -> libc::c_int;
}
unsafe fn main_0() {
// printf() displays the string inside quotation
printf(b"Hello, World!\n\x00" as *const u8 as *const libc::c_char);
}
pub fn main() {
unsafe { main_0() }
::std::process::exit(0i32);
}
Hello, World!
You mention that it doesn't work with rustc
directly. That is true, because rustc
is a compiler, it is not a dependency manager. You need to link the dependencies manually if you don't want to use cargo
. I highly recommend using cargo
, though.
If you run cargo run -vv
(of course run cargo clean
before, otherwise you won't see anything), you can see how cargo
adds the dependencies.
Upvotes: 1