GeographicLib  1.51
Getting started
Back to Installing GeographicLib. Forward to Utility programs. Up to Contents.

Much (but not all!) of the useful functionality of GeographicLib is available via simple command line utilities. Interfaces to some of them are available via the web. See Utility programs for documentation on these.

In order to use GeographicLib from C++ code, you will need to

  • Include the header files for the GeographicLib classes in your code. E.g.,
    Header for GeographicLib::LambertConformalConic class.
  • Include the GeographicLib:: namespace prefix to the GeographicLib classes, or include
    using namespace GeographicLib;
    Namespace for GeographicLib.
    Definition: Accumulator.cpp:12
    in your code.
  • Finally compile and link your code. You have two options here.
    • Use cmake to build your package. If you are familiar with cmake this typically will be far the simplest option.
    • Set the include paths and linking options "manually".
  • Building your code with cmake. In brief, the necessary steps are:
    • include in your CMakeLists.txt files
          find_package (GeographicLib REQUIRED)
          add_executable (program source1.cpp source2.cpp)
          target_link_libraries (program ${GeographicLib_LIBRARIES}) 
    • configure your package, e.g., with
          mkdir BUILD
          cd BUILD
          cmake -G "Visual Studio 14" -A x64 \
            -D CMAKE_PREFIX_PATH="C:/Program Files" \
            -D CMAKE_INSTALL_PREFIX="C:/Program Files/testgeographic" \
            .. 
      Note that you almost always want to configure and build your code somewhere other than the source directory (in this case, we use the BUILD subdirectory). Also, on Windows, make sure that the version of Visual Studio (14 in the example above) architecture (x64 in the example above) is compatible with that used to build GeographicLib. In this example, it's not necessary to specify CMAKE_PREFIX_PATH, because C:/Program Files is one of the system paths which is searched automatically.
    • build your package. On Linux and MacOS this usually involves just running make. On Windows, you can load the solution file created by cmake into Visual Studio; alternatively, you can get cmake to run build your code with
          cmake --build . --config Release --target ALL_BUILD 
      You might also want to install your package (using "make install" or build the "INSTALL" target with the command above).

The most import step is the find_package command. The cmake documentation describes the locations searched by find_package (the appropriate rule for GeographicLib are those for "Config" mode lookups). In brief, the locations that are searched are (from least specific to most specific, i.e., in reverse order) are

  • under the system paths, i.e., locations such as C:/Program Files and /usr/local);
  • frequently, it's necessary to search within a "package directory" (or set of directories) for external dependencies; this is given by a (semicolon separated) list of directories specified by the cmake variable CMAKE_PREFIX_PATH (illustrated above);
  • the package directory for GeographicLib can be overridden with the environment variable GeographicLib_DIR (which is the directory under which GeographicLib is installed);
  • finally, if you need to point to a particular build of GeographicLib, define the cmake variable GeographicLib_DIR, which specifies the absolute path of the directory containing the configuration file geographiclib-config.cmake (for debugging this may be the top-level build directory, as opposed to installation directory, for GeographicLib).

Typically, specifying nothing or CMAKE_PREFIX_PATH suffices. However the two GeographicLib_DIR variables allow for a specific version to be chosen. On Windows systems (with Visual Studio), find_package will only find versions of GeographicLib built with the right version of the compiler. (If you used a non-cmake method of installing GeographicLib, you can try copying cmake/FindGeographicLib.cmake to somewhere in your CMAKE_MODULE_PATH in order for find_package to work. However, this method has not been thoroughly tested.)

If GeographicLib is not found, check the values of GeographicLib_CONSIDERED_CONFIGS and GeographicLib_CONSIDERED_VERSIONS; these list the configuration files and corresponding versions which were considered by find_package.

If GeographicLib is found, then the following cmake variables are set:

  • GeographicLib_FOUND = 1
  • GeographicLib_VERSION = 1.51
  • GeographicLib_INCLUDE_DIRS
  • GeographicLib_LIBRARIES = one of the following two:
  • GeographicLib_SHARED_LIBRARIES = GeographicLib_SHARED
  • GeographicLib_STATIC_LIBRARIES = GeographicLib_STATIC
  • GeographicLib_LIBRARY_DIRS
  • GeographicLib_BINARY_DIRS
  • GEOGRAPHICLIB_DATA = value of this compile-time parameter

Either of GeographicLib_SHARED_LIBRARIES or GeographicLib_STATIC_LIBRARIES may be empty, if that version of the library is unavailable. If you require a specific version, SHARED or STATIC, of the library, add a COMPONENTS clause to find_package, e.g.,

    find_package (GeographicLib 1.34 REQUIRED COMPONENTS SHARED) 

causes only packages which include the shared library to be found. If the package includes both versions of the library, then GeographicLib_LIBRARIES is set to the shared version, unless you include

    set (GeographicLib_USE_STATIC_LIBS ON) 

before the find_package command. You can check whether GeographicLib_LIBRARIES refers to the shared or static library with

    get_target_property(_LIBTYPE ${GeographicLib_LIBRARIES} TYPE) 

which results in _LIBTYPE being set to SHARED_LIBRARY or STATIC_LIBRARY. On Windows, cmake takes care of linking to the release or debug version of the library as appropriate. (This assumes that the Release and Debug versions of the libraries were built and installed. This is true for the Windows binary installer for GeographicLib version 1.34 and later.)

  • Here are the steps to compile and link your code using GeographicLib "manually".
    • Tell the compiler where to find the header files. With g++ and with /usr/local specified as the installation directory, this is accomplished with
          g++ -c -g -O3 -I/usr/local/include testprogram.cpp
      With Visual Studio, specify the include directory in the IDE via, e.g.,
          C/C++ -> General -> Additional Include Directories = C:\pkg-vc14-x64\GeographicLib\include
    • If using the shared (or static) library with Visual Studio, define the macro GEOGRAPHICLIB_SHARED_LIB=1 (or 0), e.g.,
          C/C++ -> Preprocessor -> Preprocessor Definitions = GEOGRAPHICLIB_SHARED_LIB=1
      This is only needed for Windows systems when both shared and static libraries have been installed. (If you configure your package with cmake, this definition is added automatically.)
    • Tell the linker the name, Geographic, and location of the library. Using g++ as the linker, you would use
          g++ -g -o testprogram testprogram.o -L/usr/local/lib -lGeographic
      With Visual Studio, you supply this information in the IDE via, e.g.,
          Linker -> Input -> Additional Dependencies = Geographic-i.lib (for shared library)
          Linker -> Input -> Additional Dependencies = Geographic.lib (for static library)
          Linker -> General -> Additional Library Directories = C:\pkg-vc14-x64\Geographic\lib
      Note that the library name is Geographic and not GeographicLib. For the Debug version of your program on Windows add "_d" to the library, e.g., Geographic_d-i.lib or Geographic_d.lib.
    • Tell the runtime environment where to find the shared library (assuming you compiled Geographic as a shared library). With g++, this is accomplished by modifying the link line above to read
          g++ -g -o testprogram testprogram.o -Wl,-rpath=/usr/local/lib \
            -L/usr/local/lib -lGeographic
      (There are two other ways to specify the location of shared libraries at runtime: (1) define the environment variable LD_LIBRARY_PATH to be a colon-separated list of directories to search; (2) as root, specify /usr/local/lib as a directory searched by ldconfig(8).) On Windows, you need to ensure that Geographic.dll or Geographic_d.dll is in the same directory as your executable or else include the directory containing the dll in your PATH.
    • You can also use the pkg-config utility to specify compile and link flags. This requires that pkg-config be installed and that it's configured to search, e.g., /usr/local/lib/pgkconfig; if not, define the environment variable PKG_CONFIG_PATH to include this directory. The compile and link steps under Linux would typically be
          g++ -c -g -O3 `pkg-config --cflags geographiclib` testprogram.cpp
          g++ -g -o testprogram testprogram.o `pkg-config --libs geographiclib`

Here is a very simple test code, which uses the Geodesic class:

// Small example of using the GeographicLib::Geodesic class
#include <iostream>
using namespace std;
using namespace GeographicLib;
int main() {
const Geodesic& geod = Geodesic::WGS84();
// Distance from JFK to LHR
double
lat1 = 40.6, lon1 = -73.8, // JFK Airport
lat2 = 51.6, lon2 = -0.5; // LHR Airport
double s12;
geod.Inverse(lat1, lon1, lat2, lon2, s12);
cout << s12 / 1000 << " km\n";
}
int main(int argc, const char *const argv[])
Definition: CartConvert.cpp:29
Header for GeographicLib::Geodesic class.
static const Geodesic & WGS84()
Definition: Geodesic.cpp:89

This example is examples/example-Geodesic-small.cpp. If you compile, link, and run it according to the instructions above, it should print out

  5551.76 km 

Here is a complete CMakeList.txt files you can use to build this test code using the installed library:

project (geodesictest)
cmake_minimum_required (VERSION 3.1.0)

find_package (GeographicLib REQUIRED)

if (NOT MSVC)
  set (CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
endif ()

add_executable (${PROJECT_NAME} example-Geodesic-small.cpp)
target_link_libraries (${PROJECT_NAME} ${GeographicLib_LIBRARIES})

if (MSVC)
  get_target_property (_LIBTYPE ${GeographicLib_LIBRARIES} TYPE)
  if (_LIBTYPE STREQUAL "SHARED_LIBRARY")
    # On Windows systems, copy the shared library to build directory
    add_custom_command (TARGET ${PROJECT_NAME} POST_BUILD
      COMMAND ${CMAKE_COMMAND} -E
      copy $<TARGET_FILE:${GeographicLib_LIBRARIES}> ${CMAKE_CFG_INTDIR}
      COMMENT "Copying shared library for GeographicLib")
  endif ()
endif () 

The next steps are:

  • Learn about and run the Utility programs.
  • Read the section, Code organization, for an overview of the library.
  • Browse the Class List for full documentation on the classes in the library.
  • Look at the example code in the examples directory. Each file provides a very simple standalone example of using one GeographicLib class. These are included in the descriptions of the classes.
  • Look at the source code for the utilities in the tools directory for more examples of using GeographicLib from C++ code, e.g., GeodesicProj.cpp is a program to performing various geodesic projections.

Here's a list of some of the abbreviations used here with links to the corresponding Wikipedia articles:

  • WGS84, World Geodetic System 1984.
  • UTM, Universal Transverse Mercator coordinate system.
  • UPS, Universal Polar Stereographic coordinate system.
  • MGRS, Military Grid Reference System.
  • EGM, Earth Gravity Model.
  • WMM, World Magnetic Model.
  • IGRF, International Geomagnetic Reference Field.
Back to Installing GeographicLib. Forward to Utility programs. Up to Contents.