GeographicLib: Getting started (original) (raw)

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

NOTE: The inclusion of

include_directories (${GeographicLib_INCLUDE_DIRS})

is only necessary if find_package found GeographicLib via the FindGeographicLib.cmake "module". However, it's OK to include this line even for "config" lookups.

The most import step in using cmake 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

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:

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 2.0 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.)

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:

#include

using namespace std;

const Geodesic& geod = Geodesic::WGS84();

double

lat1 = 40.6, lon1 = -73.8,

lat2 = 51.6, lon2 = -0.5;

double s12;

geod.Inverse(lat1, lon1, lat2, lon2, s12);

cout << s12 / 1000 << " km\n";

}

int main(int argc, const char *const argv[])

Header for GeographicLib::Geodesic class.

Math::real Inverse(real lat1, real lon1, real lat2, real lon2, real &s12, real &azi1, real &azi2, real &m12, real &M12, real &M21, real &S12) const

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:

cmake_minimum_required (VERSION 3.17.0) project (geodesictest)

find_package (GeographicLib REQUIRED)

if (NOT CMAKE_CONFIGURATION_TYPES AND NOT CMAKE_BUILD_TYPE)

Set a default build type for single-configuration cmake generators

if no build type is set.

set (CMAKE_BUILD_TYPE "Release") endif ()

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

If GeographicLib is not installed on your system, you can download and compile the library as part of your project using CMake's FetchContent with:

cmake_minimum_required (VERSION 3.17.0) project (geodesictest)

include(FetchContent) FetchContent_Declare(GeographicLib GIT_REPOSITORY https://github.com/geographiclib/geographiclib.git GIT_TAG r2.5 ) FetchContent_MakeAvailable(GeographicLib)

if (NOT CMAKE_CONFIGURATION_TYPES AND NOT CMAKE_BUILD_TYPE)

Set a default build type for single-configuration cmake generators

if no build type is set.

set (CMAKE_BUILD_TYPE "Release") endif ()

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

In this case, the library needs to be specified as GeographicLib::GeographicLib instead of ${GeographicLib_LIBRARIES}

The next steps are:

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

Back to Installing GeographicLib. Forward to Utility programs. Up to Contents.