No console application on Linux

Viewed 844

On Windows I normally create a Windows Desktop Application, this is because console applications display a brief black box on the screen.

I am using CodeBlocks on Linux Mint, how could I do the equivalent of the above but on Linux?

I do not want to hide the terminal window after it has been displayed.

3 Answers

Linux does not have the same "subsystem" concept as windows: there is no difference or separation between console and desktop applications. When you start an application on Linux, it will not open a console window, unless the programmer explicitly programmed it to open one.

If the application writes anything to stdout or stderr, what happens with that depends on how exactly the application got started. By default, the application inherits the stdout and stderr of its parent process. If the application is being started from a terminal, the output will be visible on the terminal. If the application was started by the desktop environment from a menu entry, the output may go to a log file or it may be lost.

If you see a terminal window open when you run your program from the IDE, that's something that the IDE is doing for you, it's not your application. If it bothers your, I would think that the IDE has a way to disable this behavior in settings.

Look into QT. It is a GUI framework that works on Linux.

You can write your code without creating a main window (or maybe you have to have a main window but it can be always hidden... it's been a while since I've used it).

Be aware though, that you may run into usability issues with this type of design... the user has no way of knowing if your app was launched or if it succeeded, when it's completed, etc.

The simple way is to use (e.g.) xterm [or gnome-terminal] to get a terminal window.

Then, invoke your program from the shell [manually]:

/path_to_my_program

You may be able to configure codeblocks to do this for you.

Or, you could add some code that holds the default window open:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

int main()
{

    pid_t pid = fork();

    if (pid != 0) {
        waitpid(pid,NULL,0);
        while (1) sleep(1);
    }

    long double n;
    n=5;
    printf("n= %Lf\n",n);

    return 0;
}

UPDATE:

The invocation command can be controlled from: Settings -> Environment -> General Settings

The default is to invoke in an xterm sub-window [popup]. You may be able to change the settings to (re)use an existing [terminal] window.

Note that [a codeblocks program] cb_console_runner is used. You may be able to replace this with something more to your liking.

I do not want a GUI nor a terminal popup...

You'll need some sort of terminal to run the command in. This could be a script that diverts stdin/stdout/stderr as appropriate [and suppresses the invocation of a sub-window], so you'll have to experiment a bit.

As I mentioned above, you can just open a terminal window outside of codeblocks and then run the command manually inside it. Technically, this is not a popup. But, you lose the [automatic] debugger invocation.

Related