Programming On Redbrick

From Redbrick Wiki
Revision as of 19:32, 5 December 2007 by Coconut (talk | contribs)
Jump to navigation Jump to search

This is a general introduction to compiling and running Java, C and C++ programs on redbrick. It is assumed that you already know how to write programs in any of these languages.

Note: there is now a new Cplusplus being constructed at the moment.

GCC

GCC stands for Gnu C Compiler. It is a fairly popular UNIX C compiler and is based on the standard C Compiler, known as CC :o)

Once you have written a program in nano or vim and saved it as a .c file (the c suffix indicates it is a c language file), you can compile it to create a program that you can run on redbrick. Here is an example in which I compile a file I saved as prog.c into a program called program :

gcc -o program prog.c

If you want to ensure that the code you compile is ANSI compatible (that is that it fits the latest international standards for the way C code should be written), you can add the -ansi switch to the gcc command, so now you have :

gcc -ansi -o program prog.c

A lot of the time you will want to tidy up your code so that there aren't any warnings even when you compile it, to turn on warnings so you can see any possible errors in your code, add the -Wall switch to your gcc command, so now my example reads :

gcc -ansi -Wall -o program prog.c

Now that I have my program compiled and there were no errors, an ls on the directory I'm in will show up a file called program. To execute this program I type :

./program

To compile C++ programs, use :

g++ -o program prog.cpp

Compiling and Running Java Programs

In order to use Java, first login to Deathray, like so:

ssh deathray

The Java compiler on redbrick is called, unsurprisingly, javac. Compiling Java programs is a little more complex than compiling C programs as you need to have two environment variables set, JAVA_HOME and CLASSPATH. To see what these are set to, at the prompt type :

echo $JAVA_HOME
echo $CLASSPATH

Echo is a command for printing stuff to the screen, what it does in the above example is "echo" the value of the JAVA_HOME and CLASSATH variables to the screen.

Your JAVA_HOME variable should be set to /usr/java and your CLASSPATH should be /usr/java/lib/dt.jar, if they're anything else you can edit the variables by typing the following :

JAVA_HOME=/usr/java
CLASSPATH=/usr/java/lib/dt.jar

Of course you can add extra directories to this, seperated by a : so that you can compile programs in your home directory. Another difference between javac and gcc is that the main class in your java program has to have the same name as the file it's in, and the file has to have a .java extension, for example :

javac progam.java

Then to execute the program (assuming it compiled okay ;o)) you type :

java program

This will execute the program.