前言:
1,公司要給一線搞一個升級程序,需要在win下跑。 之前都是找開發總監幫忙,但是他最近比較忙。就讓我自己搞。有了下文.。說來慚愧,之前寫過一篇ubuntu下編譯windows的文章。里面的demo就一句話 fuck world。依賴的庫很少。這個是公司的工程,依賴的庫很多。想的有點簡單了。大家還是要把底子打好
工程介紹。
需要編譯patch.rs這個文件。
1,安裝target
rustup target add x86_64-pc-windows-gnu
2,安裝gcc?
?sudo apt-get install mingw-w64
執行編譯后提示?
?第一個坑:
warning: sys-info@0.9.1: c/windows.c:4:10: fatal error: windows.h: No such file or directory
warning: sys-info@0.9.1: 4 | #include <windows.h>
warning: sys-info@0.9.1: | ^~~~~~~~~~~
warning: sys-info@0.9.1: compilation terminated.error: failed to run custom build command for sys-info v0.9.1Caused by:process didn't exit successfully: /data/anymembak/target/debug/build/sys-info-f88ff491732c39f9/build-script-build (exit status: 1)--- stdoutTARGET = Some("x86_64-pc-windows-gnu")OPT_LEVEL = Some("0")HOST = Some("x86_64-unknown-linux-gnu")cargo:rerun-if-env-changed=CC_x86_64-pc-windows-gnuCC_x86_64-pc-windows-gnu = Nonecargo:rerun-if-env-changed=CC_x86_64_pc_windows_gnuCC_x86_64_pc_windows_gnu = Nonecargo:rerun-if-env-changed=TARGET_CCTARGET_CC = Nonecargo:rerun-if-env-changed=CCCC = Some("/usr/bin/gcc")cargo:rerun-if-env-changed=CC_ENABLE_DEBUG_OUTPUTcargo:rerun-if-env-changed=CRATE_CC_NO_DEFAULTSCRATE_CC_NO_DEFAULTS = NoneDEBUG = Some("true")CARGO_CFG_TARGET_FEATURE = Some("cmpxchg16b,fxsr,sse,sse2,sse3")cargo:rerun-if-env-changed=CFLAGS_x86_64-pc-windows-gnuCFLAGS_x86_64-pc-windows-gnu = Nonecargo:rerun-if-env-changed=CFLAGS_x86_64_pc_windows_gnuCFLAGS_x86_64_pc_windows_gnu = Nonecargo:rerun-if-env-changed=TARGET_CFLAGSTARGET_CFLAGS = Nonecargo:rerun-if-env-changed=CFLAGSCFLAGS = Nonecargo:warning=c/windows.c:4:10: fatal error: windows.h: No such file or directorycargo:warning= 4 | #include <windows.h>cargo:warning= | ^~~~~~~~~~~cargo:warning=compilation terminated.--- stderrerror occurred: Command "/usr/bin/gcc" "-O0" "-ffunction-sections" "-fdata-sections" "-gdwarf-2" "-fno-omit-frame-pointer" "-m64" "-Wall" "-Wextra" "-o" "/data/anymembak/target/x86_64-pc-windows-gnu/debug/build/sys-info-20c4b6441d409db8/out/db3b6bfb95261072-windows.o" "-c" "c/windows.c" with args gcc did not execute successfully (status code exit status: 1).warning: build failed, waiting for other jobs to finish...
error: failed to run custom build command for openssl-sys v0.9.102Caused by:process didn't exit successfully: /data/anymembak/target/debug/build/openssl-sys-e6c4b7d6399b36bc/build-script-main (exit status: 101)--- stdoutcargo:rerun-if-env-changed=X86_64_PC_WINDOWS_GNU_OPENSSL_NO_VENDORX86_64_PC_WINDOWS_GNU_OPENSSL_NO_VENDOR unsetcargo:rerun-if-env-changed=OPENSSL_NO_VENDOROPENSSL_NO_VENDOR unsetcargo:rerun-if-env-changed=CC_x86_64-pc-windows-gnuCC_x86_64-pc-windows-gnu = Nonecargo:rerun-if-env-changed=CC_x86_64_pc_windows_gnuCC_x86_64_pc_windows_gnu = Nonecargo:rerun-if-env-changed=TARGET_CCTARGET_CC = Nonecargo:rerun-if-env-changed=CCCC = Some("/usr/bin/gcc")cargo:rerun-if-env-changed=CC_ENABLE_DEBUG_OUTPUTcargo:rerun-if-env-changed=CRATE_CC_NO_DEFAULTSCRATE_CC_NO_DEFAULTS = NoneDEBUG = Some("true")CARGO_CFG_TARGET_FEATURE = Some("cmpxchg16b,fxsr,sse,sse2,sse3")cargo:rerun-if-env-changed=CFLAGS_x86_64-pc-windows-gnuCFLAGS_x86_64-pc-windows-gnu = Nonecargo:rerun-if-env-changed=CFLAGS_x86_64_pc_windows_gnuCFLAGS_x86_64_pc_windows_gnu = Nonecargo:rerun-if-env-changed=TARGET_CFLAGSTARGET_CFLAGS = Nonecargo:rerun-if-env-changed=CFLAGSCFLAGS = Nonecargo:rerun-if-env-changed=AR_x86_64-pc-windows-gnuAR_x86_64-pc-windows-gnu = Nonecargo:rerun-if-env-changed=AR_x86_64_pc_windows_gnuAR_x86_64_pc_windows_gnu = Nonecargo:rerun-if-env-changed=TARGET_ARTARGET_AR = Nonecargo:rerun-if-env-changed=ARAR = NoneRUSTC_LINKER = Some("x86_64-w64-mingw32-gcc")cargo:rerun-if-env-changed=CROSS_COMPILECROSS_COMPILE = Nonecargo:rerun-if-env-changed=ARFLAGS_x86_64-pc-windows-gnuARFLAGS_x86_64-pc-windows-gnu = Nonecargo:rerun-if-env-changed=ARFLAGS_x86_64_pc_windows_gnuARFLAGS_x86_64_pc_windows_gnu = Nonecargo:rerun-if-env-changed=TARGET_ARFLAGSTARGET_ARFLAGS = Nonecargo:rerun-if-env-changed=ARFLAGSRANLIBFLAGS = Nonerunning cd "/data/anymembak/target/x86_64-pc-windows-gnu/debug/build/openssl-sys-22d1608ab45429ff/out/openssl-build/build/src" && env -u CROSS_COMPILE AR="x86_64-w64-mingw32-ar" CC="/usr/bin/gcc" RANLIB="x86_64-w64-mingw32-ranlib" "perl" "./Configure" "--prefix=/data/anymembak/target/x86_64-pc-windows-gnu/debug/build/openssl-sys-22d1608ab45429ff/out/openssl-build/install" "--openssldir=SYS$MANAGER:[OPENSSL]" "no-dso" "no-shared" "no-ssl3" "no-tests" "no-comp" "no-zlib" "no-zlib-dynamic" "--libdir=lib" "no-md2" "no-rc5" "no-weak-ssl-ciphers" "no-camellia" "no-idea" "no-seed" "no-capieng" "mingw64" "-O2" "-ffunction-sections" "-fdata-sections" "-gdwarf-2" "-fno-omit-frame-pointer" "-m64" "-Wa,-mbig-obj"Configuring OpenSSL version 3.2.1 for target mingw64Using os-specific seed configurationCreated configdata.pmRunning configdata.pmCreated Makefile.inCreated MakefileCreated include/openssl/configuration.h************************************************************************* ****** OpenSSL has been successfully configured ****** ****** If you encounter a problem while building, please open an ****** issue on GitHub <https://github.com/openssl/openssl/issues> ****** and include the output from the following command: ****** ****** perl configdata.pm --dump ****** ****** (If you are new to OpenSSL, you might want to consult the ****** 'Troubleshooting' section in the INSTALL.md file first) ****** *************************************************************************running cd "/data/anymembak/target/x86_64-pc-windows-gnu/debug/build/openssl-sys-22d1608ab45429ff/out/openssl-build/build/src" && "make" "depend"
因為沒有配置變量。簡單的說就是用了linux下的gcc ,肯定會失敗的。沒有找到對應的文件。
本質是 build-script 里調用的 C/匯編工具鏈仍然是 Linux 的 /usr/bin/gcc
/ /usr/bin/as
,而不是 MinGW-w64 的交叉編譯器。于是:
rust 的配置文件中。增加env選項。
3,修改配置
.cargo/config.toml
[target.x86_64-pc-windows-gnu]
linker = "x86_64-w64-mingw32-gcc"
ar = "x86_64-w64-mingw32-ar"rustflags = ["-L/usr/x86_64-w64-mingw32/lib","-luser32","-lgdi32","-lkernel32","-ladvapi32",
][env]
CC_x86_64_pc_windows_gnu = "x86_64-w64-mingw32-gcc"
CXX_x86_64_pc_windows_gnu = "x86_64-w64-mingw32-g++"
AR_x86_64_pc_windows_gnu = "x86_64-w64-mingw32-ar"
RANLIB_x86_64_pc_windows_gnu = "x86_64-w64-mingw32-ranlib"
CFLAGS_x86_64_pc_windows_gnu = "-DWINVER=0x0600 -D_WIN32_WINNT=0x0600"
CXXFLAGS_x86_64_pc_windows_gnu = "-DWINVER=0x0600 -D_WIN32_WINNT=0x0600"
4、在執行
note: /usr/bin/x86_64-w64-mingw32-ld: cannot find -lUser32: No such file or directorycollect2: error: ld returned 1 exit status
ls /usr/x86_64-w64-mingw32/lib/libuser32.a? 查看了下有文件。那就是沒有建立鏈接。需要執行link命令
sudo ln -s /usr/x86_64-w64-mingw32/lib/libuser32.a \/usr/x86_64-w64-mingw32/lib/libUser32.a
原因:
-
在
x86_64-pc-windows-gnu
目標下,Rust 會讓 MinGW-w64 鏈接器把 Windows 系統庫 User32、Kernel32、Gdi32 … 加到命令行(見源碼里-luser32 -lkernel32
等)?GitHub。 -
這些庫不是 DLL,而是 導入庫(
libuser32.a
、libkernel32.a
…)。它們隨 MinGW-w64 CRT/headers 包一起提供。 -
你的 Linux 系統上只裝了
x86_64-w64-mingw32-gcc
,卻 沒有裝對應的 CRT 和 Win32 API 導入庫,結果鏈接階段就找不到libuser32.a
,于是報錯。
吐槽:不是哥們(linux大佬們),你們這樣搞 真的好嗎。。。
執行。成功。朋友們 ,歡呼吧 enjoy rust 。
但是體積非常大190M。原因:
默認的 cargo build
是 Debug 構建,再加上:
-
Windows GNU 目標會把 libstd、OpenSSL、Paho-MQTT 等一大堆依賴全都 靜態地 鏈進去;
-
Debug 模式保留完整符號表,方便 gdb/VS Code 調試;
-
沒有做 LTO/代碼裁剪;
所以可執行文件非常大(上百 MB 很正常)。
執行優化命令
cargo build -p ${crate}--example patch --release --target x86_64-pc-windows-gnu
-
-p/--package test告訴 Cargo 只對 test這個包動手,避免工作區里其它 crate 被順帶編譯。
-
--example patch?表示只編譯 examples/patch.rs 生成的可執行文件。
體積:27M
繼續優化
cargo build -p {$crate}--example patch --profile release-size --target x86_64-pc-windows-gnu
體積:1.6M 。完美。
解析:release-size
cargo.toml中添加 ;減少其他的附帶項
[profile.release-size]
inherits = "release"
# https://www.aloxaf.com/2018/09/reduce_rust_size/
strip = true # Automatically strip symbols from the binary.
opt-level = 'z' # Optimize for size.
lto = true #
panic = 'abort'
codegen-units = 1
GAME OVER
====這不是最后的
error: linking with x86_64-w64-mingw32-gcc failed: exit status: 1|= note: LC_ALL="C" PATH="/home/bruce/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/bin:/home/bruce/.cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin:/opt/apps/com.volcengine.feilian/files/mdm/opt/corplink-mdm/bin:/usr/arm-linux-gnueabihf/bin:/opt/arm-linux-musleabihf-cross/bin:/usr/bin:/usr/arm-linux-gnueabihf/bin:/opt/arm-linux-musleabihf-cross/bin:/usr/bin:/usr/arm-linux-gnueabihf/bin:/opt/arm-linux-musleabihf-cross/bin:/usr/bin" VSLANG="1033" "x86_64-w64-mingw32-gcc" "-fno-use-linker-plugin" "-Wl,--dynamicbase" "-Wl,--disable-auto-image-base" "-m64" "-Wl,--high-entropy-va" "/home/bruce/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-pc-windows-gnu/lib/rsbegin.o" "/tmp/rustcXg6Ks4/symbols.o" "/data/a/target/x86_64-pc-windows-gnu/release-size/deps/apm-9332c89c545fc484.apm.49192088e6a58101-cgu.0.rcgu.o" "-L" "/data/a/target/x86_64-pc-windows-gnu/release-size/deps" "-L" "/data/a/target/release-size/deps" "-L" "/home/bruce/.cargo/registry/src/172.16.125.28-0c009ce4dc666fcd/windows_x86_64_gnu-0.48.5/lib" "-L" "/home/bruce/.cargo/registry/src/172.16.125.28-0c009ce4dc666fcd/windows_x86_64_gnu-0.52.5/lib" "-L" "/data/a/target/x86_64-pc-windows-gnu/release-size/build/openssl-sys-fc0e3cf9791f0c87/out/openssl-build/install/lib" "-L" "/data/a/target/x86_64-pc-windows-gnu/release-size/build/libz-ng-sys-4496acf21f6c5d86/out/lib" "-L" "/data/a/target/x86_64-pc-windows-gnu/release-size/build/libz-ng-sys-4496acf21f6c5d86/out/lib64" "-L" "/home/bruce/.cargo/registry/src/172.16.125.28-0c009ce4dc666fcd/winapi-x86_64-pc-windows-gnu-0.4.0/lib" "-L" "/data/a/target/x86_64-pc-windows-gnu/release-size/build/rdkafka-sys-c542daaeb01821a5/out/lib" "-L" "/data/a/target/x86_64-pc-windows-gnu/release-size/build/libz-sys-2a5b747d96b409af/out/lib" "-L" "/data/a/target/x86_64-pc-windows-gnu/release-size/build/libz-sys-2a5b747d96b409af/out/lib" "-L" "/data/a/target/x86_64-pc-windows-gnu/release-size/build/ring-b136e78e66c2fc5d/out" "-L" "/data/a/target/x86_64-pc-windows-gnu/release-size/build/bzip2-sys-deae5791ed63b43a/out/lib" "-L" "/data/a/target/x86_64-pc-windows-gnu/release-size/build/zstd-sys-b793f55346992187/out" "-L" "/home/bruce/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-pc-windows-gnu/lib" "-Wl,-Bstatic" "/tmp/rustcXg6Ks4/libzstd_sys-599e1ba35eab961b.rlib" "/tmp/rustcXg6Ks4/libbzip2_sys-ef97b0b83aecce6e.rlib" "/tmp/rustcXg6Ks4/librdkafka_sys-b6c359dfa7f2ba04.rlib" "/tmp/rustcXg6Ks4/liblibz_sys-cdbcc05b320ff190.rlib" "/tmp/rustcXg6Ks4/libopenssl_sys-03b3492ba7b502cd.rlib" "/tmp/rustcXg6Ks4/libring-c9a050c1e55b0ed1.rlib" "/tmp/rustcXg6Ks4/liblibz_ng_sys-0100ee76e53efe33.rlib" "/tmp/rustcXg6Ks4/libstd-a1f74822451877d1.rlib" "/home/bruce/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-pc-windows-gnu/lib/libcompiler_builtins-b44a9797859024b2.rlib" "-Wl,-Bdynamic" "-liphlpapi" "-lPacket" "-lgdi32" "-luser32" "-lcrypt32" "-lws2_32" "-ladvapi32" "-lwinapi_advapi32" "-lwinapi_cfgmgr32" "-lwinapi_comctl32" "-lwinapi_gdi32" "-lwinapi_kernel32" "-lwinapi_msimg32" "-lwinapi_opengl32" "-lwinapi_setupapi" "-lwinapi_user32" "-lwinapi_winspool" "-lwinapi_ws2_32" "-lkernel32" "-lwindows.0.52.0" "-lbcrypt" "-ladvapi32" "-lwindows.0.52.0" "-lntdll" "-lwindows.0.48.5" "-lwindows.0.52.0" "-lkernel32" "-ladvapi32" "-lkernel32" "-lntdll" "-luserenv" "-lws2_32" "-lkernel32" "-lws2_32" "-lkernel32" "-lgcc_eh" "-l:libpthread.a" "-lmsvcrt" "-lmingwex" "-lmingw32" "-lgcc" "-lmsvcrt" "-luser32" "-lkernel32" "-Wl,--nxcompat" "-L" "/home/bruce/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-pc-windows-gnu/lib" "-o" "/data/a/target/x86_64-pc-windows-gnu/release-size/deps/apm-9332c89c545fc484.exe" "-Wl,--gc-sections" "-no-pie" "-Wl,--strip-all" "-nodefaultlibs" "-Wl,--disable-runtime-pseudo-reloc" "/home/bruce/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-pc-windows-gnu/lib/rsend.o"= note: /usr/bin/x86_64-w64-mingw32-ld: cannot find -lPacket: No such file or directorycollect2: error: ld returned 1 exit status
之后我編譯了整個crate,提示上面的問題。
cargo tree -e features | grep -i packet
能快速定位是哪條依賴鏈、哪個 feature 打開的,方便你決定 “裝 SDK” 還是 “關 feature
-lPacket
指的是 WinPcap / Npcap SDK 里的 Packet.lib (或 libPacket.a)。
在 Windows 下,只要你鏈接了 librdkafka、pnet、winpcap 等底層網絡庫,build-rs 通常都會
沒有lib文件
-
下載 Npcap SDK
https://npcap.com/#download → Npcap SDK(ZIP)。 -
拷貝頭文件與靜態庫(假設 SDK 解壓到
~/npcap-sdk
):sudo cp -R ~/npcap-sdk/Include/* /usr/x86_64-w64-mingw32/include/ sudo cp ~/npcap-sdk/Lib/x64/Packet.lib /usr/x86_64-w64-mingw32/lib/
在編譯。成功。結束