Difference between revisions of "CMake/Examples"
Daviddoria (talk | contribs) |
Daviddoria (talk | contribs) (Move everything down a level so that no level1 headings are present.) |
||
Line 3: | Line 3: | ||
Please add examples as you find common procedures which are not explained here! | Please add examples as you find common procedures which are not explained here! | ||
=Basics= | ==Basics== | ||
==Set a CMake variable== | ===Set a CMake variable=== | ||
<source lang="cmake"> | <source lang="cmake"> | ||
SET(VARIABLE VALUE) | SET(VARIABLE VALUE) | ||
</source> | </source> | ||
==View a CMake variable== | ===View a CMake variable=== | ||
<source lang="cmake"> | <source lang="cmake"> | ||
MESSAGE("CMAKE_BINARY_DIR: ${CMAKE_BINARY_DIR}") | MESSAGE("CMAKE_BINARY_DIR: ${CMAKE_BINARY_DIR}") | ||
</source> | </source> | ||
==View a system variable== | ===View a system variable=== | ||
<source lang="cmake"> | <source lang="cmake"> | ||
MESSAGE("$ENV{PATH}") | MESSAGE("$ENV{PATH}") | ||
</source> | </source> | ||
==Check operating system== | ===Check operating system=== | ||
<source lang="cmake"> | <source lang="cmake"> | ||
IF(WIN32) | IF(WIN32) | ||
Line 29: | Line 29: | ||
</source> | </source> | ||
==Check operating system== | ===Check operating system=== | ||
<source lang="cmake"> | <source lang="cmake"> | ||
IF(CMAKE_SYSTEM_NAME STREQUAL Linux) | IF(CMAKE_SYSTEM_NAME STREQUAL Linux) | ||
</source> | </source> | ||
=Outputs= | ==Outputs== | ||
==Message== | ===Message=== | ||
<source lang="cmake"> | <source lang="cmake"> | ||
MESSAGE("Hello world") | MESSAGE("Hello world") | ||
</source> | </source> | ||
==Error== | ===Error=== | ||
<source lang="cmake"> | <source lang="cmake"> | ||
MESSAGE(FATAL_ERROR "An error occured.") | MESSAGE(FATAL_ERROR "An error occured.") | ||
</source> | </source> | ||
=Logical Operators= | ==Logical Operators== | ||
These operators work exactly as you would expect. | These operators work exactly as you would expect. | ||
==AND== | ===AND=== | ||
if(A AND B) | if(A AND B) | ||
==OR== | ===OR=== | ||
if(A OR B) | if(A OR B) | ||
==NOT== | ===NOT=== | ||
if(NOT A) | if(NOT A) | ||
==Compound== | ===Compound=== | ||
if(NOT(A AND B)) | if(NOT(A AND B)) | ||
=Finding Packages= | ==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: | 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: | ||
FIND_PACKAGE(Eigen3) | FIND_PACKAGE(Eigen3) | ||
==Checking if a package was found== | ===Checking if a package was found=== | ||
Most of the time the [PackageName]_FOUND variable is defined. You can use it like this: | Most of the time the [PackageName]_FOUND variable is defined. You can use it like this: | ||
FIND_PACKAGE(Eigen3) | FIND_PACKAGE(Eigen3) | ||
Line 72: | Line 72: | ||
endif(EIGEN3_FOUND) | endif(EIGEN3_FOUND) | ||
==Suppress warnings== | ===Suppress warnings=== | ||
Sometimes you may want to optionally include a package. You can suppress the warning about the package not being found with: | 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) | FIND_PACKAGE(Eigen3 QUIET) | ||
=Fix Mininum Version Error/Warning= | ==Fix Mininum Version Error/Warning== | ||
<source lang="cmake"> | <source lang="cmake"> | ||
cmake_minimum_required(VERSION 2.6.0 FATAL_ERROR) | cmake_minimum_required(VERSION 2.6.0 FATAL_ERROR) | ||
Line 82: | Line 82: | ||
=Look in the directory that the CMakeLists.txt file is for header and implementation files= | ==Look in the directory that the CMakeLists.txt file is for header and implementation files== | ||
<source lang="cmake"> | <source lang="cmake"> | ||
INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}) | INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}) | ||
Line 93: | Line 93: | ||
</source> | </source> | ||
=Set Link Directories= | ==Set Link Directories== | ||
If a library you are using does not have a [LibraryName].cmake, you can do | If a library you are using does not have a [LibraryName].cmake, you can do | ||
<source lang="cmake"> | <source lang="cmake"> | ||
Line 105: | Line 105: | ||
If it does not, it can be set through the CMake GUI. | If it does not, it can be set through the CMake GUI. | ||
=Set Include Directories= | ==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. | 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"> | <source lang="cmake"> | ||
Line 111: | Line 111: | ||
</source> | </source> | ||
=View the directories that are set= | ==View the directories that are set== | ||
<source lang="cmake"> | <source lang="cmake"> | ||
get_property(inc_dirs DIRECTORY PROPERTY INCLUDE_DIRECTORIES) | get_property(inc_dirs DIRECTORY PROPERTY INCLUDE_DIRECTORIES) | ||
Line 117: | Line 117: | ||
</source> | </source> | ||
=Automate configure and generate= | ==Automate configure and generate== | ||
Note 'cmake' instead of 'ccmake' (ccmake is curses cmake (curses is the terminal gui)) | Note 'cmake' instead of 'ccmake' (ccmake is curses cmake (curses is the terminal gui)) | ||
<source lang="cmake"> | <source lang="cmake"> | ||
Line 123: | Line 123: | ||
</source> | </source> | ||
=Compiler options= | ==Compiler options== | ||
==Set a cmake flag== | ===Set a cmake flag=== | ||
<source lang="cmake"> | <source lang="cmake"> | ||
ccmake ../../src/boost -DCMAKE_IS_EXPERIMENTAL=YES_I_KNOW | ccmake ../../src/boost -DCMAKE_IS_EXPERIMENTAL=YES_I_KNOW | ||
</source> | </source> | ||
==Set a cpp flag== | ===Set a cpp flag=== | ||
<source lang="cmake"> | <source lang="cmake"> | ||
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D__WXGTK__") | SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D__WXGTK__") | ||
</source> | </source> | ||
==Force g++== | ===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: | 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: | ||
Line 143: | Line 143: | ||
</source> | </source> | ||
==Per-target== | ===Per-target=== | ||
<source lang="cmake"> | <source lang="cmake"> | ||
set_target_properties(myexe_target | set_target_properties(myexe_target | ||
Line 149: | Line 149: | ||
</source> | </source> | ||
=Set the default build type= | ==Set the default build type== | ||
<source lang="cmake"> | <source lang="cmake"> | ||
SET(CMAKE_BUILD_TYPE Debug CACHE STRING "default to debug" FORCE) | SET(CMAKE_BUILD_TYPE Debug CACHE STRING "default to debug" FORCE) | ||
</source> | </source> | ||
=Custom variable= | ==Custom variable== | ||
Here is a list of supported variable types: http://www.cmake.org/cmake/help/cmake2.6docs.html#command:set | Here is a list of supported variable types: http://www.cmake.org/cmake/help/cmake2.6docs.html#command:set | ||
==String== | ===String=== | ||
<source lang="cmake"> | <source lang="cmake"> | ||
SET(BUILD_PARAVIEW_PLUGIN ON CACHE STRING "Build Paraview plugin?" FORCE) | SET(BUILD_PARAVIEW_PLUGIN ON CACHE STRING "Build Paraview plugin?" FORCE) | ||
</source> | </source> | ||
==Bool== | ===Bool=== | ||
<source lang="cmake"> | <source lang="cmake"> | ||
SET(DAI_WITH_BP ON CACHE BOOL "Belief Propagation" FORCE) | SET(DAI_WITH_BP ON CACHE BOOL "Belief Propagation" FORCE) | ||
</source> | </source> | ||
===Comparison=== | ====Comparison==== | ||
<source lang="cmake"> | <source lang="cmake"> | ||
if(DAI_WITH_BP EQUALS ON) | if(DAI_WITH_BP EQUALS ON) | ||
Line 182: | Line 182: | ||
</source> | </source> | ||
=Linking to specific libraries= | ==Linking to specific libraries== | ||
==ITK== | ===ITK=== | ||
Add the path to your environment: | Add the path to your environment: | ||
<source lang="text"> | <source lang="text"> | ||
Line 201: | Line 201: | ||
</source> | </source> | ||
==VXL== | ===VXL=== | ||
Add the path to your environment: | Add the path to your environment: | ||
<source lang="text"> | <source lang="text"> | ||
Line 213: | Line 213: | ||
</source> | </source> | ||
==VTK== | ===VTK=== | ||
Add the path to your environment: | Add the path to your environment: | ||
<source lang="text"> | <source lang="text"> | ||
Line 225: | Line 225: | ||
</source> | </source> | ||
==Boost== | ===Boost=== | ||
Add to the environment: | Add to the environment: | ||
Line 241: | Line 241: | ||
</source> | </source> | ||
==OpenCV== | ===OpenCV=== | ||
Add to the environment: | Add to the environment: | ||
Line 257: | Line 257: | ||
</source> | </source> | ||
==Get Help Using a Library== | ===Get Help Using a Library=== | ||
<source lang="cmake"> | <source lang="cmake"> | ||
cmake --help-module FindBoost | cmake --help-module FindBoost | ||
</source> | </source> | ||
==Add new libraries to CMake== | ===Add new libraries to CMake=== | ||
<source lang="cmake"> | <source lang="cmake"> | ||
/usr/share/cmake/Modules/FindOpenGL.cmake| | /usr/share/cmake/Modules/FindOpenGL.cmake| | ||
</source> | </source> | ||
=Dependency Graph= | ==Dependency Graph== | ||
<source lang="cmake"> | <source lang="cmake"> | ||
ccmake ../src/Program/ --graphviz=test.graph | ccmake ../src/Program/ --graphviz=test.graph | ||
Line 273: | Line 273: | ||
</source> | </source> | ||
=CTest= | ==CTest== | ||
==Run a specific test by number== | ===Run a specific test by number=== | ||
e.g. Test 622 | e.g. Test 622 | ||
<source lang="cmake"> | <source lang="cmake"> | ||
Line 280: | Line 280: | ||
</source> | </source> | ||
==Run a range of tests== | ===Run a range of tests=== | ||
e.g. Test 622 to 625 | e.g. Test 622 to 625 | ||
<source lang="cmake"> | <source lang="cmake"> | ||
Line 286: | Line 286: | ||
</source> | </source> | ||
==Run a test by name== | ===Run a test by name=== | ||
<source lang="cmake"> | <source lang="cmake"> | ||
ctest -R "itkTransformPoint*"| | ctest -R "itkTransformPoint*"| | ||
</source> | </source> | ||
=Link to a library= | ==Link to a library== | ||
<source lang="cmake"> | <source lang="cmake"> | ||
ADD_EXECUTABLE(ColoredLines ColoredLines.cpp) | ADD_EXECUTABLE(ColoredLines ColoredLines.cpp) | ||
Line 298: | Line 298: | ||
=Create a library= | ==Create a library== | ||
<source lang="cmake"> | <source lang="cmake"> | ||
add_library(MatlabLibrary ./MatlabDll/LidarK.cpp) | add_library(MatlabLibrary ./MatlabDll/LidarK.cpp) | ||
</source> | </source> |
Revision as of 17:03, 2 May 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!
Basics
Set a CMake variable
SET(VARIABLE VALUE)
View a CMake variable
MESSAGE("CMAKE_BINARY_DIR: ${CMAKE_BINARY_DIR}")
View a system variable
MESSAGE("$ENV{PATH}")
Check operating system
IF(WIN32)
...do something...
ELSE(WIN32)
...do something else...
ENDIF(WIN32)
Check operating system
IF(CMAKE_SYSTEM_NAME STREQUAL Linux)
Outputs
Message
MESSAGE("Hello world")
Error
MESSAGE(FATAL_ERROR "An error occured.")
Logical Operators
These operators work exactly as you would expect.
AND
if(A AND B)
OR
if(A OR B)
NOT
if(NOT A)
Compound
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:
FIND_PACKAGE(Eigen3)
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 ...
endif(EIGEN3_FOUND)
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
cmake_minimum_required(VERSION 2.6.0 FATAL_ERROR)
Look in the directory that the CMakeLists.txt file is for header and implementation files
INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR})
or
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})
Set Link Directories
If a library you are using does not have a [LibraryName].cmake, you can do
find_library(OPENSSL_LIB ssl $ENV{OPENSSL_LIB_PATH})
This will check the environment for a variable named OPENSSL_LIB_PATH. If it exists, you can then use
target_link_libraries(mytarget ${OPENSSL_LIB})
If it does not, it can be set through the CMake GUI.
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.
INCLUDE_DIRECTORIES(/some/directory)
View the directories that are set
get_property(inc_dirs DIRECTORY PROPERTY INCLUDE_DIRECTORIES)
message("inc_dirs = ${inc_dirs}")
Automate configure and generate
Note 'cmake' instead of 'ccmake' (ccmake is curses cmake (curses is the terminal gui))
cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=/usr/mylocation ../
Compiler options
Set a cmake flag
ccmake ../../src/boost -DCMAKE_IS_EXPERIMENTAL=YES_I_KNOW
Set a cpp flag
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D__WXGTK__")
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:
set(MySources main.c anotherFile.c)
add_executable(SpinRecognize ${MySource})
set_source_files_properties(${MySources} PROPERTIES LANGUAGE CXX)
Per-target
set_target_properties(myexe_target
PROPERTIES COMPILE_FLAGS "-Wall")
Set the default build type
SET(CMAKE_BUILD_TYPE Debug CACHE STRING "default to debug" FORCE)
Custom variable
Here is a list of supported variable types: http://www.cmake.org/cmake/help/cmake2.6docs.html#command:set
String
SET(BUILD_PARAVIEW_PLUGIN ON CACHE STRING "Build Paraview plugin?" FORCE)
Bool
SET(DAI_WITH_BP ON CACHE BOOL "Belief Propagation" FORCE)
Comparison
if(DAI_WITH_BP EQUALS ON)
#do something
endif(DAI_WITH_BP EQUALS ON)
This is equivalent to
if(DAI_WITH_BP)
#do something
endif(DAI_WITH_BP)
Linking to specific libraries
ITK
Add the path to your environment:
export ITK_DIR=/home/doriad/bin/ITK
Then in the CMakeLists.txt use:
FIND_PACKAGE(ITK REQUIRED)
INCLUDE(${ITK_USE_FILE})
ADD_EXECUTABLE(CastImageFilter CastImageFilter.cxx)
TARGET_LINK_LIBRARIES(CastImageFilter
vtkHybrid
ITKIO ITKBasicFilters ITKCommon
)
VXL
Add the path to your environment:
export VXLBIN="/home/doriad/bin/vxl"
Then in the CMakeLists.txt use:
FIND_PACKAGE(VXL REQUIRED)
INCLUDE(${VXL_CMAKE_DIR}/UseVXL.cmake)
VTK
Add the path to your environment:
export VTK_DIR="/home/doriad/bin/VTK"
Then in the CMakeLists.txt use:
FIND_PACKAGE(VTK REQUIRED)
INCLUDE(${VTK_USE_FILE})
Boost
Add to the environment:
export BOOST_ROOT="/home/doriad/src/boost"
export BOOST_LIBRARYDIR="/home/doriad/bin/boost/lib" #maybe don't have to set this one? Verify.
Then in the CMakeLists.txt use:
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})
OpenCV
Add to the environment:
export OpenCV_DIR=/home/doriad/bin/OpenCV
Then in the CMakeLists.txt use:
FIND_PACKAGE(OpenCV REQUIRED )
INCLUDE_DIRECTORIES( ${OPENCV_INCLUDE_DIR} )
ADD_EXECUTABLE(Scalar Scalar.cxx)
TARGET_LINK_LIBRARIES(Scalar ${OpenCV_LIBS})
Get Help Using a Library
cmake --help-module FindBoost
Add new libraries to CMake
/usr/share/cmake/Modules/FindOpenGL.cmake|
Dependency Graph
ccmake ../src/Program/ --graphviz=test.graph
dotty test.graph
CTest
Run a specific test by number
e.g. Test 622
ctest -I 622,622
Run a range of tests
e.g. Test 622 to 625
ctest -I 622,625
Run a test by name
ctest -R "itkTransformPoint*"|
Link to a library
ADD_EXECUTABLE(ColoredLines ColoredLines.cpp)
TARGET_LINK_LIBRARIES(ColoredLines vtkHybrid)
Create a library
add_library(MatlabLibrary ./MatlabDll/LidarK.cpp)