Skip to content

cargo rustc -- --emit=obj ignores flag #15771

@LightVillet

Description

@LightVillet

Problem

I am trying to stop build on linking phase so that I can provide required libraries later. When I execute rustc --emit=obj src/main.rs it works fine, however with cargo rustc --emit=obj it fails:

error: linking with `cc` failed: exit status: 1

With output of cargo --verbose -- --emit=obj I see a command being executed with multiple emit flags:

Running `.../rustc ... --emit=dep-info,link ... --emit=obj`

Steps

  1. create new project with cargo init
  2. add this to src/main.rs:
unsafe extern "C" {
    safe fn add(x: i32) -> i32;
}

fn main() {
    println!("5 + 1 according to C: {}", add(5));
}
  1. Try to build with cargo rustc -- --emit=obj
  2. It will fail with linking
  3. Try to build with the output of cargo --verbose rustc -- --emit=obj and remove cargo's emit
  4. It will work fine

Possible Solution(s)

Executing the same command with only one --emit=obj works fine too, so possibly this issue is related to #14346.

Notes

Maybe there are another ways to stop build on linking phase, all I found is cargo rustc -- --emit=obj (which doesn't work) and nightly cargo rustc -Zunstable-options -- -Zno-link. If I am missing some other variants to do it on stable version, let me know please.

Version

cargo 1.88.0 (873a06493 2025-05-10)
release: 1.88.0
commit-hash: 873a0649350c486caf67be772828a4f36bb4734c
commit-date: 2025-05-10
host: x86_64-unknown-linux-gnu
libgit2: 1.9.0 (sys:0.20.0 vendored)
libcurl: 8.12.1-DEV (sys:0.4.80+curl-8.12.1 vendored ssl:OpenSSL/3.4.1)
ssl: OpenSSL 3.4.1 11 Feb 2025
os: Fedora 41.0.0 (FortyOne) [64-bit]

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-rustflagsArea: rustflagsC-bugCategory: bugCommand-rustcS-needs-infoStatus: Needs more info, such as a reproduction or more background for a feature request.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions