NobinPegasus
NobinPegasus

Reputation: 787

How to get the binary output of cargo run <rust.rs>?

When we compile a c file using gcc test.c -o test. We can get the binary file as test.

But while running a file using cargo run test.rs in rust. can we get the binary like we got in the C program?

The original hello.c file:

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

The rust program:

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

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!

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"

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.

while using rustc new.rs -o test, I can get the x86 test binary. How to get similar kind of file while using the cargo run new.rs?

I looked into the target/debug directory. But there are so many directories and so many files there. How to know which on is created for which .rs file?

┌──(pegasus㉿pegasus)-[~/Documents/Rust_testing/target/debug]
└─$ ls
build  deps  examples  incremental  Rust_testing  Rust_testing.d

Upvotes: 1

Views: 3412

Answers (1)

Finomnis
Finomnis

Reputation: 22601

If you do cargo build, you will find the binary in target/debug/. If you build in release via cargo build --release, you will find it in target/release/.

Be aware that cargo run hello.rs does not compile hello.rs. It will always compile src/main.rs. hello.rs will be passed to the compiled program as a command line argument.

How to know which on is created for which .rs file?

There isn't one file for one .rs file. If your crate is a binary crate, then there will be exactly one executable with the name of your crate. In your case it's Rust_testing. You can run it with ./target/debug/Rust_testing, or copy it somewhere else and execute it directly.

You can add multiple binaries per crate by putting them in the src/bin folder. For example, if you put your hello.rs file in src/bin and then execute cargo build --all, it will create a target/debug/hello executable that you can run.

For more information about cargo's folder layout, read the cargo documentation.

If you are new to Rust, I highly recommend reading the Rust book. It will guide you through how to use rustup, rustc and cargo step by step.

Upvotes: 3

Related Questions