laitimes

【Rust 小技巧】指定 target 输出目录

author:Not bald programmer
【Rust 小技巧】指定 target 输出目录

In the Rust programming language, the target directory is automatically generated by Cargo, Rust's package management and build tool. It is mainly used to store the product files of the build output and the intermediate files generated by the related compilation period. But the target directory tends to be very large, even a simple hello world program can be as large as 9 MB.

【Rust 小技巧】指定 target 输出目录

It could be a simpler project that would be more than 1 GB. Some netizens even joked that if the hard drive of the computer is too small, it may not be able to learn Rust.

0x01 指定 target 目录

Also, when there are too many Rust projects on your computer, you can't manage the target directory. In fact, we can specify a unified target directory for all projects. The following will take the Windows operating system as an example.

First, in the C:\Users\username\.cargo\ directory, locate the config.toml file. If you don't have a config.toml file in that directory, you'll need to create one. As shown in the figure below:

【Rust 小技巧】指定 target 输出目录

Note: The config file may also not have an extension, such as config, or other extensions, such as config.txt, and the extension of the configuration file is not important, but the file name must be config.

Add the following configuration information to the file:

[build]
target-dir = "D:/my-target"           

The configuration above is to specify that the uniform target directory for all items is D:/my-target. As shown below:

【Rust 小技巧】指定 target 输出目录

Let's compile the project again and look at the project root and D:/my-target directory, as shown below:

【Rust 小技巧】指定 target 输出目录

As you can see, there is no target directory in the project directory of the project, and the real target directory has been specified as the D:/my-target directory.

0x02 target 目录有什么

Here's a simple project's target directory, let's see what it contains.

【Rust 小技巧】指定 target 输出目录

The target directory contains the following subdirectories and files:

target/debug:

  • Stores executables and library files built in debug mode.
  • Contains intermediate compilation artifacts such as object files (.o files) and generated binaries.

target/release:

  • Stores executables and libraries built in release mode.
  • Compared to debug mode, release mode is optimized, and the resulting binaries are typically smaller and run faster.

target/doc:

  • Holds the project documentation (usually an HTML document generated by the cargo doc command).

target/.fingerprint:

  • Stores the fingerprint file of the build, which is used to keep track of which files have been compiled and whether they need to be recompiled.

target/package:

  • Holds the files that have been packaged for the project, typically used for the cargo publish command to publish to the crates.io repository.

Other temporary files and directories:

  • This includes build caches, generated assembly code, and more.

0x03 Specify the pros and cons of the Target catalog

There are both advantages and disadvantages to storing target files in one place:

merit

Cache Reuse:

If multiple projects use the same dependency library, you can reuse the compilation cache by specifying a shared target directory, reducing the time of repeated compilation, and greatly improving build efficiency.

Easy to manage:

By centralizing the compiled output to a specific directory, it is easier to manage and clean up the compiled output files. When there are too many Rust projects, we can always clean up the target file.

shortcoming

Multiple projects sharing the same target directory may cause conflicts due to different dependency versions, and you need to clean up the target directory in time when you encounter conflicts.

0x04 Summary

In general, specifying the target directory can provide significant build efficiency gains, but there are also potential version conflicts to consider, depending on the actual project.