DGtal  1.5.beta
How to use DGtal in my own project ?

Table of Contents

Author(s) of this documentation:
David Coeurjolly

Once you have installed the DGtal library, this documentation describes the ways to link your project and the installed DGtal libs and making sure that the dependencies are correct.

We recommend to use the cmake system since using a simple command, all dependencies used to build your installed DGtal version will be associated and linked to your project.

Let us consider the following helloworld project with the C++ file helloworld.cpp :

#include <DGtal/base/Common.h>
int main(int argc, char** argv)
{
DGtal::trace.info() << "Helloworld from DGtal ";
DGtal::trace.emphase() << "(version "<< DGTAL_VERSION << ")"<< std::endl;
return 0;
}
std::ostream & emphase()
std::ostream & info()
Trace trace
Definition: Common.h:153
int main(int argc, char **argv)

This simple example just use the "trace" mechanism in DGtal but it requires helloworld.cpp to be linked to DGtal.

Using cmake tools

To compile this file with cmake, you have a CMakeLists.txt file that looks like:

PROJECT(Helloworld)
ADD_EXECUTABLE(helloworld helloworld)

When you installed DGtal, cmake created all the configuration files that you would need to import DGtal settings in your project in the "${CMAKE_INSTALL_PREFIX}/share/DGtal/CMake/" folder. For example on linux/unix systems, standard installation creates configuration files in /usr/local/share/DGtal/CMake/".

The most important file in this folder is DGtalConfig.cmake which contains everything you need. Just copy (or better, link) this file in your project source directory. Then, to link helloworld to DGtal, you just have to update the CMakeLists.txt such that:

PROJECT(Helloworld)
#Required in DGtal
CMAKE_MINIMUM_REQUIRED(VERSION 3.11)
set(CMAKE_CXX_STANDARD 11)
FIND_PACKAGE(DGtal REQUIRED)
INCLUDE_DIRECTORIES(${DGTAL_INCLUDE_DIRS})
LINK_DIRECTORIES(${DGTAL_LIBRARY_DIRS})
ADD_EXECUTABLE(helloworld helloworld.cpp)
TARGET_LINK_LIBRARIES(helloworld ${DGTAL_LIBRARIES})
DGtal is the top-level namespace which contains all DGtal functions and types.

Additionally, you can control the version of the DGtal library with for example :

FIND_PACKAGE(DGtal 0.3.1 REQUIRED)

that will produce an error if your DGtal installed library has an older version than 0.3.1.