4

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.

  • OK, I think I've refined my answer up to everything that I could find about your code and tried to nail all the core issues. Let me know if you have any other questions – Sergiy Kolodyazhnyy May 8 '17 at 6:12
  • It probably doesn't have anything to do with the issue, but please put a terminating newline in that .desktop file. And please also copy-paste the contents of that terminal screenshot – Fox May 9 '17 at 1:24
  • @Fox nope, terminating newline doesn't affect .desktop file, at least in my tests – Sergiy Kolodyazhnyy May 11 '17 at 18:20
  • To narrow the problem, please try Exec=/bin/sh -c '/path/to/your/app'. This will start a shell to execute the program. The shell call should succeed and output what's wrong with the application call. And if the path contains spaces, you can surround it with double quotes inside the single quotes of the -c option. Paths with spaces are generally not helpful while debugging problems like this. – Philippos Jun 19 '17 at 8:00
  • After reading the Answers below, I must agree that removing spaces in the path would help this issue tremendously. What's wrong with /home/smit/smitsCPrograms/codeBlocks? – eyoung100 Jun 20 '17 at 21:08
10

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.
  • please look at my question cause i edited it. – voldimot May 8 '17 at 8:33
  • OK, so you said you made the .desktop file and it shows the error. What are the contents of the .desktop file ? What error does it say ? – Sergiy Kolodyazhnyy May 8 '17 at 8:36
  • the content is in image.i cant open is so use cat command. and error is"There was an error launching the application" when click on addition.desktop file i can t add image due to my low reputation. – voldimot May 8 '17 at 9:26
  • also tried this link "askubuntu.com/questions/64222/…" but it says "Gtk-Message: GtkDialog mapped without a transient parent. This is discouraged"and desktop file made gives same error"there was an error launching the application" – voldimot May 8 '17 at 9:30
  • 1
    Temporarily place the script in a directory that does not have special characters (e.g. space) in the path, and adjust the .desktop file accordingly. This is to see if the space character causes the problem. As I vaguely recall, I think spaces have to be denoted by \s in .desktop files. – egmont May 11 '17 at 10:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.