Cplusplus

From Redbrick Wiki
Revision as of 19:03, 5 December 2007 by Coconut (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

OMG UNDER CONSTRUCTION LOL

C++ is an object-oriented programming language, developed as a superset of the language C, which means that the language is, essentially, C with more features, hence the name. However, it has since evolved into a separate language and compatiblity with C is no longer guaranteed.

It is a very popular all-purpose language which is commonly utilused for game programming.

g++

The page Programming on Redbrick explains how to compile programs written in C++. Here we will go into more depth as to how the compiler g++ works.

A compiler works by translating your programming language (in this case, C++) into assembly language, which is the basic language that a processor understands. While C++ is a universal language (source code written on one architecture will generally work on another unless special libraries are used), programs must be recompiled if they are to be used on different architectures - for example, if you are compiling on murphy, which uses the UltraSPARC architecture, you will have to recompile your program if you wish to use it on carbon or deathray, which use the Intel x86 architecture.

g++ is the free, open-source C++ compiler which is included with many distributions of Linux as part of the gcc package, the latter of which is a C compiler. Other popular C++ compilers include Borland C++ and Microsoft Visual C++, however these are proprietary. gcc (and g++) is the most widely ported and universally available compiler in the world.

Getting started

To begin programming in C++ on RedBrick, you first have to log in. Once you've done that, you can create a new folder for your C++ files by utilising the following command

mkdir C++

This will create a directory called "C++" in your home directory. Open up this directory by typing

cd C++

Now you're ready to create your first C++ program. For this tutorial, we are going to use the nano editor, as it is simpler than the alternative, vim. Make a new C++ source file, that we will call "first", with

nano first.cpp

C++ source code files will generally use the .cpp extension - you may have noticed that C programs use .c

nano should be open, awaiting input. The first thing you should do is import the necessary libraries to allow input/output in your program. This is the only library we will use at this point. Make the following line 1 of your program

#include <iostream>

iostream (or, archaically, iostream.h) is a C++ header file, which is a file containing C++ code designed to be imported by multiple programs.

Now we want to create our main() function. main() is the first part of code that will be executed upon running your program.

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.