VS Code中MinGW-w64安装失败及C++代码编译退出码-1问题求助
VS Code中MinGW-w64安装失败及C++代码编译退出码-1问题求助
我最近在VS Code上学习C++编程,跟着官方教程配置MinGW-w64环境,但接连遇到两个问题卡壳了,折腾了好久没搞定,麻烦大家帮忙支支招:
问题1:MinGW-w64安装时出现下载超时错误
通过MSYS2安装MinGW-w64工具链的过程中,终端疯狂抛出下载失败的错误,要么是连接超时,要么是传输速度慢到几乎停滞,具体错误内容如下:
error: failed retrieving file 'mingw-w64-ucrt-x86_64-openssl-3.6.0-1-any.pkg.tar.zst' from mirror.msys2.org : Connection timed out after 10017 milliseconds error: failed retrieving file 'mingw-w64-ucrt-x86_64-ncurses-6.5.20250927-2-any.pkg.tar.zst' from mirror.msys2.org : Operation too slow. Less than 1 bytes/sec transferred the last 10 seconds error: failed retrieving file 'texinfo-7.2-2-x86_64.pkg.tar.zst' from mirror.msys2.org : Operation too slow. Less than 1 bytes/sec transferred the last 10 seconds warning: too many errors from mirror.msys2.org, skipping for the remainder of this transaction error: failed retrieving file 'mingw-w64-ucrt-x86_64-isl-0.27-1-any.pkg.tar.zst' from mirror.msys2.org : Operation too slow. Less than 1 bytes/sec transferred the last 10 seconds ...(其余类似下载错误)
我怀疑是默认镜像源的网络问题,但不知道怎么调整,也不确定这种不完整的安装会不会影响后续编译,请问该怎么解决这个安装问题?
问题2:编译C++代码时触发链接错误,终端返回退出码-1
硬着头皮完成安装后,我运行一段简单的测试代码,结果编译直接失败,终端返回退出码-1,错误全是undefined reference类型的链接错误,比如找不到std::cout的定义。
我的测试代码:
#include <iostream> #include <vector> #include <string> using namespace std; int main(){ vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"}; for (const string& word : msg) { cout << word << " "; } cout << endl; }
VS Code自动生成的编译命令:
Executing task: C/C++: gcc.exe build active file Starting build... cmd /c chcp 65001>nul && C:/msys64/ucrt64/bin/gcc.exe -fdiagnostics-color=always -g C:\Users\Егор\projects\helloworld\hell.cpp -o C:\Users\Егор\projects\helloworld\hell.exe
核心错误片段:
C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/15.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\A946~1\AppData\Local\Temp\ccN4gmbj.o: in function `main': C:/Users/Егор/projects/helloworld/hell.cpp:13:(.text+0x142): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char>, std::allocator<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)' ...(大量类似的C++标准库符号未定义错误) collect2.exe: error: ld returned 1 exit status Build finished with error(s). The terminal process terminated with exit code: -1.
代码本身没有语法问题,应该是编译配置的问题,但我对VS Code的任务配置不太熟,不知道该怎么改。
我已经尝试过的操作
- 网上搜过这些错误关键词,但没找到完全匹配的解决办法;
- 重新运行MSYS2的安装命令,还是遇到同样的下载超时;
- 检查过系统环境变量,已经把MSYS2的
ucrt64/bin路径加到PATH里了。
麻烦大家帮忙分析下问题根源,最好能给出具体的操作步骤,谢谢啦!🙏




