How to disable lints for the "bin" target only [duplicate] - rust

This question already has answers here:
How to disable unused code warnings in Rust?
(11 answers)
Closed 3 years ago.
I have a Rust project folder structure that contains an executable and a shared C-compatible library that are both build using the same sources. The Cargo.toml manifest file looks like:
[package]
name = "foo-bar"
version = "0.1.0"
authors = ...
[lib]
name = "foo_bar"
crate-type = ["rlib", "cdylib"]
[[bin]]
name = "foo-bar"
test = false
doc = false
[dependencies]
...
As the executable is not using all of the code I get some "unused code" warnings when building the project with cargo build. I could add #[allow(dead_code)] lints all over my source code where necessary but that would disable them also when building the library target.
Is there a way to globally disable the "dead_code" lint only when compiling the (feature-wise smaller) bin executable target but having it enabled for the lib target?

You can modify a lint for a whole crate by putting an attribute with #! at the beginning of the crate:
main.rs:
#![allow(dead_code)]
// etc.

Related

Empty rust file creates massive WASM build

Building the following rust file is producing a binary of 720KB.
I would expect a virtually empty build, what am I missing here? Is the full core libary getting included somehow?
Here's the code
#![no_std]
#[panic_handler]
fn handle_panic(_: &core::panic::PanicInfo) -> ! {
unreachable!()
}
And the cargo.toml
[package]
name = "wasm_test"
version = "0.0.0"
edition = "2021"
[lib]
crate-type = ["cdylib"]
[profile.release]
opt-level = 's'
lto = true
And the command I'm using to build:
cargo build --target wasm32-unknown-unknown
Ok turns out I had two problems:
I was building this as a crate in a workspace, and that apparently ignores crate specific profiles
When i copied the crate out to mess around with it and isolate the problem, i was forgetting to add the --release flag, thanks #isaactfa.
In other cases i have received this warning.
warning: profiles for the non root package will be ignored, specify profiles at the workspace root:
for some reason I wasn't getting it when doing the workspace builds.
I've added the crate to the workspace exclude list and am building it seperately, and its compiling to a far more appropriate 411 bytes, down from 727151 bytes.

Is using main.rs as lib entry point unidiomatic?

I'm currently experimenting with wasm, which has to be compiled as cdylib. I don't want to maintain two entry files for the bin target and for the lib target, so I added these lines to my Cargo.toml:
[lib]
name = "sandbox"
path = "src/main.rs"
crate-type = ["cdylib"]
My fn main() has now this attribute:
#[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
Everything works as expected, but cargo warns me about this:
warning: file found to be present in multiple build targets
Can this warning safely be ignored? Why?
If yes, is it possible to suppress it?

How to link main.rs to lib.rs dynamically?

I have a crate with both src/lib.rs and src/main.rs.
main.rs is just using extern crate programname (which is lib.rs) and uses certain functions from lib.rs and it's submodules.
The documentation on linking says:
Pure-Rust dependencies are statically linked by default so you can use created binaries and libraries without installing Rust everywhere.
How can I change this behavior so a binary created from main.rs will be dynamically linked to library produced by lib.rs?
I've added the following to Cargo.toml
[lib]
path = "src/lib.rs"
crate-type = ["dylib"]
[[bin]]
name = "programname"
path = "src/main.rs"
But it does not compile and gives me errors like:
error: cannot satisfy dependencies so `std` only shows up once
help: having upstream crates all available in one format will likely make this go away
If I add "rlib" to lib section, it compiles, but the binary is not linked against libprogramname.so

Build only `lib` target

I want to build a dynamic link library (dll).
My Cargo.toml currently looks like this:
[package]
name = "sample"
version = "0.1.0"
authors = ["author"]
[lib]
name = "main"
crate-type = ["dylib"]
[dependencies]
I use VS Code with the RustyCode plugin as my IDE on windows.
When I run the build command this builds into a sample.exe and main.dll.
I know I can run cargo build --lib to only build my lib target but I dont have access to this command inside VS Code (afaik).
Is there anyway to specify that I only want to build the lib target in my Cargo.toml file so I can use the VS Code build command which runs cargo build/cargo run?
Cargo builds files using convention over configuration approach. When it finds a main.rs it builds an executable, and when it encounters lib.rs it expects to build a library.
Calling your lib main managed to confuse Cargo. The only solution I managed to find is to either change name of your crate from name = "main" to name = "foo" (and then rename your main.rs into foo.rs) or to change its name to lib.rs, as you did.
Just figured it: Rename the src/main.rs to src/lib.rs and it only builds the lib target!

Creating a library and executable in a single project [duplicate]

This question already has answers here:
Package with both a library and a binary?
(4 answers)
Closed 7 years ago.
How do I create a library and executable in one project? I just want to test my library while working on it and using tests isn't always the best way to do that. I believe I have to use [lib] and [???] but I haven't found the information about that at crates.io.
Indeed, it's strange that crates.io does not have a clear example of this.
To add both a library and an executable to your crate (BTW, the crate can only have one library in it), you need to defined them in [lib] and [[bin]] sections:
[lib]
name = "yourcrate"
[[bin]]
name = "yourcrate_bin_1"
[[bin]]
name = "yourcrate_bin_2"
With the above by default Cargo will look for the library crate root in src/lib.rs and for binaries in src/bin/yourcrate_bin_1.rs and src/bin/yourcrate_bin_2.rs. You can change paths to the crate root files with path option:
[[bin]]
name = "yourcrate_bin_2"
path = "src/yourcrate_bin_2.rs"

Resources