Deploy Cargo binaries to directory like CMake Install

I work in embedded devices, and my team uses C/C++ exclusively along with CMake to manage dependencies and build executables/libraries. Our normal development flow is to use CMake to build our code and install the generated executables/libraries into a directory on our hardware so that our intermediate build files/code are separate from our executables/libraries since the latter are what's actually deployed to the hardware.

I'm trying to use Rust for development, but it seems like Cargo doesn't quite have the same "install" functionality as CMake in that I can separate the Rust build files from the final binary. It seems like Rust mixes the incremental build files with the final binaries.

Is there a way to specify to Cargo/rustc to "install" the final binaries similar to CMake's install functionality?

You can use cargo install for something like this because it lets you specify the directory to install things into (e.g. cargo install --path ./path/to/your/crate --root /tmp/). This is the preferred way to install a Rust executable during development.

The cargo build command also has an unstable --out-dir flag which tells the compiler to copy the final artefacts to a particular directory after they are built. That should make it easier if you are wanting to collect the final binaries into a dist/ directory for packaging (e.g. cargo build --out-dir dist -Z unstable-options). The benefit of this over cargo install is it will copy across libraries (i.e. crate-type is cdylib or staticlib), whereas cargo install is mainly intended for executables.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.