5

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.

securityauditor
  • 293
  • 3
  • 13
  • 3
    On Linux there are no "console applications" as such, in that applications do not (for the most part) open console windows for themselves. There is no general-purpose application property that causes any such thing to happen. There *are* plenty of applications that expect to use their standard streams for their primary UI, but that has nothing to do with displaying windows of any kind on any screen. Quite the opposite, in fact. – John Bollinger Jul 12 '20 at 21:38
  • 1
    I think you just want to run your program from a terminal. – drescherjm Jul 12 '20 at 21:40
  • 1
    @JohnBollinger I think what he's seeing is double-clicking on a "vanilla" C++ executable from a windowing system in Linux.... that will generally open a console window... this is quite similar to double-clicking on a "console" application in Windows. QT has some kind of magic to prevent the console window from opening IIRC. – JoelFan Jul 12 '20 at 21:41
  • On Linux you either launch through the GUI, in which case there is not supposed to be any sort of console window, or you launch from the console itself, in which case there can be some direct console output in the **shell and terminal program of the user's choice**. – tadman Jul 12 '20 at 21:45
  • @tadman So if I do ./myProgram or double click the file, how can I make sure nothing appears on the screen, whether it be a terminal or GUI? – securityauditor Jul 12 '20 at 22:22
  • @securityauditor Did you try to execute the program that way to see if it is a problem? – klutt Jul 12 '20 at 23:07

3 Answers3

9

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.

Joni
  • 108,737
  • 14
  • 143
  • 193
  • It's not just the IDE.... this also happens from Gnome, KDE, etc. – JoelFan Jul 12 '20 at 21:44
  • 1
    Site note: If a Linux application *did* somehow open a terminal window that would be very strange and annoying. – tadman Jul 12 '20 at 21:44
  • 2
    @JoelFan what do you mean? If clicking on a Gnome/KDE system menu entry opens an application in a terminal emulator, it's because the menu entry is configured to open a terminal emulator. This is controlled by the `Terminal` setting in the .desktop file https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html – Joni Jul 12 '20 at 21:48
  • @Joni, I'm talking about double-clicking on the executable file directly – JoelFan Jul 12 '20 at 21:50
4

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.

JoelFan
  • 37,465
  • 35
  • 132
  • 205
3

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.

Craig Estey
  • 30,627
  • 4
  • 24
  • 48