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:
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: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 isTerminal=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 orgetchar();
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:
system("read -p 'Press Enter to EXIT...' var");
. Theread
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 asbash
. If you are using C, do it properly usingscanf()
orgetchar()
to add delay to your program. It is present afterreturn 0;
line which means yoursystem()
line won't be reached (the program will just quit atreturn
statement), so your placement of the pause for the program is also invalid..desktop
file with links to official documentation..desktop
files for each and every executable file, there's plenty of solutions here. I even posted a script there as well.