我可以获得 Rust 链接的原生工件的完整路径吗?

Can I get the full path of native artifacts that Rust links against?

我在 Windows 10 上使用 32 位版本的 Rust 1.6 来编译 rustlab。当我 运行 cargo build 构建它时,它说 to

link against the following native artifacts when linking against this static library

我也想这样做。有没有办法获取所用库的完整路径?

PS C:\rs\rustlab> cargo build -v
   Compiling libc v0.2.7
     Running `rustc C:\Users\cameron\.cargo\registry\src\github.com-48ad6e4054423464\libc-0.2.7\src\lib.rs --crate-name libc --crate-typ
e lib -g --cfg "feature=\"default\"" -C metadata=0c94fdfb80c4b805 -C extra-filename=-0c94fdfb80c4b805 --out-dir C:\rs\rustlab\target\deb
ug\deps --emit=dep-info,link -L dependency=C:\rs\rustlab\target\debug\deps -L dependency=C:\rs\rustlab\target\debug\deps --cap-lints all
ow`
   Compiling rustlab v0.1.0 (file:///C:/rs/rustlab)
     Running `rustc src\lib.rs --crate-name rustlab --crate-type staticlib --crate-type dylib -g --out-dir C:\rs\rustlab\target\debug --
emit=dep-info,link -L dependency=C:\rs\rustlab\target\debug -L dependency=C:\rs\rustlab\target\debug\deps --extern libc=C:\rs\rustlab\ta
rget\debug\deps\liblibc-0c94fdfb80c4b805.rlib`
note: link against the following native artifacts when linking against this static library
note: the order and any duplication can be significant on some platforms, and so may need to be preserved
note: library: gcc_eh
note: library: gcc_eh
note: library: ws2_32
note: library: userenv
note: library: shell32
note: library: advapi32

例如,我有 WS2_32.lib 的三个 x86 版本。用的是哪一个?

C:\Program Files (x86)\Windows Kits.1\Lib\winv6.3\um\x86\WS2_32.Lib
C:\Program Files (x86)\Windows Kits\Lib.0.10240.0\um\x86\WS2_32.Lib
C:\Program Files (x86)\Windows Kits\Lib.0.10586.0\um\x86\WS2_32.Lib

There are two ABI options for Rust on Windows:MSVC 和 GNU (GCC)。您说您使用的是 32 位版本的 Rust 1.6; Rust 1.6 没有使用 MSVC ABI 的 32 位版本(它仅从 Rust 1.8 开始可用),所以我假设您使用的是 GNU ABI 版本。

如果您确实在使用 GNU ABI 版本,那么您需要 link 针对 GNU ABI 静态库。这些库的名称类似于 libXXXX.a,而不是 MSVC 的 XXXX.lib

如果您的系统上没有任何名为 libws2_32.a 的文件,那么您需要安装 MinGW,它是 Windows 的 GCC 端口,还包括静态库最常用的 Windows DLL。 MinGW 有多个活动 "forks":mingw-w64 and TDM-GCC have more recent versions of GCC than the original MinGW 项目,似乎处于休眠状态。

Is there a way to get the full paths for the libraries that were used?

通常,您在 linking 时不会指定完整路径。对于 GCC,您传递 -lws2_32 之类的选项,然后 linker 将为您找到该库。如果linker没有找到,你可以添加一个-L <path>选项,在linker的静态库搜索路径中添加一个目录。

Cargo 有一些关于如何编写 build script 的文档,可以在您 运行 cargo build.

时自动添加这些选项