Tuesday 7 May 2013

Writing and Compiling Programs in Linux


Writing and Compiling Programs in Linux

C++ programs

Many UNIX and Linux distributions contain a C and a C++ compiler. To locate g++ (the c++ compiler), try find / -name g++ (this searches all subdirectories under / for the g++ file.

You’ll probably find it’s in a package and needs to be installed with a prompt something like:
            zypper install gcc-c++

Sometimes this may be blocked e.g.

PackageKit is blocking zypper.

If so, you should have the option of quitting (PackageKit) and continuing with the install.

Once it’s installed, find out where it’s located (find / -name g++) and its directory to the PATH by entering something like:

export PATH=$PATH:/usr/local/bin

However, it may be in the PATH already – especially if you are root.

We can now start writing and compiling programs. Look up the following web site for general information on compiling c and c++ programs in a UNIX environment.


Look over this for a few minutes. Create a new sub-directory for your programs and place your programs here.

Exercise 1

Attempt to write and compile the following c++ program

#include <iostream>
                        using namespace std;


int main()
{
    cout << "hello world" << endl;

    return 0;
}

Ø  Create a new file e.g. vi prog1.cc – l add ‘.cc’ to the suffix of each program to indicate that it’s a c++ program, C programs usually end in ‘.c’.

Ø  Write the program as above.

Ø  The command to run the c++ compiler is g++.

Ø  To compile prog1.cc, type g++ prog1.cc –o prog1.out.

Ø  List your files (type ls) and you will see a new file prog1.out has been created.

Ø  This new file - prog1.out is executable and you can run it e.g. ./prog1.out

Ø  Do a detailed listing (ls –l), compare the size of prog1.cc and prog1.out.

ls -l prog1.out
-rwx--x--x   1 joe dcom2     710716 Mar 19 13:00 prog1.out
ls -l prog1.cc
-rw-------   1 joe dcom2         90 Mar 19 12:59 prog1.cc


C programs

c programs can be similarly compiled. Linux contains a c compiler. Try compiling a c program yourself. The web site mentioned above might help (or search for another). A simple s program is as follows:

#include <stdio.h>
 
int main(int argc, char* argv[])
{
    printf ("hello world\n");
 
    return 0;
}

Java programs

A Linux Java JDK can be downloaded to your clone. It’s easy to write, compile and execute simple java programs.


or alternatively google java jdk downloads

Select JDK Download

Accept the License agreement

From the list of versions -

Select jdk-7u15-linux-i586.tar.gz

This is a compressed archive and when you click download, it will probably go to the Downloads directory in root's home.

Create a new directory for java e.g. /java and move the compressed archive here

Decompress the file – use gzip -d

De-archive the file – use tar xvf

This should result in a directory which contains the java jdk. Change to this directory and go to the subdirectory bin. Note the javac and java files. We will use these to develop and run programs.

Add this bin directory to your PATH e.g. PATH=$PATH:/java/jdkDir/bin
(substitute the directory name for jdkDir)

Create your own java programs e.g.

public class HelloWorld {

    public static void main (String[] args) {
   System.out.println ("Hello World!");
    }
}

Ideally place this in your home directory (/root if you are root) using any editor. Remember save the file with the name of the class followed by .java e.g. HelloWorld.java.

No comments:

Post a Comment