Rustc does not compile the file and there are no error warnings

// main.rs

fn main() {
  println!("Hello, world!");
}

Below is my console information(hello_world project)

➜  hello_world $ rustc --version --verbose
rustc 1.68.0 (2c8cc3432 2023-03-06)
binary: rustc
commit-hash: 2c8cc343237b8f7d5a3c3703e3a87f2eb2c54a74
commit-date: 2023-03-06
host: x86_64-apple-darwin
release: 1.68.0
LLVM version: 15.0.6
➜  hello_world $ rustc ./main.rs          
➜  hello_world $ ll                       
total 8
-rw-r--r--  1 veath  staff    99B  3 21 17:05 main.rs

Why is the compiled main file not output?

Are you sure it isn't there? The total 8 in the output of ls suggests that there are at least 7 other files in the directory.

Look for "main" - that should be the name of the executable if the source is "main.rs" ...

1 Like

Yes, I recreated the folder and main file, and the total is also 8 without compiling

I try to create a project with cargo, and then execute cargo run, the following error occurs

hello_cargo git:(master) ✗ cargo run
   Compiling hello_cargo v0.1.0 (/Users/veath/information/learn-rust/hello_cargo)
    Finished dev [unoptimized + debuginfo] target(s) in 0.66s
     Running `target/debug/hello_cargo`
error: could not execute process `target/debug/hello_cargo` (never executed)

Caused by:
  No such file or directory (os error 2)

You shouldn't be mixing cargo commands with raw invocations of rustc. If you want a Cargo project, then use cargo build and cargo run. If you don't want a Cargo project, then you'll need to execute the resulting binary directly, eg. ./main. (Not recommended.)

3 Likes

Have you looked for the file elsewhere?

Perhaps antivirus quarantine?

Yes, I didn’t mix them. The hello_world project used rustc. I re-created another project hello_cargo with cargo new, and then run cargo build. It seems that no binary files were generated.

➜  hello_cargo git:(master) ✗ cargo build
   Compiling hello_cargo v0.1.0 (/Users/veath/information/learn-rust/hello_cargo)
    Finished dev [unoptimized + debuginfo] target(s) in 0.53s
➜  hello_cargo git:(master) ✗ cargo run 
   Compiling hello_cargo v0.1.0 (/Users/veath/information/learn-rust/hello_cargo)
    Finished dev [unoptimized + debuginfo] target(s) in 0.20s
     Running `target/debug/hello_cargo`
error: could not execute process `target/debug/hello_cargo` (never executed)

Caused by:
  No such file or directory (os error 2)

Does your machine have antivirus software running?

I installed CleanMyMac X, but I turned it off

What files are in target/debug?

@bjorn3

➜  hello_cargo git:(master) ✗ ll ./target/debug 
total 8
drwxr-xr-x  2 veath  staff    64B Mar 21 16:22 build
drwxr-xr-x  9 veath  staff   288B Mar 22 17:22 deps
drwxr-xr-x  2 veath  staff    64B Mar 21 16:22 examples
-rw-r--r--  1 veath  staff   134B Mar 21 16:22 hello_cargo.d
drwxr-xr-x  3 veath  staff    96B Mar 21 16:22 incremental

And target/debug/deps? Also have you tried cargo clean and then building again?

This is what was rebuilt

➜  hello_cargo git:(master) ✗ ll ./target/debug/deps 
total 80
-rw-r--r--  2 veath  staff   5.5K Mar 23 11:37 hello_cargo-851f06aa8bc56012.11m7cfnkck1i041r.rcgu.o
-rw-r--r--  2 veath  staff   6.2K Mar 23 11:37 hello_cargo-851f06aa8bc56012.221z7yflapk9ti2f.rcgu.o
-rw-r--r--  2 veath  staff   2.6K Mar 23 11:37 hello_cargo-851f06aa8bc56012.23e7sld1tkbc25tw.rcgu.o
-rw-r--r--  2 veath  staff   3.1K Mar 23 11:37 hello_cargo-851f06aa8bc56012.2e4o6nm80ezf3wvx.rcgu.o
-rw-r--r--  2 veath  staff   2.7K Mar 23 11:37 hello_cargo-851f06aa8bc56012.34yy354kqcdbiny0.rcgu.o
-rw-r--r--  2 veath  staff   4.8K Mar 23 11:37 hello_cargo-851f06aa8bc56012.4ln4v2odlqua7jl8.rcgu.o
-rw-r--r--  1 veath  staff   233B Mar 23 11:37 hello_cargo-851f06aa8bc56012.d

Maybe cargo build -vv gives some more details?

@bjorn3

➜  hello_cargo git:(master) ✗ cargo build -vv      
   Compiling hello_cargo v0.1.0 (/Users/veath/information/learn-rust/hello_cargo)
     Running `CARGO=/Users/veath/.rustup/toolchains/1.68.0-x86_64-apple-darwin/bin/cargo CARGO_BIN_NAME=hello_cargo CARGO_CRATE_NAME=hello_cargo CARGO_MANIFEST_DIR=/Users/veath/information/learn-rust/hello_cargo CARGO_PKG_AUTHORS='' CARGO_PKG_DESCRIPTION='' CARGO_PKG_HOMEPAGE='' CARGO_PKG_LICENSE='' CARGO_PKG_LICENSE_FILE='' CARGO_PKG_NAME=hello_cargo CARGO_PKG_REPOSITORY='' CARGO_PKG_RUST_VERSION='' CARGO_PKG_VERSION=0.1.0 CARGO_PKG_VERSION_MAJOR=0 CARGO_PKG_VERSION_MINOR=1 CARGO_PKG_VERSION_PATCH=0 CARGO_PKG_VERSION_PRE='' CARGO_PRIMARY_PACKAGE=1 DYLD_FALLBACK_LIBRARY_PATH='/Users/veath/information/learn-rust/hello_cargo/target/debug/deps:/Users/veath/.rustup/toolchains/1.68.0-x86_64-apple-darwin/lib:/Users/veath/.rustup/toolchains/1.68.0-x86_64-apple-darwin/lib:/Users/veath/lib:/usr/local/lib:/usr/lib' rustc --crate-name hello_cargo --edition=2021 src/main.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=228 --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C split-debuginfo=unpacked -C debuginfo=2 -C metadata=851f06aa8bc56012 -C extra-filename=-851f06aa8bc56012 --out-dir /Users/veath/information/learn-rust/hello_cargo/target/debug/deps -C incremental=/Users/veath/information/learn-rust/hello_cargo/target/debug/incremental -L dependency=/Users/veath/information/learn-rust/hello_cargo/target/debug/deps`
    Finished dev [unoptimized + debuginfo] target(s) in 0.71s

That commandline looks correct. Maybe check if the linker is correctly invoked? Something like RUSTFLAGS="-Clink-arg=--bogus-args" cargo build should work. It should return an error. Could you post the full error?

no error is generated

➜ hello_cargo git:(master) ✗ RUSTFLAGS="-Clink-arg=--bogus-args" cargo build
Compiling hello_cargo v0.1.0 (/Users/veath/information/learn-rust/hello_cargo)
Finished dev [unoptimized + debuginfo] target(s) in 0.50s
➜ hello_cargo git:(master) ✗

And RUSTFLAGS="-Clinker=/bin/false" cargo build to check if the linker is called in the first place?

Maybe try run cargo clean first and then try @bjorn3 's instructions?