c++ 简单的“Hello World”程序在VS代码中给出了分段错误

ugmeyewa  于 2023-07-01  发布在  其他
关注(0)|答案(1)|浏览(131)

我最近从msys2安装了mingw,并测试了下面的程序:

#include<iostream>

using namespace std; 

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

但是我在VS代码中得到了一个分段错误。
相同的代码在CodeBlocks(edit)和mingw终端中运行良好。
似乎每当有cout或cin语句时,我都会得到错误。
我确实做了pacman -Syuu来更新msys2包,但没有用。
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"
}

谢谢

w7t8yxp5

w7t8yxp51#

经过大量徒劳的尝试来修复错误,我设法解决了这个问题,通过将mingw\bin的路径移动到用户和系统变量路径列表中环境变量的顶部。
问题的根源是我有多个不同应用程序的libstdc++-6.dll文件,其路径高于msys2 libstdc++-6.dll的路径,这导致调用了错误的库版本。

相关问题