Code::blocks closes immediately after finishing the main() function.

Hi forum!
I hope i set this thread in the correct category.

I'm using code::blocks as my IDE in FreeBSD 14.2 STABLE, and when i run code in it, let's say this one:

C++:
#include <iostream>

using namespace std;

int main()
{
   cout << "Enter a number: ";
   int x{};
   cin >> x;

   cout << "Your number plus 2 is: " << x + 2 << "! \n";
   return 0;
}

The code executes correctly, but i can't see the last "cout" output since the terminal window closes too fast, almost immediately.

I've used code::blocks in Debian and Arch and the terminal window wouldn't close immediately, but keep open so you can see the final output of your code.
That made me think this is not an expected behavior, maybe?

I've tried different solutions provided in forums as having the "Pause when execution ends" option toggled on (it is by default).

A temporary fix is adding a "Insert any number to close..." type of function just before the return statement.
And i saw a user saying that is expected that code::blocks closes that way if you don't insert a method to make the program wait by yourself.
But i didn't need that before, and the terminal window just kept open after finishing my code execution in GNU/Linux distros.

Is this a bug or common behavior in FreeBSD? Or maybe am i missing some configuration settings?
 
I hope i set this thread in the correct category.
No, but that's ok. I'm going to move this to "Userland programming and scripting" as I think this is more about using the software (for programming, it's an IDE after all) than installing/updating the port/package of this software.
 
No, but that's ok. I'm going to move this to "Userland programming and scripting" as I think this is more about using the software (for programming, it's an IDE after all) than installing/updating the port/package of this software.
Thanks! At this point i'm not sure what categories should i use in cases where i need help with any particular pieces of software. Should i post that kind of posts in "Userland programming and scripting" too?
 
At this point I'm not sure what categories should i use in cases where i need help with any particular pieces of software.
It's sometimes tricky, there are also cases where a certain issue could fit in multiple sections. I always try to look at the source of the issue to determine where to put things, but that's not always clear from a single post. In any case, don't fret too much about it, I'm constantly moving threads around if I think the post will get better traction in a different section.

Should i post that kind of posts in "Userland programming and scripting" too?
If that piece of software is related to programming, sure.
 
ok, so now that the "what forum does it go in" is out of the way...I think your best solution is to do what you hinted at earlier: insert a final cin >> x just before the return. making an xterm pause before termination ivolves some shell trickery that may no be worth the time for you to get working.

Incidentally, if your IDE provides a macro when running in the environment then you could test that macro and only include the final cin when appropriate.
 
the terminal window
What's the terminal window? Something from codeblocks itself or an 'external' terminal like Xterm?

as having the "Pause when execution ends" option toggled on (it is by default).
Right, don't use codeblocks myself. Is this some option you can set in the IDE? Then it sounds like a bug.

And i saw a user saying that is expected that code::blocks closes that way if you don't insert a method to make the program wait by yourself.
Yes, that's what I would expect to happen too. The 'basic' X user terminal typically ends (and thus closes) when the process ends. Terminal window open -> process running -> process exit 0 (not fail) -> terminal window close.

The bug might be in codeblocks itself, I see it's unmaintained, and the version in the ports tree is a version behind upstream.
 
What's the terminal window? Something from codeblocks itself or an 'external' terminal like Xterm?
The terminal window is 'external', Alacritty, specifically.

Right, don't use codeblocks myself. Is this some option you can set in the IDE? Then it sounds like a bug.
Yeah, it's an option you can set in the IDE.
I will append a screenshot of the option toggle.

The bug might be in codeblocks itself, I see it's unmaintained, and the version in the ports tree is a version behind upstream.
I saw that too... Do you guys know about a maintained IDE that provides a similar set of tools to codeblocks?
I know you can use Emacs or Vim, or Geany, but since I'm still learning how to program, i would prefer something not so different to the codeblocks GUI.
 

Attachments

  • Wed Apr  9 10:39:36 -04 2025.jpg
    Wed Apr 9 10:39:36 -04 2025.jpg
    4 KB · Views: 18
Back
Top