-2

I attempted to set up my environment for C++ development and follow the hands-on practice. However, when running a simple program that prints "Hello, world," I encountered the following output: "Segmentation fault." While debugging the code, it seems that the problem lies with the "libstdc++-6.dll" file. However, I'm not sure how to resolve this issue, and I'm also uncertain if it's a common problem. Can someone assist me?

I'm using Windows 11.

#include <iostream>

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

Prompt

$ g++ hello.cpp -o hello
$ ./hello
Segmentation fault
Weather Vane
  • 33,872
  • 7
  • 36
  • 56
  • 2
    This is not a common problem. I suspect the issue lies somewhere in your "set up my environment for C++ development" step. – Drew Dormann Jul 06 '23 at 14:07
  • 2
    There is nothing wrong with your program. Your gcc installation might be broken/misconfigured. I suggest removing all traces of it from your machine (make sure no gcc.exe and no libstdc++-6.dll lurk around) and installing a fresh one using [msys2](https://www.msys2.org/) – n. m. could be an AI Jul 06 '23 at 14:08
  • Hello! I used MSYS2 to install and also executed some commands using "pacman" to check if everything is correct. The output confirms that it is, but there still might be issues with my installation? @DrewDormann – Bruno Ramos Martins Jul 06 '23 at 14:24
  • `gdb ./hello` may give clues – Ted Lyngmo Jul 06 '23 at 14:55
  • 1
    Test the same program inside the mingw64 terminal if you did not do that. I am not sure how you are running your code as there are many ways to do that. If it does not crash there you likely have [dllhell](https://en.wikipedia.org/wiki/DLL_Hell) – drescherjm Jul 06 '23 at 15:03
  • @dresserjm I ran it through git bash terminal. However, following your tip, I tried running it in PowerShell and it worked. Is it possible to fix so I can run in git bash as well? – Bruno Ramos Martins Jul 06 '23 at 15:16
  • 3
    ***Is it possible to fix so I can run in git bash as well?*** My expectation is that you have the runtime for more than 1 version of MinGW installed and that the one that is found in the `PATH` for git bash is not correct. – drescherjm Jul 06 '23 at 15:30
  • @drescherjm I have checked the path of the g++ command in PowerShell using the command: (Get-Command g++).Source, and in Git Bash using: which g++, and the paths coincide. I also tried to verify if there is any duplication in the MinGW installation or another version, but I couldn't find any. I will continue trying to make adjustments. – Bruno Ramos Martins Jul 06 '23 at 15:49

1 Answers1

2

I had the exact same problem, caused by another dll file on my path with the name libstdc++-6.dll. For me the it was because of the Julia programming language. When using Visual Studio Code, you can see what files are loaded, in my case:

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

Basically, make sure to check your path ;) Especially for other programming languages (or maybe other older versions of your C++ compiler).

Stupid
  • 96
  • 6
  • It doesn't seem like a bad answer, however, can you make it clearer? Should Julia be loaded or not? What to do if it is (or isn't)? – Ted Lyngmo Jul 06 '23 at 19:49
  • 1
    @TedLyngmo Well Julia is a completely different programming language, so the compiler loading dll files from it is a big problem. When I removed Julia from my path, my compiler started using the correct dll. My code started running smoothly :). Basically my answers is to look through your path variables and remove all those that seem potentially problematic until you get a hit. – Stupid Jul 06 '23 at 19:53
  • Good info. Please put it in the answer. – Ted Lyngmo Jul 06 '23 at 20:08