0

I am having difficulty running the following program:

#include <string>
#include <iostream>

using namespace std;

int main()
{
    cout << "First line.";
    string s = "Hello World";
    cout << " After string.";
    return 0;
}

It compiles fine, but produces no output when run.

If I comment out the string s = "Hello World"; line, then the program runs and outputs First Line. After string. as expected.

It seems that whenever I try to declare a string, the program halts. This also happens when the string is inside a function which is not called.

I am building the program in VSCode on Windows and my compiler and version are: g++ (x86_64-posix-sjlj-rev0, Built by MinGW-W64 project) 8.1.0

  • 1
    [Seems to work fine](https://godbolt.org/z/bq5YnnPrb). – François Andrieux May 02 '22 at 22:07
  • I'm sure I've seen a question like this somewhere before. Well, there was [this question](https://stackoverflow.com/q/15843820/103167) but that has a different cause. – Ben Voigt May 02 '22 at 22:09
  • @FrançoisAndrieux, Yes, it _should_ work and that's why I want help figuring out why it isn't. – Michael Barrowman May 02 '22 at 22:10
  • 1
    It's buffered. Use `std::flush` – Sam Varshavchik May 02 '22 at 22:11
  • 3
    @SamVarshavchik: All buffers should flush when `main()` returns and the process exits. – Ben Voigt May 02 '22 at 22:11
  • How are you compiling and running the code? – ChrisMM May 02 '22 at 22:12
  • 1
    @MichaelBarrowman When you say "whenever I try to declare a string, the program halts", do you mean it hangs and does not exit? Or it exits without output? (Both meanings of "HALT" have been used in computer context, consider the Halting Problem, where halt means completes and exits, and also the Halt-and-Catch-Fire instruction, where "HALT" meant the processor goes to sleep and nothing completes) – Ben Voigt May 02 '22 at 22:12
  • @ChrisMM, I'm using `g++` to compile into an `.exe` and running that in _command line_. @BenVoigt, it exits with no output. – Michael Barrowman May 02 '22 at 22:13
  • 2
    If you run the compiled .exe from the explorer with a double-click, does it show any errors? If yes, it's probably a dll-related problem (see [this thread](https://stackoverflow.com/questions/64396979/how-do-i-use-sdl2-in-my-programs-correctly), starting from *"For ntldd algorithm is ..."*). – HolyBlackCat May 02 '22 at 22:16

1 Answers1

6

I was able to get to the bottom of the problem thanks to @HolyBlackCat's comment, so I will post the answer here for anybody with the same problem in the future.

I ran the .exe through Windows Explorer (double clicking), rather than through command line, and an error window popped up saying:

the procedure entry point __gxx_personality_sj0 could not be located in the dynamic link library

Once I knew the error I was looking for, I found this SO post:

The procedure entry point __gxx_personality_sj0 could not be located in...

As the other post suggests, I ran the libstdc++-6.dll in my command prompt, and another error window popped open saying that the file didn't have an app associated with it. However, the title bar of that window gave me the directory to the incorrect libstdc++-6.dll file.

To solve the problem, I moved the mingw64 directory to the top of my PATH environment variable, so that the libstdc++-6.dll file in that directory would be used first when compiling.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770