c2rust
c2rust copied to clipboard
Can't compile c2rust generated program using rustc
Here's my C code:
#include <stdio.h>
void main() {
// printf() displays the string inside quotation
printf("Hello, World!");
}
Here's the rust code generated by c2rust:
#![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); }
I'm unable to compile the generated code using rustc.
When tried to compile it gives the following error message:
┌──(pegasus㉿pegasus)-[~/Documents/Rust_testing]
└─$ rustc output.rs -o test2
error[E0557]: feature has been removed
--> output.rs:4:12
|
4 | #![feature(main, register_tool)]
| ^^^^ feature has been removed
error: cannot find attribute `main` in this scope
--> output.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`
--> output.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`
--> output.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`
--> output.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
--> output.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`.
That C code doesn't compile either, as it declares void main()
instead of int main()
.
Also, the reason libc
is not resolved is because you're using rustc
directly and not passing the right args necessary for dependencies. Try using cargo
.
I'm not quite sure about the #![feature(main)]
error. I'm guessing the main
feature has since been removed or renamed. I think what c2rust
is trying to do here is support non-standard main
functions.
The C code compiles fine using gcc compiler.
The libc is resolved by putting libc = "0.2" in Cargo.toml.
Then putting all the .rs files in src/bin and using cargo build --all
in the project directory gives the same effect as the gcc gives in case of C creating individual binary files for each C file.
It puts individual binaries for each .rs file into target/debug directory.
also even when using cargo it throws whole bunch of errors. all the #[ ] needs to be removed to successfully build the files.
Here's the C file:
void main() {
// printf() displays the string inside quotation
printf("Hello, World!");
}
Here's the converted rust file by c2rust:
#![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); }
Here's the complete set of errors.
┌──(pegasus㉿pegasus)-[~/Documents/Rust_testing]
└─$ cargo build --all
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
Compiling Rust_testing v0.1.0 (/home/pegasus/Documents/Rust_testing)
error[E0557]: feature has been removed
--> src/bin/converted_hello.rs:4:12
|
4 | #![feature(main, register_tool)]
| ^^^^ feature has been removed
error: cannot find attribute `main` in this scope
--> src/bin/converted_hello.rs:13:3
|
13 | #[main]
| ^^^^
|
= note: `main` is in scope, but it is a function, not an attribute
warning: `#[no_mangle]` has no effect on a foreign function
--> src/bin/converted_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
For more information about this error, try `rustc --explain E0557`.
warning: `Rust_testing` (bin "converted_hello") generated 1 warning
error: could not compile `Rust_testing` due to 2 previous errors; 1 warning emitted
c2rust
still generating #[main]
is a bug, so I'll file an issue for that. It was removed in https://github.com/rust-lang/rust/issues/29634 in 2017.
We should also stop emitting the #[no_mangle]
s, so I'll file an issue for that, too.
It seems what c2rust
is generating doesn't even need #[main]
, so we don't need to do anything else besides remove that feature being used to fix this. That being said, void main()
I'm pretty sure is not valid C, even if gcc
accepts it.
It would not be valid C++, but in C main is allowed to have an implementation defined signature that returns void. So it's merely non-portable C.
I filed issues for the two bugs here:
- #678
- #679
It would not be valid C++, but in C main is allowed to have an implementation defined signature that returns void. So it's merely non-portable C.
Ah, I didn't realize that. Are there use cases for this that are in significant usage? void
return types would be trivial to implement, but I have no idea how we would transpile any other main
function.
I filed issues for the two bugs here:
Closing in favor of these more specific issues.