使用 Rust Cargo 构建时出现未定义的引用错误

Undefined reference error when building with Rust Cargo

我在使用 Cargo 构建项目时收到这些错误消息:

error: linking with `gcc` failed: exit code: 1
note: gcc '-Wl,--enable-long-section-names' '-fno-use-linker-plugin' '-Wl,--nxcompat' '-static-libgcc' '-m64' '-L' 'C:\Rust\bin\rustlib\x86_64-pc-windows-gnu\lib' '-o' 'D:\__start\rust-iron\target\rust-iron.exe' 'D:\__start\rust-iron\target\rust-iron.o' '-Wl,--gc-sections' 'D:\__start\rust-iron\target\deps\libiron-bc16133d0a47ffb0.rlib' 'D:\__start\rust-iron\target\deps\libhyper-f94dbe5abc871209.rlib' 'D:\__start\rust-iron\target\deps\libmodifier-1f4dc100e0edbf7d.rlib' 'D:\__start\rust-iron\target\deps\libmucell-ac2514f1ba9841c5.rlib' 'D:\__start\rust-iron\target\deps\libplugin-3af9d50d18e9bee7.rlib' 'D:\__start\rust-iron\target\deps\libmime-115ca8125c42dc12.rlib' 'D:\__start\rust-iron\target\deps\liberror-e47fda06a095ed9c.rlib' 'D:\__start\rust-iron\target\deps\libtypeable-175afcd94f146dcc.rlib' 'D:\__start\rust-iron\target\deps\libphantom-cd9583ecf037f4d7.rlib' 'D:\__start\rust-iron\target\deps\liblog-4e79c2d7625e8c6f.rlib' 'D:\__start\rust-iron\target\deps\libcookie-4a08a2c06bb5c4fc.rlib' 'D:\__start\rust-iron\target\deps\liburl-4c384fb33b0bde93.rlib' 'D:\__start\rust-iron\target\deps\librustc-serialize-8214bc2e838e96ca.rlib' 'D:\__start\rust-iron\target\deps\libopenssl-9e47789a605aabbf.rlib' 'D:\__start\rust-iron\target\deps\libtime-24f9c2dbbbc6f19b.rlib' 'D:\__start\rust-iron\target\deps\libmatches-68db25b520030534.rlib' 'D:\__start\rust-iron\target\deps\libopenssl-sys-f6d7192b17104bf7.rlib' 'D:\__start\rust-iron\target\deps\libtypemap-add7a21f97d0ca71.rlib' 'D:\__start\rust-iron\target\deps\libunsafe-any-3f018797a3613438.rlib' 'D:\__start\rust-iron\target\deps\libunicase-88de2458e97b894d.rlib' 'C:\Rust\bin\rustlib\x86_64-pc-windows-gnu\lib\libstd-4e7c5e5c.rlib' 'C:\Rust\bin\rustlib\x86_64-pc-windows-gnu\lib\libcollections-4e7c5e5c.rlib' 'C:\Rust\bin\rustlib\x86_64-pc-windows-gnu\lib\libunicode-4e7c5e5c.rlib' 'C:\Rust\bin\rustlib\x86_64-pc-windows-gnu\lib\librand-4e7c5e5c.rlib' 'C:\Rust\bin\rustlib\x86_64-pc-windows-gnu\lib\liballoc-4e7c5e5c.rlib' 'C:\Rust\bin\rustlib\x86_64-pc-windows-gnu\lib\liblibc-4e7c5e5c.rlib' 'C:\Rust\bin\rustlib\x86_64-pc-windows-gnu\lib\libcore-4e7c5e5c.rlib' '-L' 'D:\__start\rust-iron\target' '-L' 'D:\__start\rust-iron\target\deps' '-L' '\opt\windows_64\lib64' '-L' 'D:\__start\rust-iron\target\build\time-24f9c2dbbbc6f19b\out' '-L' 'C:\Rust\bin\rustlib\x86_64-pc-windows-gnu\lib' '-L' 'D:\__start\rust-iron\.rust\bin\x86_64-pc-windows-gnu' '-L' 'D:\__start\rust-iron\bin\x86_64-pc-windows-gnu' '-Wl,--whole-archive' '-Wl,-Bstatic' '-Wl,--no-whole-archive' '-Wl,-Bdynamic' '-lssl' '-lcrypto' '-lws2_32' '-lcompiler-rt'
note: C:\Rust\bin\rustlib\x86_64-pc-windows-gnu\lib/libcrypto.a(c_zlib.o):c_zlib.c:(.text+0x59): undefined reference to `inflate'
C:\Rust\bin\rustlib\x86_64-pc-windows-gnu\lib/libcrypto.a(c_zlib.o):c_zlib.c:(.text+0xea): undefined reference to `deflate'
C:\Rust\bin\rustlib\x86_64-pc-windows-gnu\lib/libcrypto.a(c_zlib.o):c_zlib.c:(.text+0x13d): undefined reference to `inflateEnd'
C:\Rust\bin\rustlib\x86_64-pc-windows-gnu\lib/libcrypto.a(c_zlib.o):c_zlib.c:(.text+0x158): undefined reference to `deflateEnd'
C:\Rust\bin\rustlib\x86_64-pc-windows-gnu\lib/libcrypto.a(c_zlib.o):c_zlib.c:(.text+0x1c3): undefined reference to `inflateEnd'
C:\Rust\bin\rustlib\x86_64-pc-windows-gnu\lib/libcrypto.a(c_zlib.o):c_zlib.c:(.text+0x1cc): undefined reference to `deflateEnd'
C:\Rust\bin\rustlib\x86_64-pc-windows-gnu\lib/libcrypto.a(c_zlib.o):c_zlib.c:(.text+0x267): undefined reference to `inflateInit_'
C:\Rust\bin\rustlib\x86_64-pc-windows-gnu\lib/libcrypto.a(c_zlib.o):c_zlib.c:(.text+0x2dd): undefined reference to `deflateInit_'
C:\Rust\bin\rustlib\x86_64-pc-windows-gnu\lib/libcrypto.a(c_zlib.o):c_zlib.c:(.text+0x545): undefined reference to `deflate'
C:\Rust\bin\rustlib\x86_64-pc-windows-gnu\lib/libcrypto.a(c_zlib.o):c_zlib.c:(.text+0x5ff): undefined reference to `zError'
C:\Rust\bin\rustlib\x86_64-pc-windows-gnu\lib/libcrypto.a(c_zlib.o):c_zlib.c:(.text+0x7f0): undefined reference to `deflate'
C:\Rust\bin\rustlib\x86_64-pc-windows-gnu\lib/libcrypto.a(c_zlib.o):c_zlib.c:(.text+0x8b1): undefined reference to `zError'
C:\Rust\bin\rustlib\x86_64-pc-windows-gnu\lib/libcrypto.a(c_zlib.o):c_zlib.c:(.text+0x911): undefined reference to `deflateInit_'
C:\Rust\bin\rustlib\x86_64-pc-windows-gnu\lib/libcrypto.a(c_zlib.o):c_zlib.c:(.text+0x9e0): undefined reference to `inflate'
C:\Rust\bin\rustlib\x86_64-pc-windows-gnu\lib/libcrypto.a(c_zlib.o):c_zlib.c:(.text+0xa4f): undefined reference to `zError'
C:\Rust\bin\rustlib\x86_64-pc-windows-gnu\lib/libcrypto.a(c_zlib.o):c_zlib.c:(.text+0xac4): undefined reference to `inflateInit_'
ld: C:\Rust\bin\rustlib\x86_64-pc-windows-gnu\lib/libcrypto.a(c_zlib.o): bad reloc address 0x8 in section `.data'

error: aborting due to previous error

我读到我可以添加 -lz 标志来防止这种情况发生,但是我该如何使用 Cargo 来做到这一点?

Build scripts!

将行 build = "build.rs" 添加到您的 Cargo.toml,然后为 build.rs 编写:

fn main() {
    println!("cargo:rustc-flags=-lz");
}

理论上会将该标志添加到您的编译参数中