Saturday, April 23, 2011

Compiling and Running your own programs


  • Unix and Linux in particular provide a robust environment for software development.
  • The basic tools to write software can be as simple as a text editor and a compiler (or interpreter).
  • Compilers that are generally found on Linux installations are the GNU C/C++ compiler (gcc) and the Java Development Kit (JDK) that icludes the Java compiler (javac)
  • Other versions of UNIX make use of their native "C" compilers such as cc
  • Interpreted languages such as Perl, Python and TCL are also often installed by default on Linux systems.
  • Programming in the "C", C++ or Java programming languages is beyond the scope of these notes, however, you may wish to view these general notes on programming languages athttp://cisnet.baruch.cuny.edu/holowczak/classes/programming/
  • Let us assume we will write a simple program to echo out "Hello World" to the console. Below are the instructions for writing, compiling and running such a program:
  • "C" Programming Language
    1. Use the pico editor to create a file called hello.c containing the following lines: #include <stdio.h> int main() { printf("Hello World\n"); }
    2. Save this file. At the UNIX command prompt, invoke the gcc compiler as follows:
      gcc hello.c
      
    3. The result of this step (provided there are no syntax errors in the program) will be an executable file called a.out This is the default file name given by the compiler.
    4. To execute the program type: ./a.out 
      The period and slash in front of the a.out indicate to run the a.out file in the current directory.
    5. To compile a program and save the compiled version in a different file name, use the -o option as in:
      gcc -o hello.exe hello.c
    6. Note: To avoid having to put the ./ in front of the file name to execute a program, you may add the current directory (indicated by a period) in your PATH by typing:
      PATH=$PATH:.
      Note the final two characters are a full colon followed by a period.

  • C++ Programming Language
    1. Use the pico editor to create a file called hello.cc containing the following lines: #include <iostream> main() { cout << "Hello World\n"; return 0; }
    2. Save this file. At the UNIX command prompt, invoke the c++ compiler as follows:
      c++ hello.cc
      
    3. The result of this step (provided there are no syntax errors in the program) will be an executable file called a.out This is the default file name given by the compiler.
    4. To execute the program type: ./a.out 
      The period and slash in front of the a.out indicate to run the a.out file in the current directory.
    5. To compile a program and save the compiled version in a different file name, use the -o option as in:
      c++ -o hello2.exe hello.cc

  • Java Programming Language
    1. Use the pico editor to create a file called helloworld.java containing the following lines:
      class helloworld
      {
        public static void main (String args [])
        {
         System.out.println ("Hello World");
        }
      }
      
    2. Save this file. At the UNIX command prompt, invoke the Java compiler as follows:
      javac helloworld.java
      
    3. The result of this step (provided there are no syntax errors in the program) will be a Java .class file called helloworld.class This is the default file name given by the compiler.
    4. To execute the program type: java helloworld 
      Note that the .class extension is ommitted.
    5. If the Java runtime complains that it can not find this class, you will need to add the current directory to your CLASSPATH environment variable. To do this type:
      CLASSPATH=$CLASSPATH:. 
      Note the final two characters are a full colon followed by a period.
  • Perl Programming Language
    1. Perl is an interpreted language. The first line of the program is a special comment that tells the script which interpreter to use.
    2. More on Picking Up Perl http://ebb.org/PickingUpPerl/
    3. Use the pico editor to create a file called helloworld.pl containing the following lines:
      #!/usr/bin/perl
      # Hello World program written in Perl
      print "Hello World\n";
      
      
    4. To run the Perl program either:
      • Run perl followed by the name of the perl program: perl ./helloworld.pl
        or 
      • Change the mode of the program's file to be executable: chmod 7500 helloworld.pl 
        and then run it directly: ./helloworld.pl
  • Python Programming Language
    1. Python is an object oriented interpreted language. The first line of the program is a special comment that tells the script which interpreter to use.
    2. More on Learn to Program using Python: Lesson 1, Getting Started by Richard G. Baldwin
    3. Use the pico editor to create a file called helloworld.py containing the following lines:
      #!/usr/bin/python
      # Hello World program written in Python
      print "Hello World"
      
      
    4. To run the Python program either:
      • Run Python followed by the name of the perl program: python ./helloworld.py
        or 
      • Change the mode of the program's file to be executable: chmod 7500 helloworld.py 
        and then run it directly: ./helloworld.py

No comments:

Post a Comment