A program made in c runs on double click in windows but not in linux

executableguiterminal

I have made a simple addition program in C in both OS, Linux (Ubuntu and CentOS) and Windows 7 with the same source code, which is as follows:

#include <stdio.h>
int main(){
int a,s,d;

printf("type the values u want to add and give tab between them\n");

scanf("%d %d",&a,&s);

d=a+s;

printf("addition is %d",d);

return 0;

system("read -p 'Press Enter to EXIT...' var");
}

In Windows it runs when I double-click on addition.exe but in Ubuntu (also in CentOS) when I click on the executable file addition, nothing happens. It does not run or open a terminal. However, it runs when I type ./addition in a terminal.

But I want to run it by double-clicking on it. What should I do?

The properties of that files are in this image:

Properties of the "addition" executable file

Also there is no option like "open in terminal" in the open with section of properties.

I also tried creating .desktop file which is as follows:

[Desktop Entry]
Name=addition
Type=Application
Exec=/media/smit/D/smits programs of c/projects by code blocks/02U/addition/bin/Debug/addition
Terminal=true

When I click on addition.desktop then it says an error occur while launching application.

I also tried to open it by copying this desktop file to /usr/share/applications.

Best Answer

The core of the issue is that you're trying to run the program, which is console application, but you don't have terminal attached to it. In terminal you can run your program by just calling the program name, but in GUI you need to specify explicitly that there should be a terminal window raised to run console apps (this is particularly true of GNOME-based desktops, such as Ubuntu's Unity).

What should be done is that you also need to create a .desktop file for your program with 4 fields. Here's an example:

[Desktop Entry]
Name=MyProg
Type=Application
Exec=/home/xieerqi/example_directory/hello_world_prog
Terminal=true

I don't know about CentOS, but as far as Ubuntu goes, it's the requirement that .desktop applications must be made executable also if they are located in any directory under user's home directory. .desktop files that live in other directories, such as /usr/share/applications do not require that.

So, once you have the .desktop file in place, and made it executable, you will be able to run the program. The important bit is Terminal=true line. That's what will tell GUI to raise a terminal and run your program there. NOTE: if your program executes stuff and exits immediately, you will need to have some sort of delay or getchar(); call just to keep the window open, because terminal window will exit when program exits. That's why many users are sometimes confused "Why is my program not running?" It runs, in reality, it just exits too fast.


Additional notes:

  • get rid of system("read -p 'Press Enter to EXIT...' var"); . The read call is a shell built in, and is not a standalone program, which means it only can be used if you use a shell, such as bash. If you are using C, do it properly using scanf() or getchar() to add delay to your program. It is present after return 0; line which means your system() line won't be reached (the program will just quit at return statement), so your placement of the pause for the program is also invalid.
  • Read this post on AskUbuntu for example of proper .desktop file with links to official documentation.
  • If you are feeling lazy and don't want to make .desktop files for each and every executable file, there's plenty of solutions here. I even posted a script there as well.
Related Question