mq7
mq7

Reputation: 1279

How can I specify a custom Cargo output directory?

I put this in my Cargo.toml

[build]
target-dir = "../my-target"

However, Cargo doesn't recognize this key.

cargo run --release --bin my_project

warning: unused manifest key: build
error: failed to open: /.../project-root/target/releases/.cargo-lock

Caused by:
  Permission denied (os error 13)

The custom target dir with the environment variable works:

CARGO_TARGET_DIR=../my-target cargo run --bin my_project

but how can I specify '../my-target' in Cargo.toml?

Upvotes: 36

Views: 35736

Answers (3)

sdht0
sdht0

Reputation: 560

[build] is a Cargo-level configuration rather than for the project:

This document will explain how Cargo’s configuration system works, as well as available keys or configuration. For configuration of a project through its manifest, see the manifest format.

Put your [build] inside $PROJECT_DIR/.cargo/config.toml or even $HOME/.cargo/config.toml. See the above link for all the options.

Upvotes: 32

Jonathan Feenstra
Jonathan Feenstra

Reputation: 2779

Besides setting the CARGO_TARGET_DIR environment variable or the build.target-dir setting in /.cargo/config.toml, Cargo also has the --target-dir CLI option for specifying the target directory.

Upvotes: 10

Shepmaster
Shepmaster

Reputation: 430791

Use the CARGO_TARGET_DIR environment variable:

CARGO_TARGET_DIR=../my-target cargo run --bin my_project

(This is stated in the question, but I wanted to highlight it for anyone that skips over that)

Upvotes: 34

Related Questions