debugging VSCode中的简单hello world段错误运行C/C++文件

wljmcqd8  于 2023-10-24  发布在  Vscode
关注(0)|答案(1)|浏览(175)

我试图在VSCode中运行一个简单的Hello World程序,但它因分段错误而崩溃。下面是代码:

#include <iostream>

int main() {
  std::cout << "Hello, World!" << std::endl; 
  return 0;
}

当我单击Run C/C++ File时,我在Windows Console中得到以下输出:

Thread 1 hit Breakpoint 1, 0x00007ff7cf5b145d in main ()
Loaded 'C:\WINDOWS\SYSTEM32\ntdll.dll'. Symbols loaded.
Loaded 'C:\WINDOWS\System32\kernel32.dll'. Symbols loaded.
Loaded 'C:\WINDOWS\System32\KernelBase.dll'. Symbols loaded.
Loaded 'C:\WINDOWS\System32\msvcrt.dll'. Symbols loaded.
Loaded 'C:\Programs\Git\mingw64\bin\libstdc++-6.dll'. Symbols loaded.
Loaded 'C:\Programs\Git\mingw64\bin\libwinpthread-1.dll'. Symbols loaded.
Loaded 'C:\Programs\Git\mingw64\bin\libgcc_s_seh-1.dll'. Symbols loaded.

Thread 1 received signal SIGSEGV, Segmentation fault.
0x00007fff49f3c226 in ?? () from C:\Programs\Git\mingw64\bin\libstdc++-6.dll

我已经尝试过更新VSCode和C扩展,重新启动,检查启动配置等解决方案,但到目前为止还没有解决这个问题。
有人能帮助我理解和修复这个segfault时,运行一个简单的C
程序在VSCode?

**编辑:**看起来调试器试图从C:\Programs\Git\mingw64\bin\加载符号。我不确定为什么调试器从这个路径加载符号。我没有在JSON配置中包含它。
**编辑:**实际上,这个问题发生在我做OpenGL任务的时候。一个简单的cout导致整个程序在正常运行之前就崩溃了。我已经尝试在这里包含一个最小可重现的例子。

以下是.vscode文件夹中的JSON配置:
launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": ["4"],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "preLaunchTask": "${defaultBuildTask}",
            "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

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

c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "C:/msys64/mingw64/bin/g++.exe",
            "cStandard": "c11",
            "cppStandard": "gnu++14",
            "intelliSenseMode": "windows-gcc-x64"
        }
    ],
    "version": 4
}
bxpogfeg

bxpogfeg1#

看起来我终于解决了这个问题。我把C:\Programs\Git\mingw64重命名为C:\Programs\Git\mingw64ABC,然后编译我的程序,它运行得很好。然后我把mingw64ABC恢复到它原来的名字,现在一切都正常工作,正如预期的那样。我不确定Windows是否在内部某处缓存了符号搜索路径,这可能是罪魁祸首。我推测一旦操作系统有缓存缺失,它从列表中删除了该路径,并添加了正确的搜索路径C:/msys64/mingw64/bin/

相关问题