Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't compile c2rust-generated program using rustc #661

Closed
NobinPegasus opened this issue Sep 13, 2022 · 10 comments
Closed

Can't compile c2rust-generated program using rustc #661

NobinPegasus opened this issue Sep 13, 2022 · 10 comments

Comments

@NobinPegasus
Copy link

NobinPegasus commented Sep 13, 2022

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`.
@kkysen
Copy link
Contributor

kkysen commented Sep 13, 2022

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.

@NobinPegasus
Copy link
Author

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.

@NobinPegasus
Copy link
Author

NobinPegasus commented Sep 13, 2022

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

@kkysen
Copy link
Contributor

kkysen commented Sep 13, 2022

c2rust still generating #[main] is a bug, so I'll file an issue for that. It was removed in rust-lang/rust#29634 in 2017.

@kkysen
Copy link
Contributor

kkysen commented Sep 13, 2022

We should also stop emitting the #[no_mangle]s, so I'll file an issue for that, too.

@kkysen
Copy link
Contributor

kkysen commented Sep 13, 2022

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.

@Nobody1707
Copy link

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.

@kkysen
Copy link
Contributor

kkysen commented Sep 25, 2022

@kkysen
Copy link
Contributor

kkysen commented Sep 25, 2022

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.

@kkysen kkysen changed the title Can't compile c2rust generated program using rustc Can't compile c2rust generated program using rustc Sep 25, 2022
@kkysen kkysen changed the title Can't compile c2rust generated program using rustc Can't compile c2rust-generated program using rustc Sep 25, 2022
@kkysen
Copy link
Contributor

kkysen commented Sep 30, 2022

I filed issues for the two bugs here:

Closing in favor of these more specific issues.

@kkysen kkysen closed this as completed Sep 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants