You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VS Code中运行简单Hello World C++程序出现段错误求助

MinGW + VS Code 输出cout/cin时触发段错误问题

近期通过MSYS2安装了MinGW,测试以下C++程序:

#include<iostream>

using namespace std; 

int main() {
  cout << "Hello World";
  return 0 ;
}

该程序在CodeBlocks和MSYS2终端中均可正常运行,但在VS Code中运行时会触发段错误。只要代码包含cout或cin语句,就会出现这个问题。已执行pacman -Syuu更新MSYS2所有包,问题仍未解决。

以下是当前的launch.json和tasks.json配置:

launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": " ${workspaceFolder}/${fileBasenameNoExtension}.exe",
            // "sourceFileMap": {
            //     "/mnt/c": "C:/msys64/mingw64"
            // },

            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "C:/msys64/mingw64/bin/gdb.exe ",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "Set Disassembly Flavor to Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ]
        },
    ]
}

tasks.json:

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe build active file",
            "command": "C:\\msys64\\mingw64\\bin\\g++.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

调用栈截图:
调用栈信息

内容的提问来源于stack exchange,提问作者akashKP

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 12:07:34