gpt4 book ai didi

rust - 在以 x86_64-pc-windows-gnu 为目标时使用 Cygwin 在 Rust 应用程序中链接 NTDLL

转载 作者:行者123 更新时间:2023-11-29 08:18:35 24 4
gpt4 key购买 nike

我在 DarkEld3r's os_info repository 中打开了一个拉取请求检索实际的 Windows 版本和版本名称。

应用程序需要链接到 ntdll.dll 才能使用 RtlGetVersion() .当目标设置为 x86_64-pc-windows-msvc 并使用 Cygwin 和普通 Windows 控制台时,DLL 链接正确。当目标是 x86_64-pc-windows-gnu 时,the linker returns an error :

   Running `rustc --crate-name print_version examples\print_version.rs --crate-type bin --emit=dep-info,link -C debuginfo=2 -C metadata=ec9720acb7c740b3 -C extra-filename=-ec9720acb7c740b3 --out-dir C:\projects\os-info\target\debug\examples -L dependency=C:\projects\os-info\target\debug\deps --extern kernel32=C:\projects\os-info\target\debug\deps\libkernel32-beacffc979f26c46.rlib --extern user32=C:\projects\os-info\target\debug\deps\libuser32-fbb28ae30109687c.rlib --extern winapi=C:\projects\os-info\target\debug\deps\libwinapi-bb7af3f2a31ea235.rlib --extern os_info=C:\projects\os-info\target\debug\deps\libos_info-c5887b186deb8679.rlib`
error: linking with `gcc` failed: exit code: 1
|
= note: "gcc" "-Wl,--enable-long-section-names" "-fno-use-linker-plugin" "-Wl,--nxcompat" "-nostdlib" "-m64" "C:\\Users\\appveyor\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\crt2.o" "C:\\Users\\appveyor\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\rsbegin.o" "-L" "C:\\Users\\appveyor\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib" "C:\\projects\\os-info\\target\\debug\\examples\\print_version-ec9720acb7c740b3.print_version0.rust-cgu.o" "C:\\projects\\os-info\\target\\debug\\examples\\print_version-ec9720acb7c740b3.print_version1.rust-cgu.o" "C:\\projects\\os-info\\target\\debug\\examples\\print_version-ec9720acb7c740b3.print_version10.rust-cgu.o" "C:\\projects\\os-info\\target\\debug\\examples\\print_version-ec9720acb7c740b3.print_version11.rust-cgu.o" "C:\\projects\\os-info\\target\\debug\\examples\\print_version-ec9720acb7c740b3.print_version12.rust-cgu.o" "C:\\projects\\os-info\\target\\debug\\examples\\print_version-ec9720acb7c740b3.print_version2.rust-cgu.o" "C:\\projects\\os-info\\target\\debug\\examples\\print_version-ec9720acb7c740b3.print_version3.rust-cgu.o" "C:\\projects\\os-info\\target\\debug\\examples\\print_version-ec9720acb7c740b3.print_version4.rust-cgu.o" "C:\\projects\\os-info\\target\\debug\\examples\\print_version-ec9720acb7c740b3.print_version5.rust-cgu.o" "C:\\projects\\os-info\\target\\debug\\examples\\print_version-ec9720acb7c740b3.print_version6.rust-cgu.o" "C:\\projects\\os-info\\target\\debug\\examples\\print_version-ec9720acb7c740b3.print_version7.rust-cgu.o" "C:\\projects\\os-info\\target\\debug\\examples\\print_version-ec9720acb7c740b3.print_version8.rust-cgu.o" "C:\\projects\\os-info\\target\\debug\\examples\\print_version-ec9720acb7c740b3.print_version9.rust-cgu.o" "-o" "C:\\projects\\os-info\\target\\debug\\examples\\print_version-ec9720acb7c740b3.exe" "C:\\projects\\os-info\\target\\debug\\examples\\print_version-ec9720acb7c740b3.crate.allocator.rust-cgu.o" "-Wl,--gc-sections" "-nodefaultlibs" "-L" "C:\\projects\\os-info\\target\\debug\\deps" "-L" "C:\\Users\\appveyor\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib" "-Wl,-Bstatic" "C:\\projects\\os-info\\target\\debug\\deps\\libos_info-c5887b186deb8679.rlib" "C:\\projects\\os-info\\target\\debug\\deps\\libkernel32-beacffc979f26c46.rlib" "C:\\projects\\os-info\\target\\debug\\deps\\libuser32-fbb28ae30109687c.rlib" "C:\\projects\\os-info\\target\\debug\\deps\\libwinapi-bb7af3f2a31ea235.rlib" "C:\\Users\\appveyor\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libstd-60d4a252bdda9250.rlib" "C:\\Users\\appveyor\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libpanic_unwind-0b63e20460046692.rlib" "C:\\Users\\appveyor\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libunwind-7daa32005d584d8a.rlib" "C:\\Users\\appveyor\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\liblibc-49ff25caaa6e945f.rlib" "C:\\Users\\appveyor\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\liballoc_system-a05404e5e059417a.rlib" "C:\\Users\\appveyor\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\liballoc-f824a78e715239af.rlib" "C:\\Users\\appveyor\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libstd_unicode-2abaea46f194a6e1.rlib" "C:\\Users\\appveyor\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\librand-81cbf6f1d5a19206.rlib" "C:\\Users\\appveyor\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libcore-f2bd2d524c6ace24.rlib" "C:\\Users\\appveyor\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libcompiler_builtins-c6139fac5e899293.rlib" "-Wl,-Bdynamic" "-l" "ntdll" "-l" "kernel32" "-l" "user32" "-l" "advapi32" "-l" "ws2_32" "-l" "userenv" "-l" "shell32" "-Wl,-Bstatic" "-l" "gcc_eh" "-l" "pthread" "-Wl,-Bdynamic" "-lmingwex" "-lmingw32" "-lgcc" "-lmsvcrt" "-luser32" "-lkernel32" "C:\\Users\\appveyor\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\rsend.o"
= note: ld: cannot find -lntdll

NTDLL.DLL 已链接 on line 12 of this file .

我还没有发现任何关于链接到 NTDLL 的问题。我找到了 How do I specify the linker path in Rust? ,但该解决方案对我也不起作用。我在 Cygwin 的 /usr/lib/w32api 目录中找到了“libntdll.a”库,但似乎 GCC 没有使用它。

有没有什么方法可以在以 GNU 为目标的情况下让它在 Windows 上运行,或者在链接系统 DLL 时我是否被迫使用 MSVC 目标?

最佳答案

我通过查看 winapi crate repository 找到了解决方法.

因为 0.2.8 的 crate 不支持 RtlGetVersion() DLL 函数,我不得不在 repo 中包含 libntdll.a。完成后,我必须创建一个自定义构建脚本以包含库的源路径。

```

/// build.rs
/// Run custom functionality when a build executes
fn main() {
use std::env::var;
use std::path::Path;

// When targeting x86_64-pc-windows-gnu, we need to include the DLL libraries
// found in the lib/x86_64 directory
if var("TARGET").map(|target| target == "x86_64-pc-windows-gnu").unwrap_or(false) {
let dir = var("CARGO_MANIFEST_DIR").unwrap();
println!("cargo:rustc-link-search=native={}", Path::new(&dir).join("lib/x86_64").display());
}

// When targeting i686-pc-windows-gnu, we need to include the DLL libraries
// found in the lib/i686 directory
if var("TARGET").map(|target| target == "i686-pc-windows-gnu").unwrap_or(false) {
let dir = var("CARGO_MANIFEST_DIR").unwrap();
println!("cargo:rustc-link-search=native={}", Path::new(&dir).join("lib/i686").display());
}
}

```

关于rust - 在以 x86_64-pc-windows-gnu 为目标时使用 Cygwin 在 Rust 应用程序中链接 NTDLL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47744477/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com