Open compiled program in terminal

5

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 in order to achieve this.

What should I do to have the program open 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 -or sample example.c

I am generated a file that by double clicking the terminal does not open to show me the message, now if I manually open the terminal and I go to the directory of the program and execute it:

  

./ example

This is running correctly showing me all the information and respecting the getchar ();

    
asked by Cristofer Fuentes 28.02.2017 в 17:06
source

3 answers

2

You have to use system() as far as I know.

For example if your OS has LXTerminal you could use something like this:

#include <string.h>

int main()
{
    char cmd[50];

    strcpy(cmd,"lxterminal");
    system(cmd);

  return 0;
}

or gnome-terminal basically notice that the command set to open the console is passed to cmd through system() , in this case gnome-terminal .

 #include <string.h>

 int main()
 {
    char cmd[50];

    strcpy(cmd,"gnome-terminal");
    system(cmd);

  return 0;
 }

Now I would have to know which console uses your OS and what is your command to open, then adapt the code to your program.

Maybe with the previous example I am not very clear I will leave you an idea, but take it as pseudocode . It's just to illustrate:

#include <stdio.h>
#include <string.h>

#include<unistd.h>

int main()
{

    FILE *fptr;
    fptr = fopen("lock.dat", "rb+");  //simulamos un filelock

    if(fptr == NULL)
    {
        fptr = fopen("lock.dat", "wb"); //simulamos un filelock
        char cmd[50];

        //supongamos que su programa compilado se llama a.out, y se lo pasamos.    
        strcpy(cmd,"lxterminal -e \"./a.out\""); 
        system(cmd);


    }else{

        sleep(5);
        int a = 0;

        //test para que vea algo en la pantalla y no se cierre ect
        for (a = 0; a < 10000; a++){ 
            printf("Hola Mundo");
        }
        sleep(5);

        fclose(fptr);

        remove("lock.dat");
  }
  // Antes de terminar su programa tendría que desbloquear el filelock, si usa 
  // un fichero tiene que borrar este, para poder volver a lanzar su programa 
  // mas abajo dejo un link sobre filelock o tambien puede usar google
  return 0;
}

info: you can look to create a more decent filelock here

    
answered by 28.02.2017 / 18:43
source
8

In short: you can not .

In Windows, when compiling the program, indicate if it is in graphic mode or in console mode (if you do not do it, the compiler does it implicitly). If it is a program for console, the system creates a virtual console , which is the window that you see, so that your program interacts with it.

In non-Windows environments, such as * nix, things do not work that way. No there is distinction; all are programs, which interact with the standard input and output. These input / output mechanisms are not always physical ; a program can create a virtual console, and pass it as STDIN or STDOUT to other programs children .

That's the mechanism used by terminal emulators. Create a child process by calling the command interpreter. When you run a program under that interpreter, this other program inherits STDIN and STDOUT of the interpreter 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 different the auto-create a terminal window to show your results in it. You can invoke the emulator, passing an argument to it, which would be your own program to execute; from your own program, you have to differentiate if you have auto-invoked , or have been called directly. You could use the /proc directory, or create some type of bolt file, or a socket network, or use DBUS , ...

Final advice: unless you need to create your own terminal for something, it is much easier to launch the programs from the terminal emulator provided by the system.

    
answered by 28.02.2017 в 18:58
1

Let's see that the programs do not open with a double click and you will have the terminal order XD

Better put: nano mi_programita.desktop

And you put something like:

[Desktop Entry]
Version=0.1
Name=Mi Programa
Comment=Mi programa
Exec=/ruta/de/tu/programa
Terminal=true
Type=Application

And you just give executable permissions:

  

chmod + x my_program.desktop

Like you can put it in /usr/share/applications to come out in the menu and put the argument Icon=/ruta/del/icono.extension to put an icon, is the best you can do, you must pause the program at the end if not: you can not see the application.

NOTE: if you do not use arguments it is better not to use int argc, char const *argv[] in int main(...) and leave it as int main() {

    
answered by 09.03.2017 в 11:43