Build only `lib` target - rust

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!

Related

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

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.

How do you include all crates in a workspace inside the test directory?

I have a binary Rust project which uses the workspaces to manage sub-crates.
Directory structure
/myapp
Cargo.toml
/src
/tests
test.rs
/crates
/printer
Cargo.toml
/src
myapp/Cargo.toml
[package]
name = "myapp"
[workspace]
members = ["crates/printer"]
Inside of test.rs I can compile extern crate myapp; to pull in the parts of the application that are exposed in src/lib.rs. This works as expected.
However, when attempting to compile extern crate printer; it errors that it cannot find it. I've confirmed that the printer package is correctly placed in the top-level Cargo.lock file.
How do I include my sub-crates into the /tests directory at the top level?
There's nothing special about workspaces or even the concept of tests. If you want to use a crate in Rust code, you have to add it as a dependency:
[dependencies]
printer = { path = "crates/printer" }
See also:
How to define test-only dependencies?

How to build or run a specific Rust binary project in Atom?

I am using multiple main files using a single Cargo.toml:
[package]
name = "rust_example"
version = "0.1.0"
authors = [""]
[dependencies]
[[bin]]
name = "main"
path = "src/main.rs"
[[bin]]
name = "fibonachi"
path = "src/fibonachi/main.rs"
I tested on console
cargo run --bin main` and `cargo run --bin fibonachi
And it compiles correctly.
I installed Atom along with several plugins (build, build-cargo, busy, language-rust, linter). When I compile with Atom, it shows these lines:
error: `cargo run` requires that a project only have one executable; use the `--bin` option to specify which one to run
error: `cargo run` requires that a project only have one executable; use the `--bin` option to specify which one to run
I can't find how to specify which binary I'd like to build from within Atom.

failed to parse manifest - no targets specified

I am new to Rust and attempting to build a test project with Cargo. My Cargo.toml looks like:
[package]
name = "rust-play"
version = "0.0.1"
authors = [ "Bradley Wogsland <omitted>" ]
(but the actual TOML file doesn't omit my email). When I cargo build I am getting the following error:
error: failed to parse manifest at /Users/wogsland/Projects/rust-play/Cargo.toml
Caused by:
no targets specified in the manifest
either src/lib.rs, src/main.rs, a [lib] section, or [[bin]] section must be present
My main function is in a src/test.rs file. Do I need to specify that in the TOML file? If so, how? I tried adding
target = "src/test.rs"
to no avail.
As the error says:
either src/lib.rs, src/main.rs, a [lib] section, or [[bin]] section must be present
So the direct answer is to add a [[bin]] section:
[[bin]]
name = "test"
path = "src/test.rs"
However, it's far more usual to just place the file in the expected location: src/main.rs. You could also place it in src/bin/test.rs if you plan on having multiple binaries.
If it's actually for testing your code, then unit tests go in the same file as the code they are testing and integration tests go in tests/foo.rs.
Alternative issue and solution: You can also be faced with this error if you have copied Cargo.toml file to a parent folder of the crate.
I ran into this issue on Ubuntu 20.04 after having inadvertently copied Cargo.toml to my home folder. Even though my working directory had a properly defined Cargo.toml, the copy in $HOME was taking precedence and causing builds to fail.
In my case and probably in your case as well, the rs file was not named main.rs while Cargo assumes that src/main.rs is the crate root of a binary crate. So, the rule is that If project is an executable, name the main source file src/main.rs. If it is a library, name the main source file src/lib.rs.
Additionally, Cargo will also treat any files located in src/bin/*.rs as executables like mentioned in the previous answer.
As a summary:
If you use cargo new xxx --bin, you will find the file in the src directory is named main.rs. And when you check the file Cargo.toml. It is the same as you written. So the first way is to change the file in src to main.rs
As the cargo report, we can use the [[bin]] to set the file. #Shepmaster has solved it.
Both two ways can work.
I also had this issue and it was because the parent directory also contained a Cargo.toml file and it was prioritising that over the one in the current directory

capnpc::compile not writing files

I'm having difficulty working with the capnpc crate. I'm running Arch Linux and have installed capnp from the AUR and compiled capnpc-rust from the github project and put it in /usr/local/bin. I can manually compile the .capnp file easily with the command
capnp compile -orust --src-prefix=capnp capnp/message.capnp
I tried cloning the capnpc project and compiling the .capnp test file in the test directory and that didn't work either. I'm not getting any errors (whereas earlier I was getting "File not found") so it seems like capnpc is working, but I can't find files anywhere.
build.rs
extern crate capnpc;
fn main() {
::capnpc::compile("capnp", &["capnp/message.capnp"]).unwrap();
}
Cargo.toml
...
build = "build.rs"
[lib]
name = "rustp2p"
path = "src/lib.rs"
[build-dependencies]
capnpc = "*"
[dependencies]
capnp = "0.5.0"
Edit: .rs file builds out into /target/debug/build/.../out.
When you invoke capnpc::compile from a Cargo build script, the generated code goes in a subdirectory of target/ that can be found at main compile time through the OUT_DIR environment variable. This strategy is described in the Cargo documentation.
You shouldn't need to install the capnpc-rust binary in /usr/local/bin or anywhere else for this to work.
Your build.rs and Cargo.toml files look fine to me.
You might find it helpful to consult the addressbook example.

Resources