CMake/Examples
From KitwarePublic
< CMake
Jump to navigationJump to search
Revision as of 13:36, 17 November 2010 by Daviddoria (talk | contribs)
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
Check operating system
IF(WIN32)
...do something...
ELSE(WIN32)
...do something else...
ENDIF(WIN32)
View PATH
\verb|MESSAGE("$ENV{PATH}")
View a variable
MESSAGE("CMAKE_BINARY_DIR: ${CMAKE_BINARY_DIR}")
Set a variable
SET(VARIABLE VALUE)
Fix Mininum Version Error
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})
Set Link Directories
LINK_DIRECTORIES(${LINK_DIRECTORIES} /usr/local/lib)
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
\subsection{Global}
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D__WXGTK__")
Force g++
set(SPIN_SRCS main.c analyze.c ../spinImage/dhFaceSpinImage.c ../spinImage/faceSpinImage.c ../spinImage/normalizedSpinImage.c ../spinImage/spinImage.c ../spinImage/spinImageHashed.c ../spinImage/vectorArray.c)
add_executable(SpinRecognize ${SPIN_SRCS})
set_source_files_properties(${SPIN_SRCS} 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
SET(BUILD_PARAVIEW_PLUGIN ON CACHE STRING "Build Paraview plugin?" FORCE)
Linking to specific libraries
VXL
Add the path to your environment:
export VXLBIN="/home/doriad/bin/vxl"
FIND_PACKAGE(VXL REQUIRED)
INCLUDE(${VXL_CMAKE_DIR}/UseVXL.cmake)
VTK
Add the path to your environment:
export VTK_DIR="/home/doriad/bin/ParaView3/VTK"
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"
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})
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)