1

I'm using Ubunru 15.04

GCC : 4.9.2

I usually use this command to compile my cpp files :

g++ /path/file.cpp -o /path/output

and it's cool ! so I made this into a shortcut with

.desktop

file format and make it executable and it works just fine !!

now I need to make a single shortcut to compile the cpp file and run the output

I simply tried :

g++ file.cpp -o output && /output

but this didn't work and giving Permission Denied when I try to run the .desktop file in terminal

ebram96@ebram96-jj:~$ '/path/shortcut.desktop' 
/path/shortcut.desktop: line 1: [Desktop: command not found
/path/shortcut.desktop: line 2: run: command not found
/path/shortcut.desktop: line 3: /path/file.cpp: Permission denied

here is my .desktop file :

[Desktop Entry]
Name=compile&run
Exec=g++ /path/code.cpp -o /path/output && /path/output
Terminal=true
Type=Application

I simply tried :

g++ file.cpp -o output && ./output

I tried editing /etc/sudoers file adding these lines it also didn't work :

my_username ALL = NOPASSWD: /path/shortcut.desktop

my_username ALL = NOPASSWD: /path/

my_username ALL = NOPASSWD: /same_shortcut_path/output
2
  • Edit your question and add the desktop file. I've tested a simple C++ app and it works with this Exec property: Exec=g++ /path/to/your/foo.cpp -o /path/to/your/output && /path/to/your/output
    – A.B.
    Aug 31, 2015 at 8:20
  • @A.B. Done editing , I did what you mentioned in the comment and it didn't work giving the same result Aug 31, 2015 at 8:37

3 Answers 3

1

Try adding the following to your ~/.bashrc file:

cr(){
    if [ "$#" -ne 1 ]; then
        echo "Usage: cr <file.cpp>"
    else
        name=$(echo $1 | cut -f 1 -d '.')
        g++ $1 -o name; ./name
    fi
}

This creates an alias that can be executed from the terminal.

The function name / command i.e. cr can be replaced if you wish.

To execute run: cr file.cpp

0

You can't start a desktop file in this way. Move your desktop file into ~/.local/share/applications and use gtk-launch or start it via Unity/GNOME Shell ...

gtk-launch shortcut     # without any path or extension

In your case, an alias would be the better solution

Open your .profile in an editor

nano ~/.profile

add a line like this

alias compile="g++ /path/to/your/foo.cpp -o /path/to/your/output && /path/to/your/output"

logout and re-login or use

source ~/.profile

Use the command (alias) with a simple

compile
7
  • it didn't work , after moving the shortcut to the mentioned directory and trying to launch : gtk-launch shortcut , it didn't work ,, I must open the terminal and use source ~/.profile and compile to make it work , I only want a single shortcut to compile and run without manually opening the terminal :( Aug 31, 2015 at 9:18
  • And what is the error message?
    – A.B.
    Aug 31, 2015 at 9:19
  • there is nothing appears , but i think it is giving the same result because it does the same behavior ( only a terminal appears for a very little while and disappears) and the result of that i think it is "Permission Denied" Aug 31, 2015 at 9:22
  • I also tried to make a shortcut for : source ~/.profile && compile but it is "Permission Denied" , I did a restart and log-off Aug 31, 2015 at 9:24
  • No. Open your profile settings for your profile in gnome-terminal, go to the tab Command and select Hold the terminal open for When command exits. But as I said, the desktop file is the wrong way in your case.
    – A.B.
    Aug 31, 2015 at 9:25
0

Create a bash function (to add in your .bashrc or .zshrc):

function run-c++ {
  if ! type "g++" > /dev/null; then
    sudo apt-get install g++
  fi
  g++ $1 -o /tmp/main.out && chmod +x /tmp/main.out && /tmp/main.out
}

Then you can run run-c++ ./whatever-path/to/program.cpp.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .