# How to Integrate ROOT into my project with CMake

**URL:** <https://root-forum.cern.ch/t/how-to-integrate-root-into-my-project-with-cmake/37175>\
**Category:** Howto\
**Created:** [December 12, 2019, 12:36pm UTC](https://root-forum.cern.ch/t/how-to-integrate-root-into-my-project-with-cmake/37175 "2019-12-12T12:36:14Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [December 12, 2019, 12:36pm UTC](https://root-forum.cern.ch/t/how-to-integrate-root-into-my-project-with-cmake/37175/1 "2019-12-12T12:36:14Z")

</div>

Integrating ROOT into your [CMake](http://www.cmake.org) based project should be straight forward. The main interface is the CMake command [find\_package(…)](http://www.cmake.org/cmake/help/latest/command/find_package.html), which defines the following standard variables:

| variable | type | description |
| --- | --- | --- |
| ROOT\_INCLUDE\_DIRS | PATH | include directories for the ROOT installation |
| ROOT\_LIBRARIES | LIST | Libraries to link against. The actual list of libraries is composed using the COMPONENTS listed in the find\_package(…) |
| ROOT\_DEFINITIONS | STRING | Compile definitions needed to use ROOT |
| ROOT\_CXX\_FLAGS | STRING | C++ compiler flags used to build ROOT |
| ROOT\_CC\_FLAGS | STRING | C compiler flags used to build ROOT |
| ROOT\_\<library\>\_LIBRARY | PATH | Full path for each of the ROOT libraries listed in COMPONENTS |
| ROOT\_\<command\>\_CMD | PATH | Full path for each ROOT executable (rootcling, root, hadd, etc.) |
| ROOT\_\<option\>\_FOUND | BOOL | True for each enabled build option (e.g. cocoa, python, xrootd, etc.) |
| ROOT\_FOUND | BOOL | True if the ROOT package has been found |
| ROOT\_USE\_FILE | PATH | Path to a CMake module which makes use of the previous variables and loads modules with useful macros or functions such as ROOT\_GENERATE\_DICTIONARY |

## Adding Additional libraries

The user can force additional ROOT libraries in the `ROOT_LIBRARIES` variable using the COMPONENTS option in `find_package(...)` command. For example to add the `RooStats` library the user can specify it as an extra component (the name of the component is the name of the library without any library prefix or suffix).

```auto
find_package(ROOT COMPONENTS RooStats)

```

## Useful commands

ROOT provides a number of CMake macros/functions that are used internally but can also be used by projects layered on top of ROOT.

### ROOT\_GENERATE\_DICTIONARY

Generate dictionary for a set of header files (convenient wrapper on top of rootcling command).

```auto
ROOT_GENERATE_DICTIONARY( dictionary headerfiles ... [STAGE1] 
                         LINKDEF linkdeffiles ...
                         [MODULE module] [DEPENDENCIES dep1 dep2 ...]
                         [OPTIONS opt1 opt2 ...] )

```

The file {dictionary}.cxx and {dictionary}.pcm are created using the header and linkdef files calling the rootcling command. Optionally OPTIONS and MODULE can be provided to customize the way the rootcling is called and the name for the .pcm files.

### REFLEX\_GENERATE\_DICTIONARY

Generate dictionary for a set of header files (convenient wrapper on top of rootcling command).

```auto
REFLEX_GENERATE_DICTIONARY( dictionary headerfiles ...
                          SELECTION selection ...
                          [DEPENDENCIES dep1 dep2 ...]
                          [OPTIONS opt1 opt2 ...] )

```

The file {dictionary}.cxx and {dictionary}.pcm are created using the headers and selection file calling the genreflex command. Optionally OPTIONS can be provided to customize the way the genreflex is called.

## Full example (Event project)

Following is an example of a project that creates a library including a dictionary and an executable.

```auto
# CMakeLists.txt for event package. It creates a library with dictionary and a main program
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(event)

# You need to tell CMake where to find the ROOT installation. This can be done in a number of ways:
# - ROOT built with classic configure/make use the provided $ROOTSYS/etc/cmake/FindROOT.cmake
# - ROOT built with CMake. Add in CMAKE_PREFIX_PATH the installation prefix for ROOT
list(APPEND CMAKE_PREFIX_PATH $ENV{ROOTSYS})

#---Locate the ROOT package and defines a number of variables (e.g. ROOT_INCLUDE_DIRS)
find_package(ROOT REQUIRED COMPONENTS RIO Net)

#---Define useful ROOT functions and macros (e.g. ROOT_GENERATE_DICTIONARY)
include(${ROOT_USE_FILE})

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
ROOT_GENERATE_DICTIONARY(G__Event Event.h LINKDEF EventLinkDef.h)

#---Create a shared library with geneated dictionary
add_library(Event SHARED Event.cxx G__Event.cxx)
target_link_libraries(Event ${ROOT_LIBRARIES})

#---Create a main program using the library
add_executable(Main MainEvent.cxx)
target_link_libraries(Main Event)

```
