c++visual-studio-codeg++

C++ Segmentation fault on Hello World, cin and cout


I have been trying to get back into C++ after a break of 2 years. On the first day, things went fine. But when I came back the day after, none of my projects containing cout would compile.

The error is:

Exception has occurred. Segmentation fault

The debug console gives me:

Thread 1 received signal SIGSEGV, Segmentation fault.
0x00007ffd9109f436 in std::basic_ostream<char, std::char_traits<char> >::sentry::sentry (this=this@entry=0x5ffda0, __os=...) at /workspace/srcdir/gcc_build/x86_64-w64-mingw32/libstdc++-v3/include/bits/ostream.tcc:51

This is an example of my code where this happens:

#include <iostream>

using namespace std;

int main()
{
    string abc = "a";
    std::cout << "hi";

    return 0;
}

As mentioned, the error happens on std::cout, std::string works fine. There is no difference if I use the namespace std and then only write cout, or if I don't use the namespace and write the whole thing.

The same issue happens if I try to use cin >> abc;

I am using Visual Studio Code. I don't know if it is related, but I'm not getting a c_cpp_properties.json file in my .vscode folder. I am, however, getting the rest, and I do have the c++ addon.

This is on Windows using g++.

I have tried:

I hope any of you smarter than me can figure out what's wrong.

EDIT: I found the problem, as assumed it was a libstdc++-6.dll file:

Loaded 'C:\Users\xxxx\AppData\Local\Programs\Julia-1.8.2\bin\libstdc++-6.dll'. Symbols loaded.

So yeah, feel pretty stupid, but problem solved.


Solution

  • The problem was a Julia libstdc++-6.dll file that was being loaded before the mingw64 one. If you have a similar problem, make sure to comb through your PATH variables. The Visual Studio Code debug console also tells you what files are being loaded.

    BAD:

    Loaded 'C:\Users\xxxx\AppData\Local\Programs\Julia-1.8.2\bin\libstdc++-6.dll'. Symbols loaded.
    

    GOOD:

    Loaded 'C:\msys64\mingw64\bin\libstdc++-6.dll'. Symbols loaded.