Skip to content

Ability to specify the output name for a bin target different from the crate name #1706

Closed

Description

@crumblingstatue

As far as I know, the only way to name the output of a bin target is through the name attribute of a bin section.

This, however, sets both the crate name and the output name.

It seems too restricting that the output name of an executable should be a valid crate name.
For example, it cannot contain spaces. Cargo also warns if it's uppercase, because crate names are lowercase by convention.

Rustc with the -o FILENAME option allows to set any arbitrary filename for the output.

If there is already a way to do this with cargo, it should be better documented, as I couldn't find it.

Activity

  1. alexcrichton commented on Jun 11, 2015

    @alexcrichton
    Member

    I think this can be solved with cargo rustc (a relatively new subcommand), so I'm going to classify this as a docs bug.

  2. crumblingstatue commented on Jun 11, 2015

    @crumblingstatue
    Author

    How does cargo rustc solve this problem?

    What I'm thinking of is the ability to specify the output name of the executable in Cargo.toml, so everyone who compiles it gets that executable name.

  3. alexcrichton commented on Jun 11, 2015

    @alexcrichton
    Member

    Yes cargo rustc would not allow you to encode the value in Cargo.toml, cargo does not currently allow that level of configuration.

  4. ebkalderon commented on Jan 5, 2016

    @ebkalderon

    I would like to express interest in this feature as well.

  5. cutewalker commented on Jul 11, 2016

    @cutewalker

    yes, +1 for this feature

  6. VelocityRa commented on Jul 29, 2016

    @VelocityRa

    One whole year and still nothing...

  7. iddm commented on Nov 14, 2016

    @iddm

    +1 for this feature.

  8. Lakier15 commented on Feb 1, 2017

    @Lakier15

    +1 for this feature

  9. Boscop commented on Apr 9, 2018

    @Boscop

    Yes, please :)

  10. matklad commented on May 6, 2018

    @matklad
    Contributor

    @brettcannon binary can have a name different from the name of he package, you can see it in Cargo toml in a [[bin]] section with the name key

  11. brettcannon commented on May 6, 2018

    @brettcannon

    @matklad yep, and I deleted my comment as it was incorrect; sorry about the noise; misunderstanding on my part when looking for a solution for my problem. (And feel free to hide this comment.)

  12. tzaeru commented on May 16, 2018

    @tzaeru

    +1.

    I wouldn't want to change my pretty

    [[bin]]
    name = "server"
    path = "src/server/main.rs"
    

    to

    [[bin]]
    name = "SoundAnalyzerServer.exe"
    path = "src/server/main.rs"
    
  13. 20 remaining items

  14. dcow commented on Feb 2, 2021

    @dcow

    Would love to see the ability to specify the name of the inferred main.rs binary target. Just a note, you can:

    mkdir src/bin
    mv src/main.rs src/bin/foo.rs
    

    and the main bin will not longer be auto-discovered and cargo run will run the sole foo bin target without any other config. Would be nice to be able to do something like:

    [bin.main]
    name = "foo"
    

    for aesthetics.

  15. dragonmaus commented on Feb 2, 2021

    @dragonmaus

    While I agree that it would be nice for aesthetics and/or convenience, personally I want this feature so that I can produce executables that have names not allowed by the current scheme (e.g. en% and de%).

  16. dcow commented on Feb 2, 2021

    @dcow

    Just tested and you can also:

    [[bin]]
    name = "foo"
    path = "src/main.rs"
    

    The name applies to the auto-discovered main target (no duplicate binary target is created).

  17. kotx commented on Feb 14, 2021

    @kotx

    Just tested and you can also:

    [[bin]]
    name = "foo"
    path = "src/main.rs"
    

    The name applies to the auto-discovered main target (no duplicate binary target is created).

    Unfortunately if the name isn't in snake case (or kebab case) you'll get a warning (crate `X` should have a snake case name), but I want to name my executable file in pascal case. Hoping for a workaround other than disabling the warning or changing the name.

  18. SoniEx2 commented on Feb 21, 2021

    @SoniEx2

    We used to have application binaries called foo.cli and foo.gui and unfortunately we can't switch to rust because this issue breaks backwards compatibility. All in all it's kinda silly that applications have a crate name tho, it's not like that's ever gonna be exposed anywhere, and it's not like there are macros to get the crate name at compile-time or anything.

    Why not just set the crate name the same for all applications, and/or remove the concept of crate name from rust applications?

  19. Wafelack commented on Feb 23, 2021

    @Wafelack

    You can actually get the crate name at compile time. And crate names for application aren't « silly » as you say, they are necessary for crates.io (for both installation and searching.).

  20. mainrs commented on Mar 7, 2021

    @mainrs

    Unfortunately if the name isn't in snake case you'll get a warning (crate `X` should have a snake case name), but I want to name my executable file in pascal case. Hoping for a workaround other than disabling the warning or changing the name.

    This isn't true (entirely), I have the following and I do not get any errors when calling cargo run:

    [package]
    name = "krate_name"
    
    [[bin]]
    name = "krate-name"
    path = "src/bin/krate_name.rs"

    It seems to also support kebab-case.

  21. kotx commented on Mar 7, 2021

    @kotx

    Unfortunately if the name isn't in snake case you'll get a warning (crate `X` should have a snake case name), but I want to name my executable file in pascal case. Hoping for a workaround other than disabling the warning or changing the name.

    This isn't true (entirely), I have the following and I do not get any errors when calling cargo run:

    [package]
    name = "krate_name"
    
    [[bin]]
    name = "krate-name"
    path = "src/bin/krate_name.rs"

    It seems to also support kebab-case.

    Yeah I forgot about that, edited. The warning explicitly says snake case, so I guess that's an oversight. Snake case is preferred over kebab case IIRC.

  22. whereistejas commented on Jun 26, 2021

    @whereistejas
    Contributor

    Hi everyone,
    If possible I would like to take up this issue. Based on the discussion above, we can summarise the requirements as follows:

    1. output name of the binary should not have the restrictions of a crate name.
    2. add a second key in [[bin]] section along with name called filename.
      1. name will allow us to set the name of the crate.
      2. filename will allow us to set the name of the binary.
    3. only allow this config options in [[bin]] and [[example]].

    @alexcrichton @ehuss please correct me if I'm wrong about the above requirements.

    @rustbot claim

  23. added a commit that references this issue on Aug 11, 2021
  24. ehuss commented on Aug 11, 2021

    @ehuss
    Contributor

    Closing as this is now implemented via #9627, and stabilization tracked in #9778.

  25. eureka-cpu commented on May 16, 2024

    @eureka-cpu

    We'll probably want to only allow this on [[bin]] and [[example]] targets to start off. Configuring the file stem of a [lib] target should likely be an error

    I need this feature for a [lib]. My use case: I write plugins for other software in Rust, which are technically shared libraries (crate-type = ["dylib"]: .so files, as I'm workig on Linux), but should be named something.plugin instead of libsomething.so.

    Such a good suggestion. I'm writing a plugin atm and I have a post-install script that renames the resulting "lib", since it's prefixed with lib and there doesn't seem to be a direct way of telling cargo to just use the name field. Strange that it does this by default.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-cargo-targetsArea: selection and definition of targets (lib, bins, examples, tests, benches)A-manifestArea: Cargo.toml issuesE-mediumExperience: Medium

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions