- Hands-On Data Structures and Algorithms with Rust
- Claus Matzinger
- 293字
- 2021-07-02 14:11:47
Static and dynamic libraries
Generally, Rust dependencies have two types of linking:
- Static: Via the rlib format.
- Dynamic: Via shared libraries (.so or .dll).
The preference—if a corresponding rlib can be found—is to link statically and therefore include all dependencies into the output binary, making the file a lot larger (to the dismay of embedded programmers). Therefore, if multiple Rust programs use the same dependency, each comes with its own built-in version. It's all about the context though, since, as Go's success has shown, static linking can simplify complex deployments since only a single file has to be rolled out.
There are drawbacks to the static linking approach beyond size: for static libraries, all dependencies have to be of the rlib type, which is Rust's native package format, and cannot contain a dynamic library since the formats (for example, .so (dynamic) and .a (static) on ELF systems) aren't convertible.
For Rust, dynamic linking is commonly used for native dependencies, since they are usually available in the operating system and don't need to be included in the package. The Rust compiler can favor this with a -C prefer-dynamic flag, which will get the compiler to look for the corresponding dynamic libraries first.
Therein lies the current strategy of the compiler: depending on the output format (--crate-format= rlib, dylib, staticlib, library, or bin), it decides on the best linking type with your influence via flags. However, there is a rule that the output cannot statically link the same library twice, so it won't link two libraries with the same static dependency.
For more information on the topic, we recommend checking out https://doc.rust-lang.org/reference/linkage.html. That said, the compiler is usually trustworthy and, unless there is an interoperability goal, rustc will decide optimally.
- 在你身邊為你設(shè)計(jì)Ⅲ:騰訊服務(wù)設(shè)計(jì)思維與實(shí)戰(zhàn)
- 有趣的二進(jìn)制:軟件安全與逆向分析
- 云計(jì)算服務(wù)保障體系
- Learn Unity ML-Agents:Fundamentals of Unity Machine Learning
- 大話Oracle Grid:云時(shí)代的RAC
- 數(shù)據(jù)庫技術(shù)及應(yīng)用教程
- 從0到1:JavaScript 快速上手
- Python金融數(shù)據(jù)分析(原書第2版)
- Oracle PL/SQL實(shí)例精解(原書第5版)
- 大數(shù)據(jù)分析:數(shù)據(jù)倉庫項(xiàng)目實(shí)戰(zhàn)
- Web Services Testing with soapUI
- 數(shù)據(jù)庫查詢優(yōu)化器的藝術(shù):原理解析與SQL性能優(yōu)化
- 實(shí)現(xiàn)領(lǐng)域驅(qū)動(dòng)設(shè)計(jì)
- 數(shù)據(jù)指標(biāo)體系:構(gòu)建方法與應(yīng)用實(shí)踐
- 智能與數(shù)據(jù)重構(gòu)世界