官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 尤溪县| 南开区| 聊城市| 黄山市| 剑河县| 西乌珠穆沁旗| 霍林郭勒市| 阿巴嘎旗| 千阳县| 定襄县| 松阳县| 克什克腾旗| 正阳县| 鄂托克旗| 四平市| 万州区| 手机| 卓资县| 惠水县| 桓仁| 河北省| 海兴县| 博客| 仁怀市| 富锦市| 兰溪市| 固始县| 岑巩县| 巍山| 绥芬河市| 桐城市| 北流市| 龙里县| 博野县| 永定县| 黎川县| 乐亭县| 玉门市| 交城县| 砀山县| 永州市|