100% found this document useful (2 votes)
2K views1 page

Cmake Cheat Sheet

Cmake Cheat Sheet. This is a cmake cheat sheet that I have composed so it will be easy to write CMakeList.txt files.

Uploaded by

Tizita Nesibu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
2K views1 page

Cmake Cheat Sheet

Cmake Cheat Sheet. This is a cmake cheat sheet that I have composed so it will be easy to write CMakeList.txt files.

Uploaded by

Tizita Nesibu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Cmake Cheat Sheet

Basics
Init/Config
cmake_minimum_version (VERSION 2.8)
project (project_name)
#if test build required
enable_testing()
#version number
set (project_name_VERSION_MAJOR 1)
set (project_name_VERSION_MINOR 0)
configure_file (
${PROJECT_SOURCE_DIR}/ProjectNameConfig.h.in"
${PROJECT_BINARY_DIR}/ProjectNameConfig.h"
)
include_directories ("${PROJECT_BINARY_DIR}")
include_directories ("${PROJECT_SOURCE_DIR}")

${EXTRA_BINCFLAGS})
#making binary
add_executable (BinaryName ${binary_name_SRCS})
target_link_libraries (BinaryName "..libs.." ${LIBS})
#installation
install (TARGETS BinaryName DESTINATION
${CMAKE_INSTALL_PREFIX})
#error while loading shared libraries: cannot open
#shared object file: No such file or directory
set (CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) #do this

find_package (LibXML++ REQUIRED)


include_directories (${LibXML++_INCLUDE_DIRS})
set (LIBS ${LIBS} ${LibXML++_LIBRARIES})
... [check LibXML++_FOUND variable]
target_link_libraries(exampleProgram ${LIBS})
Miscellaneous

#tests (optional)
add_test (BinaryNameTest BinaryName input_args)

#set cmake variable


set (VARIABLE VALUE)

add_test (BinaryNameTest2 BinaryName input_args)


set_tests_properties (BinaryNameTest2
PROPERTIES PASS_REGULAR_EXPRESSION "expected output")
Library (s)
set (lib_name_SRCS list_of_c_or_cpp_srcs)

#system function check


#e.g. system provides log and exp?
include (CheckFunctionExists.cmake)
check_function_exists (log HAVE_LOG)
check_function_exists (exp HAVE_EXP)

#Using external libraries that CMake doesnt yet have


#modules for, e.g., LibXML++
#creat a folder cmake/Modules/ and put FindXXX.cmake
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH}
"${CMAKE_SOURCE_DIR}/cmake/Modules/")

# set the path to the library folder


link_directories (/path/to/lib)
include_directories (/path/to/include)

#message
message ("Please do this...${var_name}")
#error message
message (FATAL_ERROR "An error occured!")
#conditional statement
if (condition)
do sth
else (condition) #condition must match
do sth else
endif (condition)

set (LIBS ${LIBS} "external libs")


ProjectNameConfig.h.in
//the configured options and settings for Tutorial
#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@
#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@
#cmakedefine HAVE_LOG
#cmakedefine HAVE_EXP
Subdirectorie(s)
add_subdirectory(sub_dir/)

set (EXTRA_LIBCFLAGS "-Wall ...")


# for c
set (CMAKE_C_FLAGS ${CMAKE_C_FLAGS} ${EXTRA_LIBCFLAGS})
# for c++
set (CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS}
${EXTRA_LIBCFLAGS})
#making lib
add_library (LibraryName STATIC/SHARED
${binary_name_SRCS})
#this perhaps has a project scope (?)
target_link_libraries (LibraryName "..libs.." ${LIBS})

Executable(s)

#logical operators
if (A AND/OR B)
if (NOT A)
#creating an alias for a lib
add_library(<name> ALIAS <target>)
#Add a dependency between top-level targets.
add_dependencies (<target> [<target-dependency>]...)
#accessing properties e.g.
get_property(inc_dirs DIRECTORY PROPERTY
INCLUDE_DIRECTORIES)
message ("inc_dirs = ${inc_dirs}")

Finding Package (s)


set (binary_name_SRCS list_of_c_or_cpp_srcs)
find_package (Eigen3)
link_directories (/path/to/lib)
include_directories (/path/to/include)
set (LIBS ${LIBS} "external libs")
set (EXTRA_BINCFLAGS "-Wall ...")
#for c
set (CMAKE_C_FLAGS ${CMAKE_C_FLAGS} ${EXTRA_BINCFLAGS})
#for c++
set (CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS}

EIGEN3_FOUND #will be defined (if found)


EIGEN3_INCLUDE_DIRS and EIGEN3_LIBRARIES
#usage
include_directories (${EIGEN3_INCLUDE_DIRS})
target_link_libraries (bin_app ${EIGEN3_LIBRARIES})

Details
Non standard find modules

Packages with components

find_package (Qt COMPONENTS QtOpenGL QtXml REQUIRED)


if ( NOT Qt_FOUND OR NOT QtXml_FOUND )
message (FATAL_ERROR "Package Qt and component
QtXml required, but not found!")
endif ( NOT Qt_FOUND OR NOT QtXml_FOUND )

You might also like