英文:
Why does the Windows crate contain compiled static libraries?
问题
以下是翻译好的部分:
"The windows crate, which provides Rust bindings to the Windows API, is split into a few internal crates, one of them being the windows-targets. This create is also split in a few internal crates, and these are loaded according to the target platform."
这个句子描述了提供 Rust 绑定到 Windows API 的 windows crate,它被分为几个内部 crate 之一是 windows-targets。windows-targets 也分为一些内部 crate,根据目标平台进行加载。
"These platform-dependent crates don't seem to do much: they simply pass cargo:rustc-link-search=native={LIB_DIR}
to the compiler."
这些依赖于平台的 crate 似乎并没有做太多事情:它们只是将 cargo:rustc-link-search=native={LIB_DIR}
传递给编译器。
"Now looking at these LIB_DIR
directories, they contain an already-compiled static library, one for each platform. Example: windows.0.48.0.lib file."
现在查看这些 LIB_DIR 目录,它们包含了一个已经编译好的静态库,每个平台一个。例如:windows.0.48.0.lib 文件。
"I couldn't find any documentation regarding this, but I suppose these static libraries are compiled from the C source files found in the baseline
directory, which contain... a duplication of the Windows API function declarations in C. That's intriguing."
我找不到关于这一点的任何文档,但我猜想这些静态库是从在 baseline 目录中找到的 C 源文件编译而来的,这些文件中包含了 Windows API 函数声明的重复。这很有趣。
"The Rust bindings themselves are written somewhere else, like here (GDI bindings)."
Rust 绑定本身是在其他地方编写的,比如在这里(GDI 绑定)。
Question: these compiled C static libraries seem to be a duplication of the API already available in Windows, so why are they necessary?
问题: 这些编译的 C 静态库似乎是对已经在 Windows 中可用的 API 的重复,那么它们为什么是必要的?
英文:
The windows crate, which provides Rust bindings to the Windows API, is split into a few internal crates, one of them being the windows-targets. This create is also split in a few internal crates, and these are loaded according to the target platform.
These platform-dependent crates don't seem to do much: they simply pass cargo:rustc-link-search=native={LIB_DIR}
to the compiler.
Now looking at these LIB_DIR
directories, they contain an already-compiled static library, one for each platform. Example: windows.0.48.0.lib file.
I couldn't find any documentation regarding this, but I suppose these static libraries are compiled from the C source files found in the baseline
directory, which contain... a duplication of the Windows API function declarations in C. That's intriguing.
The Rust bindings themselves are written somewhere else, like here (GDI bindings).
Question: these compiled C static libraries seem to be a duplication of the API already available in Windows, so why are they necessary?
答案1
得分: 1
我通过直接询问官方Windows Crate维护者获得了回应:
虽然GNU和MSVC工具链通常提供一些导入库以支持C++开发,但这些库文件经常不完整、缺失或者只是错误的。这可能导致非常难以诊断的链接器错误。
windows-targets
Crate 确保可以链接由windows
和windows-sys
Crates 定义的所有函数,而无需依赖工具链分发的隐式库文件。
如有兴趣,更多详细信息可以在这里查看。
英文:
I got a response by asking directly the official windows crate maintainers:
> While the GNU and MSVC toolchains often provide some import libs to support C++ development, those lib files are often incomplete, missing, or just plain wrong. This can lead to linker errors that are very difficult to diagnose. The windows-targets
crate ensures that all functions defined by the windows
and windows-sys
crates can be linked without relying on implicit lib files distributed by the toolchain.
For anyone interested, further details can be seen here.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论