You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C2Rust转换的Rust文件无法用rustc编译的解决方法咨询

C2Rust生成的Rust程序rustc编译失败解决办法

问题背景

我用C2Rust在线转换器将一个简单的C程序转成了hello.rs,用cargo run可以正常运行,但直接用rustc编译时出现一堆错误。

原始C代码(hello.c)

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

C2Rust生成的Rust代码(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); }

rustc编译错误信息

┌──(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!

尝试修改后仍报错的代码(new.rs)

移除了过时特性和无效属性后,还是报libc未声明的错误:

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

我的Cargo.toml配置

[package]
name = "Rust_testing"
version = "0.1.0"
edition = "2021"

[dependencies]
libc = "0.2"

错误原因

  1. 过时特性:#![feature(main, register_tool)]中的main特性已经被Rust官方移除,导致编译报错。
  2. 无效属性:#[main]不是合法的Rust属性,Rust的入口函数默认就是fn main(),不需要额外标记。
  3. 依赖未链接:直接用rustc编译时,不会自动读取Cargo.toml中的依赖配置,所以找不到libc crate。
  4. 多余属性:#[no_mangle]对外联函数无效,因为extern块中的函数符号不会被Rust mangling,这个属性是多余的。

解决步骤

步骤1:修正Rust代码

修改后的完整代码如下:

#![allow(dead_code, mutable_transmutes, non_camel_case_types, non_snake_case,
         non_upper_case_globals, unused_assignments, unused_mut)]
#![register_tool(c2rust)]

// 显式引入libc crate
extern crate libc;

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

步骤2:用rustc编译时链接libc依赖

方法一:通过--extern参数指定libc的路径(先执行cargo build让Cargo下载好依赖)

rustc hello.rs --extern libc=./target/debug/deps/liblibc-*.rlib -o test2

注意:替换liblibc-*.rlib为实际生成的文件名,比如liblibc-abc123.rlib

方法二:用cargo rustc代替直接rustc,自动处理依赖

cargo rustc --bin Rust_testing -- -o test2

步骤3:验证运行

编译完成后执行生成的二进制文件:

./test2

会输出Hello, World!

内容的提问来源于stack exchange,提问作者NobinPegasus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 20:40:48