Getting Started Documentation Glish Learn More Programming Contact Us
Version 1.9 Build 1556
News FAQ
Search Home


next up previous contents index
Next: Second Version Up: Introduction to Programming for AIPS++ Previous: Introduction to Programming for AIPS++


A First Program

We will start with a small sample program which prints the values generated by a simple function. The object, of course, is merely to explain the conventions and procedures used in an aips++ program (The source code for this program is found in $AIPSROOT/code/trial/implement/test/tExample1.cc).

     1  #include <aips/aips.h>
     2  #include <aips/Exceptions.h>
     3  #include <aips/Mathematics/Math.h>
     4  #include <iostream.h>
     5
     6  main(int argc, char **argv)
     7  {
     8   const Int     n(8);
     9   const Float   interval(10.0);
    10   Float   x, y;
    11
    12   try {
    13      for (Int i=0; i<n; i++) {
    14         x = i;
    15         y = i * sin(i / interval);
    16         cout << x << "\t" << y << endl;
    17      }
    18   }
    19
    20   catch (AipsError x) {
    21      cerr << "aipserror: error " << x.getMesg() << endl;
    22      return 1;
    23   }
    24   return 0;
    25  }

The following discussion refers to the line numbers which have been prepended to the code above:

1-
Every aips++ program must contain this line. It declares variable types Int, Float, etc. plus other goodies used for debugging.

2-
This class handles exceptions for aips++ programs.

3-
This calls the routine that defines the sine function. The various C++ vendors do not always provide the expected routines in their math libraries. This invocation allows the aips++ routines to call whichever versions are appropriate.

4-
This is the standard IO for this C++ package. It is not directly related to aips++.

6-
The declaration of the main program must provide for argument input. The use of argc and argv causes the compiler to issue a warning in this instance because they are not used. (They will be used in a later example.) Note that these data types are NOT written with a leading capital letter since the compiler wants native-mode variables in this instance.

8-10-
Note the use of first letter capitals in the type declarations. These are defined in aips.h. Note too the specification of values on these lines rather than in the code body. This is to make it easier to make changes to the code. The use of the qualifier const means that these parameters cannot be accidentally altered in the code.

12-
The try block (code between { and }) define the extend of the code that is to be monitored by the handlers (defined by the catch clauses below).

13-
The use of inline declaration of variables (Int i) in for loops is strongly recommended.

20-
The catch statement intercepts various types of errors. In this case we are asking the program to report on general errors related to aips++. Note that the argument x is NOT related in any way to the variable x declared on line 10.

21-
In this case we are telling the program to print out the fact that an error has occured on the terminal.

To compile this program one should use the gmake command which will report on any errors and which will create an executable module with the name supplied. The gmake macro has built-in knowledge of the location of each of the included <aips/xxxxx> files. In order to use gmake you must be logged in to one of a limited number of areas. In particular, you should be in ~/aips++/code/aips/test

Assuming the source code was named sample1 the command would simply be

gmake NODEP=1 sample1 where the string NODEP=1 merely tells the compiler to take a short-cut which is quite allright for modules that are not intended to be part of the aips++ library. Note that we did not have to specify the suffix appended to the source module name. However that suffix must be cc.

The program is started simply by typing its name: sample1. The output is as follows (note the default format):

0       0
1       0.0998334
2       0.397339
3       0.886561
4       1.55767
5       2.39713
6       3.38785
7       4.50952


next up previous contents index
Next: Second Version Up: Introduction to Programming for AIPS++ Previous: Introduction to Programming for AIPS++   Contents   Index
Please send questions or comments about AIPS++ to aips2-request@nrao.edu.
Copyright © 1995-2000 Associated Universities Inc., Washington, D.C.

Return to AIPS++ Home Page
2006-10-15