windows搭建Rust开发环境报错error: linking with `x86_64-w64-mingw32-gcc` failed: exit code: 1
Windows11配置Rust环境
在windows下进行Rust开发,编译器有两个选择要么是msvc,要么是MinGW(GNU)。
msvc不多说,微软自家的C++编译器。
搭建过程
参照了其他网友的安装过程,首先配置环境变量,将安装位置自定义,然后在重启cmd窗口进行自定义安装设置,选择GNU编译器,然后执行安装。
(截至目前,一切正常)
- 安装完成检查 ,
rustc --version
- 构建项目 ,
cargo new hello
- 进入目录 ,
cd hello
- 构建并运行
cargo run
如果出现
Compiling hello v0.1.0 (D:\Rust_Project\hello)
Finished dev [unoptimized + debuginfo] target(s) in 1.36s
Runningtarget\debug\hello.exe
Hello, world!
则安装成功
如果报错:
Compiling hello v0.1.0 (D:\Rust_Project\hello)
error: linking with `x86_64-w64-mingw32-gcc` failed: exit code: 1
|
= note: "x86_64-w64-mingw32-gcc" "-fno-use-linker-plugin" "-Wl,--dynamicbase" "-Wl,--disable-auto-image-base" "-m64" "-Wl,--high-entropy-va" "D:\\Rust\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\rsbegin.o" "C:\\Users\\DELL\\AppData\\Local\\Temp\\rustc6L8C9a\\symbols.o" "D:\\A1Workspace\\1Rust\\hello\\target\\debug\\deps\\hello-94e0b131053dbfdb.15swslepqkj8eu1p.rcgu.o" "D:\\A1Workspace\\1Rust\\hello\\target\\debug\\deps\\hello-94e0b131053dbfdb.1q4jhfk5se492f7k.rcgu.o" "D:\\A1Workspace\\1Rust\\hello\\target\\debug\\deps\\hello-94e0b131053dbfdb.24kwju3ch8niicv2.rcgu.o" "D:\\A1Workspace\\1Rust\\hello\\target\\debug\\deps\\hello-94e0b131053dbfdb.47x4ikpgauz3uioz.rcgu.o" "D:\\A1Workspace\\1Rust\\hello\\target\\debug\\deps\\hello-94e0b131053dbfdb.58nncwb0ayil5w7d.rcgu.o" "D:\\A1Workspace\\1Rust\\hello\\target\\debug\\deps\\hello-94e0b131053dbfdb.u122ff0cthe3tb7.rcgu.o" "D:\\A1Workspace\\1Rust\\hello\\target\\debug\\deps\\hello-94e0b131053dbfdb.qr862d1ou6aiah9.rcgu.o" "-L" "D:\\A1Workspace\\1Rust\\hello\\target\\debug\\deps" "-L" "D:\\Rust\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib" "-Wl,-Bstatic" "D:\\Rust\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libstd-c39a33301a990d4c.rlib" "D:\\Rust\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libpanic_unwind-cca52022a9e303e0.rlib" "D:\\Rust\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libobject-87ab4ba6115beae7.rlib" "D:\\Rust\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libmemchr-5cb7fa9a12663885.rlib" "D:\\Rust\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libaddr2line-63721decd0530f88.rlib" "D:\\Rust\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libgimli-f46ca0e8336ae9ce.rlib" "D:\\Rust\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\librustc_demangle-eb7f3864ed6b2a68.rlib" "D:\\Rust\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libstd_detect-7a1913f37e63e841.rlib" "D:\\Rust\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libhashbrown-bc9416be95f71206.rlib" "D:\\Rust\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\librustc_std_workspace_alloc-583427bdf1f371c2.rlib" "D:\\Rust\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libminiz_oxide-cda0273dfc900176.rlib" "D:\\Rust\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libadler-9e3a606a7eac348e.rlib" "D:\\Rust\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libunwind-17b9231d7a9331b7.rlib" "D:\\Rust\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libcfg_if-dcab4b455bc920a1.rlib" "D:\\Rust\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\liblibc-9852f6e85c86dae7.rlib" "D:\\Rust\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\liballoc-2b83ab5495098fa8.rlib" "D:\\Rust\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\librustc_std_workspace_core-baa2c1aeb27e5083.rlib" "D:\\Rust\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libcore-b5f8c582e1776c2a.rlib" "D:\\Rust\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libcompiler_builtins-d13d1e3b0b541614.rlib" "-Wl,-Bdynamic" "-lkernel32" "-ladvapi32" "-lbcrypt" "-lkernel32" "-lntdll" "-luserenv" "-lws2_32" "-lkernel32" "-lws2_32" "-lkernel32" "-lntdll" "-lkernel32" "-lgcc_eh" "-l:libpthread.a" "-lmsvcrt" "-lmingwex" "-lmingw32" "-lgcc" "-lmsvcrt" "-luser32" "-lkernel32" "-Wl,--nxcompat" "-L" "D:\\Rust\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib" "-o" "D:\\A1Workspace\\1Rust\\hello\\target\\debug\\deps\\hello-94e0b131053dbfdb.exe" "-Wl,--gc-sections" "-no-pie" "-nodefaultlibs" "D:\\Rust\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\rsend.o"
= note: Warning: .drectve `-exclude-symbols:"_ZN4core3ops8function6FnOnce40call_once$u7b$$u7b$vtable.shim$u7d$$u7d$17heb0d76d343146ad9E" ' unrecognized
Warning: .drectve `-exclude-symbols:_ZN4core3ops8function6FnOnce9call_once17h9fe88f2c7072c54bE ' unrecognized
Warning: corrupt .drectve at end of def file
Warning: corrupt .drectve at end of def file
Warning: corrupt .drectve at end of def file
Warning: .drectve `-exclude-symbols:_ZN3std2rt10lang_start17h070876c9816f8b95E ' unrecognized
Warning: corrupt .drectve at end of def file
Warning: corrupt .drectve at end of def file
Warning: corrupt .drectve at end of def file
Warning: corrupt .drectve at end of def file
D:\A1Workspace\1Rust\hello\target\debug\deps\hello-94e0b131053dbfdb.1q4jhfk5se492f7k.rcgu.o: In function `core::ops::function::FnOnce::call_once':
/rustc/82e1608dfa6e0b5569232559e3d385fea5a93112\library\core\src\ops/function.rs:250: undefined reference to `_Unwind_Resume'
D:\Rust\.rustup\toolchains\stable-x86_64-pc-windows-gnu\lib\rustlib\x86_64-pc-windows-gnu\lib\libstd-c39a33301a990d4c.rlib(std-c39a33301a990d4c.std.503e9df93eace999-cgu.0.rcgu.o): In function `alloc::collections::btree::mem::replace':
/rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library\core\src\ptr/mod.rs:(.text+0x28f): undefined reference to `_Unwind_Resume'
D:\Rust\.rustup\toolchains\stable-x86_64-pc-windows-gnu\lib\rustlib\x86_64-pc-windows-gnu\lib\libstd-c39a33301a990d4c.rlib(std-c39a33301a990d4c.std.503e9df93eace999-cgu.0.rcgu.o): In function `<std::io::Write::write_fmt::Adapter<T> as core::fmt::Write>::write_str':
/rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library\std\src\io/mod.rs:1754: undefined reference to `_Unwind_Resume'
D:\Rust\.rustup\toolchains\stable-x86_64-pc-windows-gnu\lib\rustlib\x86_64-pc-windows-gnu\lib\libstd-c39a33301a990d4c.rlib(std-c39a33301a990d4c.std.503e9df93eace999-cgu.0.rcgu.o): In function `<std::io::Write::write_fmt::Adapter<T> as core::fmt::Write>::write_str':
/rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library\std\src\io/mod.rs:1754: undefined reference to `_Unwind_Resume'
D:\Rust\.rustup\toolchains\stable-x86_64-pc-windows-gnu\lib\rustlib\x86_64-pc-windows-gnu\lib\libstd-c39a33301a990d4c.rlib(std-c39a33301a990d4c.std.503e9df93eace999-cgu.0.rcgu.o): In function `<std::io::Write::write_fmt::Adapter<T> as core::fmt::Write>::write_str':
/rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library\std\src\io/mod.rs:1754: undefined reference to `_Unwind_Resume'
D:\Rust\.rustup\toolchains\stable-x86_64-pc-windows-gnu\lib\rustlib\x86_64-pc-windows-gnu\lib\libstd-c39a33301a990d4c.rlib(std-c39a33301a990d4c.std.503e9df93eace999-cgu.0.rcgu.o):/rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library\alloc\src/alloc.rs:(.text+0x1546): more undefined references to `_Unwind_Resume' follow
D:\Rust\.rustup\toolchains\stable-x86_64-pc-windows-gnu\lib\rustlib\x86_64-pc-windows-gnu\lib\libstd-c39a33301a990d4c.rlib(std-c39a33301a990d4c.std.503e9df93eace999-cgu.0.rcgu.o): In function `rust_eh_personality':
/rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library\std\src\sys\personality/gcc.rs:247: undefined reference to `_GCC_specific_handler'
D:\Rust\.rustup\toolchains\stable-x86_64-pc-windows-gnu\lib\rustlib\x86_64-pc-windows-gnu\lib\libstd-c39a33301a990d4c.rlib(std-c39a33301a990d4c.std.503e9df93eace999-cgu.0.rcgu.o): In function `<std::sys::windows::fs::DirBuffIter as core::iter::traits::iterator::Iterator>::next':
/rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library\alloc\src/alloc.rs:(.text+0x3bfb3): undefined reference to `_Unwind_Resume'
D:\Rust\.rustup\toolchains\stable-x86_64-pc-windows-gnu\lib\rustlib\x86_64-pc-windows-gnu\lib\libstd-c39a33301a990d4c.rlib(std-c39a33301a990d4c.std.503e9df93eace999-cgu.0.rcgu.o): In function `<std::sys::windows::fs::File as core::fmt::Debug>::fmt':
/rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library\alloc\src/alloc.rs:(.text+0x3c213): undefined reference to `_Unwind_Resume'
D:\Rust\.rustup\toolchains\stable-x86_64-pc-windows-gnu\lib\rustlib\x86_64-pc-windows-gnu\lib\libstd-c39a33301a990d4c.rlib(std-c39a33301a990d4c.std.503e9df93eace999-cgu.0.rcgu.o): In function `std::sys::windows::fs::readdir':
/rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library\std\src\sys\windows/fs.rs:(.text+0x3c621): undefined reference to `_Unwind_Resume'
D:\Rust\.rustup\toolchains\stable-x86_64-pc-windows-gnu\lib\rustlib\x86_64-pc-windows-gnu\lib\libstd-c39a33301a990d4c.rlib(std-c39a33301a990d4c.std.503e9df93eace999-cgu.0.rcgu.o): In function `alloc::alloc::dealloc':
/rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library\alloc\src/alloc.rs:117: undefined reference to `_Unwind_Resume'
D:\Rust\.rustup\toolchains\stable-x86_64-pc-windows-gnu\lib\rustlib\x86_64-pc-windows-gnu\lib\libstd-c39a33301a990d4c.rlib(std-c39a33301a990d4c.std.503e9df93eace999-cgu.0.rcgu.o): In function `std::sys::windows::fs::rename':
/rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library\alloc\src/alloc.rs:(.text+0x3c8a9): undefined reference to `_Unwind_Resume'
D:\Rust\.rustup\toolchains\stable-x86_64-pc-windows-gnu\lib\rustlib\x86_64-pc-windows-gnu\lib\libstd-c39a33301a990d4c.rlib(std-c39a33301a990d4c.std.503e9df93eace999-cgu.0.rcgu.o):/rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library\std\src\os\windows\io/handle.rs:368: more undefined references to `_Unwind_Resume' follow
D:\Rust\.rustup\toolchains\stable-x86_64-pc-windows-gnu\lib\rustlib\x86_64-pc-windows-gnu\lib\libpanic_unwind-cca52022a9e303e0.rlib(panic_unwind-cca52022a9e303e0.panic_unwind.246e719bc435ba30-cgu.0.rcgu.o): In function `panic_unwind::real_imp::panic':
/rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library\panic_unwind\src/gcc.rs:72: undefined reference to `_Unwind_RaiseException'
D:\Rust\.rustup\toolchains\stable-x86_64-pc-windows-gnu\lib\rustlib\x86_64-pc-windows-gnu\lib\libpanic_unwind-cca52022a9e303e0.rlib(panic_unwind-cca52022a9e303e0.panic_unwind.246e719bc435ba30-cgu.0.rcgu.o): In function `core::ptr::drop_in_place<alloc::boxed::Box<dyn core::any::Any+core::marker::Send>>':
/rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library\alloc\src/alloc.rs:(.text+0x199): undefined reference to `_Unwind_Resume'
D:\Rust\.rustup\toolchains\stable-x86_64-pc-windows-gnu\lib\rustlib\x86_64-pc-windows-gnu\lib\libobject-87ab4ba6115beae7.rlib(object-87ab4ba6115beae7.object.aff377d705a54930-cgu.0.rcgu.o):object.aff377d705a:(.text+0x8d4): undefined reference to `_Unwind_Resume'
D:\Rust\.rustup\toolchains\stable-x86_64-pc-windows-gnu\lib\rustlib\x86_64-pc-windows-gnu\lib\libobject-87ab4ba6115beae7.rlib(object-87ab4ba6115beae7.object.aff377d705a54930-cgu.0.rcgu.o):object.aff377d705a:(.text+0x1fb8): undefined reference to `_Unwind_Resume'
D:\Rust\.rustup\toolchains\stable-x86_64-pc-windows-gnu\lib\rustlib\x86_64-pc-windows-gnu\lib\libobject-87ab4ba6115beae7.rlib(object-87ab4ba6115beae7.object.aff377d705a54930-cgu.0.rcgu.o):object.aff377d705a:(.text+0x2a85): undefined reference to `_Unwind_Resume'
D:\Rust\.rustup\toolchains\stable-x86_64-pc-windows-gnu\lib\rustlib\x86_64-pc-windows-gnu\lib\libgimli-f46ca0e8336ae9ce.rlib(gimli-f46ca0e8336ae9ce.gimli.500d92357741775b-cgu.0.rcgu.o):gimli.500d92357741:(.text+0x1956): undefined reference to `_Unwind_Resume'
D:\Rust\.rustup\toolchains\stable-x86_64-pc-windows-gnu\lib\rustlib\x86_64-pc-windows-gnu\lib\libgimli-f46ca0e8336ae9ce.rlib(gimli-f46ca0e8336ae9ce.gimli.500d92357741775b-cgu.0.rcgu.o):gimli.500d92357741:(.text+0x19e6): more undefined references to `_Unwind_Resume' follow
collect2.exe: error: ld returned 1 exit status
= note: some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified
= note: use the `-l` flag to specify native libraries to link
= note: use the `cargo:rustc-link-lib` directive to specify the native libraries to link with Cargo (see https://doc.rust-lang.org/cargo/reference/build-scripts.html#cargorustc-link-libkindname)
error: could not compile `hello` (bin "hello") due to previous error
问题思考
出现上述报错一般是编译器的问题,msvc和gnu两个是不兼容的编译器,用了msvc编译的库,那么这个库也只能引用msvc编译的依赖库;gnu也是一样。
既然在windows下编写,仍然建议使用msvc编译器。
卸载Rust
在之前的命令行窗口中继续输入
rustup self uninstall
重新执行安装
再次安装时,选择默认的 x86_64-pc-windows-msvc,不修改为 x86_64-pc-windows-gnu 了。别的参数可以按照自己的需要去调整。
装完后,再创建hello项目编译运行,成功。
经过这一波折腾,着实不容易。我觉得在windows平台,还是用msvc好了,别折腾gnu了,或者可能有更好的解决方案,大家可以评论区交流。
Cargo 内容:
- cargo build 构建项目。
- cargo run 一步构建并运行项目。
- cargo check 在不生成二进制文件的情况下构建项目来检查错误。
- 有别于将构建结果放在与源码相同的目录,Cargo 会将其放到 target/debug 目录。
开发环境配置
个人比较喜欢Jetbrains的CLion,以下简单描述一下Rust在CLion中的配置
1、下载安装CLion,如果想免费用,请您自行想办法。然后,打开CLion, 依次点击File -> Settings -> Plugins,搜索Rust,然后点击安装。(即 安装Rust插件)
2、 新建项目,左侧选择Rust,然后有测选择Binary,点击创建即可。