Example: A Basic GPSTk Application

This example serves as an introduction to using the GPSTk. The code associated with the example can be found in the file gpstk/dev/examples/example1.cpp. Its purpose is to illustrate the most basic concepts required for effective use of the GPSTk.
     1  #include <iostream>
     2  #include "DayTime.hpp"
     3  using namespace std;
     4  using namespace gpstk;
     5  int main(int argc, char* argv[])
     6  {
     7     try {
     9        DayTime time;
    10        cout << "Hello world!" << endl;
    11        cout << "The current GPS week is " 
    12             << time.GPSfullweek() << endl;
    13        cout << "The day of the GPS week is " 
    14              << time.GPSday() << endl;
    15        cout << "The seconds of the GPS week is "
    16             << time.GPSsecond() << endl;
    17     }
    18     catch( Exception error)
    19     {
    20        cout << error << endl;
    21        exit(-1);
    22     }
    23     exit(0);
    24  }

Lines 1 and 2 include declarations for the classes used in this example. Note that line 2 uses quotes to include the class declaration for the GPSTk class DayTime. As you might expect, DayTime is a class that represents time. As time is an integral concept to GPS, this class plays an important part in the foundation of the GPSTk.

In lines 3 and 4 we define which namespaces we will use for the following code. In the GPSTk the concept of namespace is used extensively. Using namespaces is an excellent way to reduce name clashes.

Line 7 establishes a try-catch block, so that if a GPSTk-derived exception is thrown it is caught and displayed, in lines 18-22.

Lines 9 through 16 demonstrate use of the DayTime class. Note that the default constructor represents the current system time.

To compile the above and execute in Linux with gcc would look like the following.
user@host:~/gpstk/examples$ g++ -o example -I~/gpstk/src
 -L~/gpstk/src -lgpstk example1.cpp 
user@host:~/gpstk/examples$ ./example 
Hello world!
The current GPS week is 1281
The day of the GPS week is 5
The seconds of the GPS week is 499360

The compilation command would simplified if you installed the GPSTk as a system library. The -I and -L options would not be necessary. In that case the compilation command would be simply

g++ -o example -lgpstk example1.cpp

More example code can be found in the source distribution under gpstk/dev/examples, gpstk/dev/tests and gpstk/dev/apps.

Topic revision: r4 - 20 Dec 2006, RickMach

This site is powered by FoswikiCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Foswiki? Send feedback