1

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:

  • Rebooting
  • Uninstalling and reinstalling g++
  • Creating new files in new areas
  • Checking my path for problematic files.
  • Googling the issue, found nothing that seemed relevant

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.

Stupid
  • 96
  • 6
  • 1
    Did you follow the VSCode instructions that tell you to install msys2 and have that install MinGW. If you did this test your code in the mingw64 terminal. If you did not follow the official instructions please do so. They are here: [https://code.visualstudio.com/docs/cpp/config-mingw](https://code.visualstudio.com/docs/cpp/config-mingw) – drescherjm Jul 06 '23 at 19:07
  • At a guess a different version of libstdc++ is on your path to the version that you're compiling against – Alan Birtles Jul 06 '23 at 19:07
  • 3
    see a very similar question here for more suggestions https://stackoverflow.com/questions/76629649/segmentation-fault-libstdc-6-dll – Alan Birtles Jul 06 '23 at 19:08
  • @drescherjm Thank you for your quick response. I have reinstalled MINGW64 from scratch using that guide, but it did not change anything. How do I test code directly in the mingw64 terminal? – Stupid Jul 06 '23 at 19:17
  • @AlanBirtles Thank you for the link, looks like im not the only one with this issue then (:, I have tried looking through my path and even removes some folders, but sadly got no hits. – Stupid Jul 06 '23 at 19:23
  • ***How do I test code directly in the mingw64 terminal?*** Run this program: `"C:\msys64\mingw64.exe"` you should have a folder inside `C:\msys64\home` for your user. Inside that folder you can use explorer to copy and paste your cpp file. Then type `g++ cppfilename -o exefilename` where cppfilename is the name of the file you copied and exefilename is the name of the executable output. After that type `./exefilename` using the exefilename you named in the last step. – drescherjm Jul 06 '23 at 19:29
  • @drescherjm I tried running my code through this terminal and it worked! Thanks :D I guess that means the problem is with Visual Studio Code. The .exe file that is created does however not work when i open it myself. It instantly closes (even with a cin at the end to wait for input). – Stupid Jul 06 '23 at 19:35
  • ***The .exe file that is created does however not work when i open it myself.*** The some dlls from `C:\msys64\mingw64\bin` need to be in a folder of your PATH environment variable or the same folder as the exe. If you type `ldd exefilename` in the mingw64 terminal it will tell you the required dlls. For a simple program its probably: `libstdc++-6.dll, libwinpthread-1.dll, and libgcc_s_seh-1.dll` – drescherjm Jul 06 '23 at 19:51
  • 1
    If you have multiple toolchains present in the system path, you're always at risk that one you don't want will be found before the one you do. – user4581301 Jul 06 '23 at 19:51
  • 1
    @drescherjm I figured it out, I had another libstdc++-6.dll file in my path from the Julia programming language, after removing it the exe works. Thanks for all the help :) – Stupid Jul 06 '23 at 19:58
  • @user4581301s comment is important. You likely have more than 1 folder containing the runtime for MinGW listed in the folders of your `PATH` environment variable. You may want to check that out. – drescherjm Jul 06 '23 at 19:58
  • Great! I was typing my last comment before I saw your last one.. – drescherjm Jul 06 '23 at 19:59
  • @user4581301 I guess having multiple languages on path is a recipe for disaster, I just feel like it should be possible for them to coexist... – Stupid Jul 06 '23 at 19:59
  • 1
    This is called [dll hell](https://en.wikipedia.org/wiki/DLL_Hell) specifically you had incompatible versions of the same dll in your PATH and windows found the incompatible one first. – drescherjm Jul 06 '23 at 20:00
  • 1
    For me it's particularly bad. I maintain a lot of legacy systems, so I've usually got around a dozen installs of GCC alone. The solution is forgo the ease of configuration by dumping tools into the system path and instead explicitly configure everything that depends on the tools, IDEs etc..., with full paths. – user4581301 Jul 06 '23 at 20:03
  • This may be a question we can add an answer since its asked in a good way. However I suspect there is a reasonable duplicate. I need to get back to debugging my Qt code.. – drescherjm Jul 06 '23 at 20:05
  • Now-a-days I tend to have a VM built up for each of the maintenance jobs. Makes a nice, hard wall between all of the different tools and really speeds things up when I have to migrate to a new computer. – user4581301 Jul 06 '23 at 20:05
  • 1
    I can see why you call it hell, wasted a couple of hours on 2 files having the same name... I'll let you get back to work and write an answer and accept it myself 8) (when my cooldown is over) – Stupid Jul 06 '23 at 20:08
  • @user4581301 sounds like a pain, must be alot of VM's, but if it works, it works (: – Stupid Jul 06 '23 at 20:14

2 Answers2

5

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.
Stupid
  • 96
  • 6
1

It's a problem with the VS Code executable.

To slove it for Linux you should run/test it from the terminal.

Linux:

  1. (Optional if not already installed) Install g++ in the terminal using the command:
      Ubuntu/Debian: sudo apt update then sudo apt install g++
      Fedora/Red Hat: sudo yum install g++
      Arch Linux: pacman -Syu then pacman -S gcc
  2. Go into your folder and then compile the script using: g++ -o myfile myfile.cpp
  3. Run it using ./myfile

To slove it for Windows you should run/test it from a MinGW enviorment (easiest fix).

Windows:

  1. Download MSYS2 from here.
  2. Run the installer. MSYS2 requires 64 bit Windows 8.1 or newer.
  3. Enter the Installation Folder (I recommend leaving it default).
  4. When done, uncheck option Run MSY2S now, and then click Finsh (or just close the window).
  5. Search the program MSYS2 MINGW64 and open it.
  6. Now you are in Linux terminal for MinGW (this is a mini Arch Linux).
  7. Open in File Explorer the location where you installed it (default C:\msys64\)
  8. Now enter in the home folder (there should be your user's folder) then enter in your user's folder.
  9. Here is the root folder of the terminal window (basically your workspace).
  10. Now you can make your files/folders and they can be found from the terminal. Also you can open VS Code in this location and develop your apps here.
  11. Install g++ using pacman -S gcc
  12. After you are done use the command in the terminal g++ -o myfile myfile.cpp
  13. And run it with ./myfile

Hope that I helped you!

Edit: didn't see you solved it. I was writing it while you solved the problem.

McWhopper
  • 29
  • 7
  • I guess now it is for others that can't solve it. – McWhopper Jul 06 '23 at 20:42
  • Thank you for the in depth answer still! Does not treat the root problem, but definetly a solution when you can't just remove the problem from your path :) – Stupid Jul 06 '23 at 21:07