Score:0

How to create a .desktop to a C program?

kr flag

So I'm trying to execute a C terminal program just by double clicking it. I'm trying to do that through a .desktop file

  • The desktop file has permission to execute as a file and it's in my desktop.
  • The program is named teste, wait for User input and it's in the /home/vithor/
  • I'm Using the PopOs ( it is a "ubuntu distro" );
  • I already allowed executable to launch in the nautilus configs;
[Desktop Entry]
Version=1.0
Name=ProgramName
Comment=This is my comment
Exec=/home/vithor/teste
Icon=/home/vithor/Pictures/Icons/books.png
Terminal=true
Type=Application
Categories=Utility;
~                    

But it's not working, what am I doing wrong? Or what alternative do I have to run a C program just by clicking it?

us flag
Remove the ; after utility. Also, Pop OS is not supported here.
us flag
*But it's not working* -- What is the current behavior? Does the desktop file show the icon?
Score:1
us flag

I am assuming the name of the program is teste, and it is located at your home folder.

  1. Remove #!/usr/bin/env bash from the first line. The first line should be [Desktop Entry]

  2. Change the Exec entry to

    Exec=/home/vithor/teste
    
  3. Change the last line to Categories=Utility

  4. Mark the .desktop file as executable with the command chmod +x /path/to/file.desktop.

Now it should work when you double click it.

However, if the C program is supposed to print in the terminal and do nothing else, then it would print the results and would quickly close the terminal, and you won't be able to see anything. In such a case, change the source so that it waits for you to enter some character before closing.

Example code:

#include <stdio.h>

void main(){
// your code here

int a;
printf("enter any character to terminate: ");
scanf("%d",&a);
}

Now compile the code with

gcc path/to/source.c -o teste
WinEunuuchs2Unix avatar
in flag
Nice example of a C program. However shouldn't there be a `gcc` or `make` or `lint` command somewhere?
us flag
@WinEunuuchs2Unix Of course, one needs gcc to compile this. I am assuming OP knows how to compile, since the question is about how to execute the binary `teste` with a .desktop file.
WinEunuuchs2Unix avatar
in flag
No doubt OP and you know `gcc`. Some of us don't is all :)
us flag
@wineunuuchs2unix Oh OK, I will update the answer.
Vithor avatar
kr flag
Thank you for your help! But I tried it, and it didn't work :(. I will edit the question and add more information!
Score:0
nl flag

Use this .desktop file:

[Desktop Entry]
Version=1.0
Name=ProgramName
Comment=This is my comment
Exec=x-terminal-emulator -e /bin/bash -c '/home/vithor/teste ; echo "" ; echo "Press Enter to close....." ; read'
Icon=/home/vithor/Pictures/Icons/books.png
Terminal=true
Type=Application
Categories=Utility;

It opens up a terminal, runs your program and after your program is done, it waits until you press Enter to close it.

BTW I've tested this and it actually works.

If this works for you, please click the Tick icon on the left of this answer to accept this answer.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.