I am trying to run a simple Hello World program in VSCode, but it is crashing with a segmentation fault. Here is the code:
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
When I click Run C/C++ File, I get this output in the Debug 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
I have tried solutions like updating VSCode and C++ extensions, restarting, checking launch config, but nothing has resolved the issue so far.
Can someone please help me understand and fix this segfault when running a simple C++ program in VSCode?
EDIT: It looks like the debugger is trying to load symbols from C:\Programs\Git\mingw64\bin\
. I am not sure why the debugger is loading symbols from this path. I have not included it anywhere in the JSON configurations.
EDIT: Actually, this problem occurred when I was working on my OpenGL assignment. A simple cout
caused the whole program to crash even before it ran properly. I have tried to include a minimum reproducible example here.
Here are the JSON configurations in .vscode
folder:
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
}
Looks like I finally managed to solve the problem. I renamed C:\Programs\Git\mingw64
to C:\Programs\Git\mingw64ABC
, then compiled my program and it ran fine. Then I restored mingw64ABC
to its original name and now everything is working correctly as expected. I am not sure whether Windows caches the symbol search paths internally somewhere, that might be the culprit. I am speculating that once the OS had a cache miss, it removed that path from the list and appended the correct search path C:/msys64/mingw64/bin/
.