CMake/Examples: Difference between revisions

From KitwarePublic
Jump to navigationJump to search
Line 94: Line 94:

=Set Link Directories=
=Set Link Directories=
This should only be used if you are linking to a library that does not have CMake support.
<source lang="cmake">
<source lang="cmake">

Revision as of 13:06, 22 April 2011

Welcome to the CMake Wiki Examples! These short snippets which show you how to perform many common CMake procedures. Please see [1] for the verbose documentation.

Please add examples as you find common procedures which are not explained here!


Set a CMake variable

<source lang="cmake"> SET(VARIABLE VALUE) </source>

View a CMake variable

<source lang="cmake"> MESSAGE("CMAKE_BINARY_DIR: ${CMAKE_BINARY_DIR}") </source>

View a system variable

<source lang="cmake"> MESSAGE("$ENV{PATH}") </source>

Check operating system

<source lang="cmake">

IF(WIN32) something...
ELSE(WIN32) something else...


Check operating system

<source lang="cmake"> IF(CMAKE_SYSTEM_NAME STREQUAL Linux) </source>



<source lang="cmake"> MESSAGE("Hello world") </source>


<source lang="cmake"> MESSAGE(FATAL_ERROR "An error occured.") </source>

Logical Operators

These operators work exactly as you would expect.


if(A AND B)


if(A OR B)


if(NOT A)


if(NOT(A AND B))

Finding Packages

Most of the time large packages will have their Find[PackageName].cmake file included in the cmake distribution. If that is the case, all you must do it:


Checking if a package was found

Most of the time the [PackageName]_FOUND variable is defined. You can use it like this: FIND_PACKAGE(Eigen3) if(EIGEN3_FOUND)

... do something ...


Suppress warnings

Sometimes you may want to optionally include a package. You can suppress the warning about the package not being found with: FIND_PACKAGE(Eigen3 QUIET)

Fix Mininum Version Error/Warning

<source lang="cmake"> cmake_minimum_required(VERSION 2.6.0 FATAL_ERROR) </source>

Look in the directory that the CMakeLists.txt file is for header and implementation files

<source lang="cmake"> INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}) </source>


<source lang="cmake"> INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}) </source>

Set Link Directories

This should only be used if you are linking to a library that does not have CMake support. <source lang="cmake"> LINK_DIRECTORIES(${LINK_DIRECTORIES} /usr/local/lib) </source>

Set Include Directories

This command adds a path to the include directories, you do NOT have to do the 'export' style ``keep everything that is here and add this one syntax. <source lang="cmake"> INCLUDE_DIRECTORIES(/some/directory) </source>

View the directories that are set

<source lang="cmake"> get_property(inc_dirs DIRECTORY PROPERTY INCLUDE_DIRECTORIES) message("inc_dirs = ${inc_dirs}") </source>

Automate configure and generate

Note 'cmake' instead of 'ccmake' (ccmake is curses cmake (curses is the terminal gui)) <source lang="cmake"> cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=/usr/mylocation ../ </source>

Compiler options

Set a cmake flag

<source lang="cmake"> ccmake ../../src/boost -DCMAKE_IS_EXPERIMENTAL=YES_I_KNOW </source>

Set a cpp flag

<source lang="cmake"> SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D__WXGTK__") </source>

Force g++

Ocassionally you will come across files with the extension .c that actually contain c++ code. CMake treats the file based on its extension. If you want to override this behavior (in this case, compile .c files with your c++ compiler), do the following:

<source lang="cmake"> set(MySources main.c anotherFile.c) add_executable(SpinRecognize ${MySource}) set_source_files_properties(${MySources} PROPERTIES LANGUAGE CXX) </source>


<source lang="cmake"> set_target_properties(myexe_target

                               PROPERTIES COMPILE_FLAGS "-Wall") 


Set the default build type

<source lang="cmake"> SET(CMAKE_BUILD_TYPE Debug CACHE STRING "default to debug" FORCE) </source>

Custom variable

Here is a list of supported variable types:


<source lang="cmake"> SET(BUILD_PARAVIEW_PLUGIN ON CACHE STRING "Build Paraview plugin?" FORCE) </source>


<source lang="cmake"> SET(DAI_WITH_BP ON CACHE BOOL "Belief Propagation" FORCE) </source>



  1. do something


Linking to specific libraries


Add the path to your environment: <source lang="text"> export ITK_DIR=/home/doriad/bin/ITK </source>

Then in the CMakeLists.txt use: <source lang="cmake"> FIND_PACKAGE(ITK REQUIRED) INCLUDE(${ITK_USE_FILE})

ADD_EXECUTABLE(CastImageFilter CastImageFilter.cxx) TARGET_LINK_LIBRARIES(CastImageFilter vtkHybrid ITKIO ITKBasicFilters ITKCommon ) </source>


Add the path to your environment: <source lang="text"> export VXLBIN="/home/doriad/bin/vxl" </source>

Then in the CMakeLists.txt use: <source lang="cmake"> FIND_PACKAGE(VXL REQUIRED) INCLUDE(${VXL_CMAKE_DIR}/UseVXL.cmake) </source>


Add the path to your environment: <source lang="text"> export VTK_DIR="/home/doriad/bin/VTK" </source>

Then in the CMakeLists.txt use: <source lang="cmake"> FIND_PACKAGE(VTK REQUIRED) INCLUDE(${VTK_USE_FILE}) </source>


Add to the environment: <source lang="text"> export BOOST_ROOT="/home/doriad/src/boost" export BOOST_LIBRARYDIR="/home/doriad/bin/boost/lib" </source>

Then in the CMakeLists.txt use: <source lang="cmake"> SET(Boost_USE_MULTITHREAD ON) #set a flag FIND_PACKAGE(Boost 1.34.1 COMPONENTS date_time filesystem) INCLUDE_DIRECTORIES(${INCLUDE_DIRECTORIES} ${Boost_INCLUDE_DIRS}) LINK_DIRECTORIES(${LINK_DIRECTORIES} ${Boost_LIBRARY_DIRS}) </source>


Add to the environment: <source lang="text"> export OpenCV_DIR=/home/doriad/bin/OpenCV </source>

Then in the CMakeLists.txt use: <source lang="cmake"> FIND_PACKAGE(OpenCV REQUIRED ) INCLUDE_DIRECTORIES( ${OPENCV_INCLUDE_DIR} )

ADD_EXECUTABLE(Scalar Scalar.cxx) TARGET_LINK_LIBRARIES(Scalar ${OpenCV_LIBS}) </source>

Get Help Using a Library

<source lang="cmake"> cmake --help-module FindBoost </source>

Add new libraries to CMake

<source lang="cmake"> /usr/share/cmake/Modules/FindOpenGL.cmake| </source>

Dependency Graph

<source lang="cmake"> ccmake ../src/Program/ --graphviz=test.graph dotty test.graph </source>


Run a specific test by number

e.g. Test 622 <source lang="cmake"> ctest -I 622,622 </source>

Run a range of tests

e.g. Test 622 to 625 <source lang="cmake"> ctest -I 622,625 </source>

Run a test by name

<source lang="cmake"> ctest -R "itkTransformPoint*"| </source>

Link to a library

<source lang="cmake"> ADD_EXECUTABLE(ColoredLines ColoredLines.cpp) TARGET_LINK_LIBRARIES(ColoredLines vtkHybrid) </source>

Create a library

<source lang="cmake"> add_library(MatlabLibrary ./MatlabDll/LidarK.cpp) </source>