VSCode Not Displaying Segfault

I’m running C++17 on VSCode with gcc. When I have code that should segfault, for example:

#include <iostream>
#include <stack>
using namespace std;

stack<int> s;

int main() {
    cout << s.top();
}

VSCode does not display “command terminated by signal 11” in terminal, and it does not highlight the line where it segfaults like it is supposed to. How do I fix this?

I don’t think it’s supposed to… I just ran your code and I also use VS Code. For me, the program just terminated. I believe this is normal behavior for GCC. One way you can get the segmentation fault message is by running the executable with gdb as follows:

gdb ./executable_name

Then, type r or run.

Hope I’m not too late to the discussion, what I do to debug vscode is I setup a run configuration to run a.exe (or the equavilent default output for gcc on your os), then set a breakpoint in vscode. After that when I compile with g++ I use the -g file (this allows vscode to map the executable to the source file I think). Then I go to Run → Start with debugging and the program should start and break on the breakpoint.

I believe you also need the c++ extension for this to work (I’m not 100% sure this is the right one due to myself having two other c++ extensions enabled).