使用MinGW编译C++代码时出现undefined reference构建错误
问题:C++编译失败,链接器报错
__chkstk_ms和__emutls_get_address 刚从Python转学C++,运行以下代码时构建失败,已安装MinGW编译器,可能是新手错误。
main.cpp代码
int main(){ std::cout << "Hello world"; return 0; }
tasks.json配置
{ "tasks": [ { "type": "cppbuild", "label": "C/C++: g++.exe build active file", "command": "C:\\MinGW\\bin\\g++.exe", "args": [ "-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe" ], "options": { "cwd": "C:\\MinGW\\bin" }, "problemMatcher": [ "$gcc" ], "group": { "kind": "build", "isDefault": true }, "detail": "Task generated by Debugger." } ], "version": "2.0.0" }
错误信息
Starting build... C:\MinGW\bin\g++.exe -g "C:\Users\{__pathToFile__}\main.cpp" -o "C:\Users\{__pathToFile__}\main.exe" C:\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\mingw32\bin\ld.exe: /mingw/lib/libmingw32.a(setargv.o):(.text+0x48): undefined reference to `__chkstk_ms' C:\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\mingw32\bin\ld.exe: /mingw/lib/libmingwex.a(glob.o):(.text+0x736): undefined reference to `__chkstk_ms' C:\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\mingw32\bin\ld.exe: /mingw/lib/libmingwex.a(glob.o):(.text+0x7a4): undefined reference to `__chkstk_ms' C:\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\mingw32\bin\ld.exe: /mingw/lib/libmingwex.a(glob.o):(.text+0x86f): undefined reference to `__chkstk_ms' C:\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\mingw32\bin\ld.exe: /mingw/lib/libmingwex.a(glob.o):(.text+0xcab): undefined reference to `__chkstk_ms' C:\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\mingw32\bin\ld.exe: /mingw/lib/libmingwex.a(glob.o):(.text+0xcf2): more undefined references to `__chkstk_ms' follow C:\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\mingw32\bin\ld.exe: /mingw/lib/libmingwex.a(mbrscan.o):(.text+0x13): undefined reference to `__emutls_get_address' C:\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\mingw32\bin\ld.exe: /mingw/lib/libmingwex.a(mbrscan.o):(.text+0x3a): undefined reference to `__emutls_get_address' C:\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\mingw32\bin\ld.exe: /mingw/lib/libmingwex.a(mbrscan.o):(.text+0x5b): undefined reference to `__emutls_get_address' C:\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\mingw32\bin\ld.exe: /mingw/lib/libmingwex.a(mbrscan.o):(.text+0x91): undefined reference to `__emutls_get_address' C:\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\mingw32\bin\ld.exe: /mingw/lib/libmingwex.a(mbrscan.o):(.text+0x16e): undefined reference to `__emutls_get_address' C:\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\mingw32\bin\ld.exe: /mingw/lib/libmingwex.a(mbrscan.o):(.text+0x3d8): more undefined references to `__emutls_get_address' follow C:\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\mingw32\bin\ld.exe: /mingw/lib/libmingwex.a(codeset.o):(.text+0x40): undefined reference to `__chkstk_ms' collect2: ld returned 1 exit status Build finished with error(s)
解决方案
1. 补全代码缺失的头文件
std::cout属于标准输入输出库,必须包含<iostream>头文件才能使用,修改后的代码:
#include <iostream> int main(){ std::cout << "Hello world"; return 0; }
2. 升级MinGW版本
从错误信息能看到你用的是gcc 3.4.5,这个版本是2006年的旧版本,对现代C++支持差,还会出现链接符号缺失的问题。解决步骤:
- 卸载当前旧版MinGW
- 下载最新的MinGW-w64发行版(注意是MinGW-w64,不是旧版MinGW),可以直接官网下载预编译包,或者用MSYS2工具安装(更方便后续更新)
- 安装后确保
g++.exe所在路径添加到系统环境变量PATH中,或者在tasks.json里更新为新的路径
3. 验证编译器版本
打开命令提示符,输入以下命令确认版本:
g++ --version
显示版本在8.x以上(推荐10.x及更高)即为正常。
4. 调整tasks.json(可选)
如果更换了MinGW-w64,更新tasks.json里的command路径,示例:
"command": "C:\\mingw-w64\\x86_64-12.2.0-posix-seh-rt_v10-rev0\\mingw64\\bin\\g++.exe"
路径根据你的实际安装位置调整。
内容的提问来源于stack exchange,提问作者ilias
相关产品推荐
相关产品推荐

