-1

I was writing my console application project and everything went correctly until I tried to run the .exe file out of CodeBlocks.

When I am trying to run my app with compiling on CodeBlocks, my program runs correctly and returns the correct result. In another way, when I am trying to run the .exe file, not by CodeBlocks, the program runs, I can type input, but before returning a final output, my console is closing.

I added all .dll files from the bin folder from MinGW to the folder with the program, but it didn't help. Do I need to add something more to this folder or do I need to change something with compiling my program? I want to make this program possible to run on another person's computer without CodeBlocks.

I am using Code::Blocks 17.12 and the C++ language.

  • *"my console is crashing"* Do you mean it closes? This is not the same thing. – HolyBlackCat Nov 20 '21 at 11:49
  • Yeah, it closes and doesn't return final output. – Loleczkowskyy Nov 20 '21 at 11:54
  • Just because a C++ programs produces correct results when it's executed one way does not guarantee that it's bug-free. That's because [undefined behavior means anything can happen](https://stackoverflow.com/questions/32132574/), which includes the program not running correctly, and crashing, when executed in some specific way. And the number of potential or possible bugs in a C++ program, of course, is unlimited. And without showing a [mre], it is unlikely that anyone will be able to tell you anything more. – Sam Varshavchik Nov 20 '21 at 12:02
  • 1
    Code::Blocks attaches a debugger to the console window which (optionally) keeps the console window visible after the program terminates. Running the program directly (e.g. by clicking on a shortcut to the executable) does not attach a debugger to the console window, so the console window closes immediately when your program terminates. Running a production program with debugger attached is not a good idea, for various reasons. A common technique is for the program to flush console output and then read input after all output is produced e.g. immediately before `main()` returns. – Peter Nov 20 '21 at 12:12
  • *"Yeah, it closes and doesn't return final output."* It's not a crash then. The program probably finishes and closes normally. To see the final output, either run it from the console, or read some dummy input after printing, or add `std::system("pause");` after printing (Windows-only). – HolyBlackCat Nov 20 '21 at 12:15

1 Answers1

1

Add a getchar() or system("pause") at the end of your program. The window closes because the program has ended executing, console applications are normally run from within a terminal, where you can still see your program output after it finished.

simondvt
  • 324
  • 3
  • 13