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

Build example as lib and bin at the same time #5365

Closed
pronebird opened this issue Apr 16, 2018 · 5 comments
Closed

Build example as lib and bin at the same time #5365

pronebird opened this issue Apr 16, 2018 · 5 comments
Labels
A-examples Area: example targets C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` S-propose-close Status: A team member has nominated this for closing, pending further input from the team

Comments

@pronebird
Copy link

pronebird commented Apr 16, 2018

Hi,

I wonder if it's possible to build an example as a lib and binary at the same time. The reason for that is basically the fact that I have a windows only crate with windows only example.

Adding #![cfg(windows)] at the top of the source code works well for libraries, but fails to compile for binaries that need explicit main.

One of solutions is to wrap everything in example.rs into mod and guard it with proper #[cfg(windows)] however it leads to issues with external crates used in the example since dev deps are defined for windows only too [target.'cfg(windows)'.dev-dependencies]. So there is a lot of noise in the code if I take this approach.

The other approach is split the source code on two targets (lib + binary):

// lib.rs
#![cfg(windows)]
#[macro_use]
extern crate log;

pub fn run() {
  // the rest of code.
}
// main.rs
#[cfg(windows)]
extern crate mylib;

#[cfg(windows)]
fn main() {
    mylib::run();
}

#[cfg(not(windows))]
fn main() {
    panic!("This program is only intended to run on Windows.");
}

So I configured the example in my Cargo.toml to include two crate-types, however Cargo does not seem to understand that configuration. At least what I see during compilation is an error that the compiler can't find mylib crate.

[[example]]
name = "example"
crate-type = ["lib", "bin"]
@ehuss
Copy link
Contributor

ehuss commented Apr 16, 2018

See #3138 for a similar request (platform-specific examples).

@alexcrichton alexcrichton added the C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` label Apr 16, 2018
@azriel91
Copy link

Different use case, but similar symptom: I'd like to create examples that can be run both natively and as a WASM application. The WASM version requires the example to be built as a library, whereas natively it should be a binary.

Currently I've gone with just making the example a normal crate, and users have to enter the directory (or pass in the manifest path) to run it.

@ehuss ehuss added the A-examples Area: example targets label Mar 26, 2021
@dclong
Copy link

dclong commented Jun 16, 2021

Similar situation here.

@epage
Copy link
Contributor

epage commented Oct 19, 2023

For myself, I feel like #9208 is the right way to be going such that I'm going to propose to the cargo team that we close this in favor of that.

@epage epage added the S-propose-close Status: A team member has nominated this for closing, pending further input from the team label Oct 19, 2023
@weihanglo
Copy link
Member

Second. Closing in favor of #9208.

@weihanglo weihanglo closed this as not planned Won't fix, can't repro, duplicate, stale Nov 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-examples Area: example targets C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` S-propose-close Status: A team member has nominated this for closing, pending further input from the team
Projects
None yet
Development

No branches or pull requests

7 participants