编译c2rust生成的rust文件时出错.使用rustc



我有一个名为hello.rs.的rust程序

程序是我无法使用rustc编译它。我使用c2rust在线转发器生成了hello.rs。但如果我使用cargo运行hello.rs,程序运行得很顺利。

使用rustc时,会显示以下错误。如何修复?

hello.c原始文件:

void main() {
// printf() displays the string inside quotation
printf("Hello, World!");

}

hello.rs文件

#![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); }

错误消息:

┌──(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`.

当使用cargo时,它会完美地编译和运行。

└─$ 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!

删除#[]时,此代码在操场上运行。

这是新的.rs代码:

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); }

但它仍然显示了三个错误[E0433]:未能解决:使用未声明的机箱或模块libc

这是我的Cargo.toml文件:

[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"

即使使用cargo run,它也不适用于我。我不确定,也许这需要一个旧版本的夜间Rust?

无论哪种方式,如果你删除了有问题的代码,它都可以正常工作:

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!nx00" as *const u8 as *const libc::c_char);
}
pub fn main() {
unsafe { main_0() }
::std::process::exit(0i32);
}
Hello, World!

您提到它不能直接与rustc一起工作。的确如此,因为rustc是一个编译器,它不是一个依赖管理器。如果不想使用cargo,则需要手动链接依赖项。不过,我强烈建议使用cargo

如果您运行cargo run -vv(当然之前运行过cargo clean,否则您将看不到任何内容(,您可以看到cargo是如何添加依赖项的。

相关内容

最新更新