I want to learn C/C++. Here my requirement is not to use any IDE. I want to run my C/C++ program in following way,

  1. I will write "Cpp1.cpp" //which will print "Hello World".
  2. Save the file and close.
  3. Right click on the "Cpp1.cpp" file.
  4. In the right click menu options, I should have a button as "Run C++".

After following above 4 steps, output or errors should appear in the terminal window.

I tried to do using Nautilus script, but it failed miserably after multiple trials.

Please see the screenshot (which is not working the way that I am expecting).

Reference image

I am trying to do like below, check here

  • If you are starting to learn c/c++ this should not be where to start .. actually AskUbuntu is not about someone developing a code you request but rather about Ubuntu specific themes AND expects that you already did some research and provide a specific problem and example of what you already did. – derHugo Oct 9 '17 at 18:28
  • Hi Hugo, I agree with you. This is not the right forum to ask c, cpp. Certainly I am not asking c, cpp program or resolve any error. I asked question to exploit Ubuntu features. I have already done the background work in nautilus. The screen shot shown in question is based on my work. I haven't shown code, bcz, I don't wanted bias others from giving tweaked solutions of mine. And I feel, the problem mentioned will help beginners to increase their productivity. – Ravi Teja Oct 9 '17 at 20:47

1. Create Nautilus script's file and make it executable:

touch "$HOME/.local/share/nautilus/scripts/MyC++Run"
chmod +x "$HOME/.local/share/nautilus/scripts/MyC++Run"

2. Here is the content of the script. It creates an auxiliary (auto deleted) script, which is executed in a new gnome-terminal, thus you can see the error messages within the terminal window:

#!/bin/bash -e

# Get the list of the selected in Nautilus items as an array $ITEM_LIST

# Create aux script, that compile and execute the program. Run the script in gnome-terminal
compile_and_exec_program() {
        OUT="${DIR}/${NAME}.out"              # Define the name of the output file
        AUX="${DIR}/${NAME}.bash"             # Define the name of the aux script
        printf '#!/bin/bash -e\n' > "${AUX}"  # Create the auxiliary script
        printf '%s "%s" "%s" && "%s"\n' "${1}" "${OUT}" "${item}" "${OUT}" >> "${AUX}"
        printf 'rm -f "%s"\nexec bash' "${AUX}" >> "${AUX}"
        chmod +x "${AUX}"                     # Make the aux script exec and run it
        nohup gnome-terminal -x sh -c "$(echo \'"${AUX}"\')" >/dev/null 2>&1 &

# For each selected item: get its name, location, etc. and proceed...
for item in "${ITEM_LIST[@]}"; do

        ITEM="$(basename "${item}")"          # Get the item name (exclude the path)
        DIR="$(dirname "${item}")"            # Get the path to the item (exclude the name)
        NAME="${ITEM%.*}"                     # Get the name (exclude the extension)
        EXT="${ITEM##*.}"                     # Get the extension (exclude the name)

        # If the item is a file and its extension is `c` or `cpp`, then compile and execute
        if [ -f "$item" ]; then
                if   [ "$EXT" == "c" ];   then compile_and_exec_program "gcc -o"
                elif [ "$EXT" == "cpp" ]; then compile_and_exec_program "g++ -o"
                else notify-send "Wrong extension of the selected file: $ITEM"
                notify-send "The selected item is a directory: $ITEM"
  • Additional explanations: Using of an auxiliary script is the most robust way to run more than one commands within a new gnome-terminal, that I found while I made one of my answers.

    Depending of the input parameters of the function compile_and_exec_program, the content of the generated, by the printf section, auxiliary script will be similar as:

    #!/bin/bash -e
    g++ -o /work/dir/project.cpp /work/dir/output.out && /work/dir/project.out
    rm -f /work/dir/project.bash
    exec bash

    Where && means (as usual) if the command that is on the left side is successfully executed, then execute the command that is on the right side. The line rm -f /work/dir/project.bash will remove the auxiliary script itself. The last line exec bash intends to keep open the new gnome-terminal window.

    This part $(echo \'"${AUX}"\') intends to print single quote marks around the name of the aux script. It is important when the script name contains some special characters. I couldn't found other way to do that. Another way to qote only the spaces is using of: ${AUX/\ /\\ }.

  • Here is an example script, that creates a log files, where you can see the error messages from the process.

3. Here is a demonstration (from the previous version) of the script's features:

enter image description here

  • Nice - but you probably want to compile cpp with g++ rather than gcc so that the correct standard libs are linked - you could consider using `make "${FILE_NAME}" which should call the appropriate compiler / linker agnostically – steeldriver Oct 8 '17 at 17:09
  • Hello Pa, Woowww..Thanks.. However, there is an issue. If there is any errors in program, Terminal is not opening. Due to this we can not see whats the reason for not execution of program. I request you to update this also.... – Ravi Teja Oct 9 '17 at 3:31
  • Hi Pa, Thanks for the response. Certainly I am not expecting the 1 Code output. Rather my expectation is 2 Code output. However there is small glitch in 2 code. Step1: write "Hello World" program without any error. Step 2: Execute it. Step 3: You will see Cpp1.out in the folder. Step 4: Update the Cpp1.cpp code by removing ; to create error. Step 5: Run Cpp1.cpp. Step 6: You can see the error list with description in terminal. Here is the issue, 2 code also running Cpp1.out which was generated in step 3. This is not expected. Could you please update the code. Thanks! – Ravi Teja Oct 9 '17 at 13:28
  • Hi Pa, Issue 2: If the file name has any space, (example: "rev array.cpp" in terminal showing error as "not found". Please update for this also. Thanks! – Ravi Teja Oct 9 '17 at 13:43
  • Issue 3: If the file is other than desktop folder, "not found" error is showing in terminal. Please update this.. – Ravi Teja Oct 9 '17 at 13:58

You have a basic misunderstanding about C/C++ programming: These are not scripts, interpreted at run time. Instead these programs need to be compiled and converted into run-able programs.

Assuming the name of your file is Cpp1.cpp, then you will need to execute the following a terminal:

gcc -o Cpp1 Cpp1.cpp

The resulting output, Cpp1 will be an executable binary file which can be run using the command ./Cpp1 Please note that in this case, this program cannot be run by right clicking on it: it does not have any knowledge about opening windows and using them.

  • Hi Green, Please see the method I am thinking of, i.stack.imgur.com/hB4L1.png Please re look. Thanks – Ravi Teja Oct 8 '17 at 14:13
  • If you are clever with scripting (I am not) you could cause a script to take the filename as an argument, compile the program and then execute it. – Charles Green Oct 8 '17 at 21:10

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.