1

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

Ravi Teja
  • 15
  • 6

2 Answers2

3

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
IFS_BAK=$IFS
IFS=$'\t\n'
ITEM_LIST=($NAUTILUS_SCRIPT_SELECTED_FILE_PATHS)
IFS=$IFS_BAK

# 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"
                fi
        else
                notify-send "The selected item is a directory: $ITEM"
        fi
done
  • 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

pa4080
  • 30,621
1

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.

Charles Green
  • 21,859