Difference between revisions of "CMake/Examples"

From KitwarePublic
Jump to navigationJump to search
(Created page with "Welcome to the CMake Wiki Examples! These short snippets which show you how to perform many common CMake procedures. Please see [http://www.cmake.org/cmake/help/cmake-2-8-docs.ht...")
 
(Replace content with link to new CMake community wiki)
 
(39 intermediate revisions by 3 users not shown)
Line 1: Line 1:
Welcome to the CMake Wiki Examples! These short snippets which show you how to perform many common CMake procedures. Please see [http://www.cmake.org/cmake/help/cmake-2-8-docs.html] for the verbose documentation.
+
{{CMake/Template/Moved}}
  
Please add examples as you find common procedures which are not explained here!
+
This page has moved [https://gitlab.kitware.com/cmake/community/wikis/doc/cmake/Examples here].
 
 
 
 
=Force g++=
 
<source lang="cmake">
 
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)
 
</source>
 
 
 
=Check operating system=
 
<source lang="cmake">
 
IF(WIN32)
 
  ...do something...
 
ELSE(WIN32)
 
  ...do something else...
 
ENDIF(WIN32)
 
</source>
 
 
 
 
 
 
=View PATH=
 
<source lang="cmake">
 
\verb|MESSAGE("$ENV{PATH}")
 
</source>
 
 
 
=View a variable=
 
<source lang="cmake">
 
MESSAGE("CMAKE_BINARY_DIR: ${CMAKE_BINARY_DIR}")
 
</source>
 
 
 
 
 
=Set a variable=
 
<source lang="cmake">
 
SET(VARIABLE VALUE)
 
</source>
 
 
 
 
 
=Fix Mininum Version Error=
 
<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>
 
 
 
 
 
=Set Link Directories=
 
<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>
 
 
 
 
 
=Set a cmake flag=
 
<source lang="cmake">
 
ccmake ../../src/boost -DCMAKE_IS_EXPERIMENTAL=YES_I_KNOW
 
</source>
 
 
 
=Set a cpp flag=
 
\subsection{Global}
 
<source lang="cmake">
 
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D__WXGTK__")
 
</source>
 
 
 
==Per-target==
 
<source lang="cmake">
 
set_target_properties(myexe_target
 
                                PROPERTIES COMPILE_FLAGS "-Wall")
 
</source>
 
 
 
=Set the default build type=
 
<source lang="cmake">
 
SET(CMAKE_BUILD_TYPE Debug CACHE STRING "default to debug" FORCE)
 
</source>
 
 
 
=Custom variable=
 
<source lang="cmake">
 
SET(BUILD_PARAVIEW_PLUGIN ON CACHE STRING "Build Paraview plugin?" FORCE)
 
</source>
 
 
 
=Linking to specific libraries=
 
==VXL==
 
Add the path to your environment:
 
<source lang="cmake">
 
export VXLBIN="/home/doriad/bin/vxl"
 
</source>
 
 
 
<source lang="cmake">
 
FIND_PACKAGE(VXL REQUIRED)
 
INCLUDE(${VXL_CMAKE_DIR}/UseVXL.cmake)
 
</source>
 
 
 
==VTK==
 
Add the path to your environment:
 
<source lang="cmake">
 
export VTK_DIR="/home/doriad/bin/ParaView3/VTK"
 
</source>
 
 
 
<source lang="cmake">
 
FIND_PACKAGE(VTK REQUIRED)
 
INCLUDE(${VTK_USE_FILE})
 
</source>
 
 
 
==Boost==
 
 
 
Add to the environment:
 
<source lang="cmake">
 
export BOOST_ROOT="/home/doriad/src/boost"
 
export BOOST_LIBRARYDIR="/home/doriad/bin/boost/lib"
 
</source>
 
 
 
<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>
 
 
 
==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>
 
 
 
=CTest=
 
==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>
 

Latest revision as of 10:40, 30 April 2018


The CMake community Wiki has moved to the Kitware GitLab Instance.

This page has moved here.