Compile C CPP and Java programs using Kate

From GLUG-BOM

You are here: Main Page >> Howtos >> Compile C CPP and Java programs using Kate

Contents

Overview

Compiling (and/or executing) C, C++ (CPP) or Java programs, or for that matter almost any programming language involves two typical steps. One, to write the source code in a text editor and saving the file. Second, invoking a Konsole (K factor ;) ) to issue commands to compile the program and execute it. Therefore, one would have to fire up his favourite text editor, type the program save it. Then, open up the terminal, move to the directory, where his files are stored. Issue commands to compile and/or execute the program. This is marginally relaxed in Kate, because, it provides a terminal within the environment to compile and run programs. However, wouldn't it be more fun, if you could press a combination of keys and "zoom!" the program compiles and generates the executable?

What is this document for and what it isn't for

The default shell for most Linux distributions is '"BASH"'. Assuming you have BASH shell running, configuration options for configuring Kate includes BASH commands and BASH specific keywords. This document is to help you with configuring Kate to compile and run programs for C, C++ and Java programs on unique keystrokes. However, the procedure won't differ much for other programming languages. The document will explicitly state the commands for C, C++ and Java programs.

It is also assumed that the compilers and interpreters of programming languages are installed. This means, for C / C++ GCC is installed. For Java, either Sun Java (sdk packages) or Blackdown Java are installed.

BASH specific explanations are provided here. However, one doesn't need to be an expert at it. One can learn more about BASH from the internet or various books. (If you are good at BASH and would like to expand the explanations, please feel free to do so.)

Configuring Kate

  • You can start Kate through the command line simply by typing Kate or accessing it from the K Menu.
  • Click on Settings -> Configure Kate
  • You see a Main Configuration Dialog (Configure - Kate) window. A screen-shot is shown below.

  • Click on External Tools. You might see a few entries already. Here, you'll start adding up small scripts that'll execute when you press key combination.

  • For including each script you'll have to follow the same procedure below. (Steps 1 to 7, below the image)

Image:Script_window.png

  1. Click on the New button. You'll see a window similar to a screen-shot shown above.
  2. In the window, type a Label such as "Compile <language> program" or a label of your choice.
  3. You can also choose an icon for the script by clicking on the square beside the text-box
  4. In the second text-area field, script, the script for compiling / executing programs will be typed.
  5. In Executable, type konsole (typically used for compiling C/C++/Java programs). For actual scripts see, the next section.
  6. You can leave MIME types, Save and Command Line Name to be empty.
  7. Click on OK, and the successive OK button of 'Configure Kate'

Scripts for C, CPP and Java

For Compiling C/C++ programs, the following command needs to be entered in "script" box. (Note, the script is written here on several lines allowing wiki to format it accordingly. Please type those lines together in the script window.)

cd "%directory" && CF="%filename" 
konsole -e sh -c 'gcc-4.0 -o ${CF%.*}.out "%filename"; 
printf "%s" "Press return to exit"; read $dummy'

For Executing C/C++ programs

cd "%directory" && CF="%filename" 
konsole -e sh -c './${CF%.*}.out; 
printf "%s" "Press return to exit"; read $dummy'

(Note if you have j2sdk / java-1.5.0-2 (sun) installed in a different directory, replace "javac / java" with the entire path to the binary. For example, you may have installed it to /opt/j2sdk. Therefore, instead of javac it would be /opt/j2sdk/javac )

For Compiling Java programs

cd "%directory" && 
konsole -e sh -c 'javac -verbose "%filename"; 
printf "%s" "Press return to exit . . . "; read $dummy'

Note: You can choose to omit the -verbose flag. The verbose files displays the jar files (and other such files) loaded during compilation. Without the verbose flag, the compiler will display just the errors.

For Running Java programs (Console, GUI, etc. NOT Applets)

cd "%directory" &&  CF="%filename" 
konsole -e  sh -c 'java ${CF%.*}; 
printf  "%s" "Press return to exit . . . "; read $dummy'

For Executing Applets (Only Applets)

cd "%directory" && 
konsole -e  sh -c 'appletviewer %filename'

Configuring Shortcuts to invoke the scripts

  • Click on Settings -> Configure Shortcuts
  • You'll see the entries created (using the scripts above) in the window, under External Tools. A screenshot is shown below.

  • Click each and click on the "Custom" (radio) button below. You'll see a window to press keys to configure them as shortcuts.

  • Press a combination such as Ctrl+1 to compile C programs and so on, for each script you have included.

A brief explanation of the script used here

From http://docs.kde.org - Kate Documentation (first and second)

%directory

The directory part of the current documents URL or an empty string if the current document is unsaved. In our case, the cd command changes to the directory where the source file is stored.

%filename

The filename of the current document without the path, or an empty string if the current document is unsaved. In our case, the filename is name of the file.


konsole -e

This opens a konsole to execute a command that follows as an argument to -e option. In our scripts, it is used to execute a script (sh). Note 'sh' follows, konsole -e.

Executing scripts

sh -c 

sh allows one to execute a script, typically with a filename (which is a script (in our case it is the compiler/interpreter)) as its argument. The -c option allows one to specify a command as a string (i.e. within quotes) to be executed.

This means

sh -c "string_containing_a_command_for_compiling" 

would be used to invoke the compiler / interpreter to compile and/or run the program.

Since, we are invoking the konsole, just to execute a command, it would flash on executing the script and one may not be able to see errors, if any, after compilation. Therefore, additional commands are added at the end of the scripts

Additional commands

printf "format_string" "string to be printed"

This command is used to print a string. In our script, this is used to indicate the user that the execution of the script is complete and informs him / her what to do next.

read $val

This command is used to read a value. The user is expected to press "Enter" key to close the Konsole.

Sources

This document was created with the help of

  1. http://www.tuxmachines.org/node/13762
  2. #bash on irc.freenode.net