Hello, I have a console program in C, it compiles well in Ubuntu but when I open it it does not generate the console window, I have to open it from the console to be able to achieve this
What should I do so that the program opens automatically from the console?
[EDITED] More detailed information:
example.c
#include <stdio.h>
int main(int argc, char const *argv[]) {
printf("Hola Mundo");
getchar();
return 0;
}
gcc -o example example.c
a file is generated that when I double-click it, the terminal does not open to show me the message, now if I manually open the terminal and go to the program directory and execute it:
./example
This is running correctly showing me all the information and respecting the getchar();
In short: you can't .
On Windows, when compiling the program, you indicate whether it is in graphical mode or in console mode (if you don't, the compiler does it implicitly). If it's a console program, the system creates a virtual console , which is the window you see, for your program to interact with.
In non-Windows environments, such as *nix, things don't work like that. There is no distinction; they are all programs, which interact with standard input and output. These input/output mechanisms are not always physical ; a program can create a virtual console, and pass it as
STDIN
orSTDOUT
to other child programs .That is the mechanism used by terminal emulators. They create a child process , calling the shell. When you run a program under that shell, this other program inherits
STDIN
ySTDOUT
from the shell that launched it, which in turn inherits it from the program that created it, which, as I said, is the terminal application.That mechanism makes it different from auto-creating a terminal window to display your results in. You can call the emulator, passing it an argument, which would be your own program to run; from your own program, you have to differentiate if you have self-invoked , or have been called directly. You could use directory , or create some kind of lock
/proc
file , or a network socket , or use DBUS , ...Final Tip: Unless you need to create your own terminal for something, it's much easier to launch programs from the system-provided terminal emulator.
You have to make use of as
system()
far as I know.For example if your OS has LXTerminal you could use something like this:
or
gnome-terminal
basically notice what is passed tocmd
the fixed command to open the console viasystem()
, in this casegnome-terminal
.Now you would have to know what console your OS uses and what is its command to open it, then adapt the code to your program.
Perhaps with the previous example it is not very clear to you, I will give you an idea, but take it as pseudocode . It's just to illustrate:
info : you can look to create a more decent filelock here
let's say the programs do not open with double click and you will have the terminal order XD
Better put:
nano mi_programita.desktop
And you put something like:
And you just give it executable permissions:
You can still put it in
/usr/share/applications
so that it appears in the menu and put the argumentIcon=/ruta/del/icono.extension
to put an icon on it, it is the best you can do, you must pause the program at the end, otherwise: you will not be able to see the application.NOTE: if you don't use arguments it's better not to use
int argc, char const *argv[]
enint main(...)
and leave it asint main() {