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 <GeographicLib/LambertConformalConic.hpp>
using namespace GeographicLib;
find_package (GeographicLib 1.34 REQUIRED) include_directories (${GeographicLib_INCLUDE_DIRS}) add_definitions (${GeographicLib_DEFINITIONS}) add_executable (program source1.cpp source2.cpp) target_link_libraries (program ${GeographicLib_LIBRARIES})
add_definitions
line is only needed for Windows and can be omitted if you're using cmake version 2.8.11 or later.)mkdir BUILD cd BUILD cmake -G "Visual Studio 10" \ -D CMAKE_PREFIX_PATH=C:/pkg-vc10 \ -D CMAKE_PREFIX_PATH=C:/pkg-vc10/testgeographic \ ..
cmake --build . --config Release --target ALL_BUILD
C:/Program Files
and /usr/local
);CMAKE_PREFIX_PATH
(illustrated above);GeographicLib_DIR
(which is the directory under which GeographicLib is installed);GeographicLib_DIR
, which specifies 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).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 found, then the following cmake variables are set:
GeographicLib_FOUND
= 1GeographicLib_VERSION
= 1.38GeographicLib_INCLUDE_DIRS
GeographicLib_LIBRARIES
= one of the following two:GeographicLib_SHARED_LIBRARIES
= GeographicLibGeographicLib_STATIC_LIBRARIES
= GeographicLib_STATICGeographicLib_DEFINITIONS
= one of the following two:GeographicLib_SHARED_DEFINITIONS
= -DGEOGRAPHICLIB_SHARED_LIB=1GeographicLib_STATIC_DEFINITIONS
= -DGEOGRAPHICLIB_SHARED_LIB=0GeographicLib_LIBRARY_DIRS
GeographicLib_BINARY_DIRS
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
and GeographicLib_DEFINITIONS
are set to the shared versions, 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.)
g++ -c -g -O3 -funroll-loops -I/usr/local/include testprogram.cpp
C/C++ -> General -> Additional Include Directories = C:\pkg-vc10\GeographicLib\include
GEOGRAPHICLIB_SHARED_LIB=1
(or 0
), e.g., C/C++ -> Preprocessor -> Preprocessor Definitions = GEOGRAPHICLIB_SHARED_LIB=1
g++ -g -o testprogram testprogram.o -L/usr/local/lib -lGeographic
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-vc10\Geographic\lib
g++ -g -o testprogram testprogram.o -Wl,-rpath=/usr/local/lib \ -L/usr/local/lib -lGeographic
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
.Here is a very simple test code, which uses the Geodesic class:
// Small example of using the GeographicLib::Geodesic class #include <iostream> #include <GeographicLib/Geodesic.hpp> 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"; return 0; }
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 2.8.4) find_package (GeographicLib 1.34 REQUIRED) if (NOT MSVC) set (CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) endif () include_directories (${GeographicLib_INCLUDE_DIRS}) add_definitions (${GeographicLib_DEFINITIONS}) 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:
Here's a list of some of the abbreviations used here with links to the corresponding Wikipedia articles: