Try Documentalist, my app that offers fast, offline access to 190+ programmer API docs.

CMakeLists.txt is what describes cmake file
cmake build . : execute CMakeLists.txt from current (.) directory
cmake -G "generator" . : generates files for a different build system. Default generator is make. Possible generators: Unix Makefiles , Ninja , XCode ,
cmake --trace . : run trace, which shows all variables set (for debugging)
# this is how to de-obfuscate build commands i.e. see what commands are being executed
# https://stackoverflow.com/questions/2670121/using-cmake-with-gnu-make-how-can-i-see-the-exact-commands
cmake -DCMAKE_RULE_MESSAGES:BOOL=OFF -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON .
make --no-print-directory
When working on CMakeLists.txt you often need to clean cmake cache. I add this alias to my .bash_profile : alias cmake_clean="rm -f CMakeCache.txt; rm -rf CMakeFiles/; rm -f cmake_install.cmake" (or just the commends)
cmake_minimum_required (VERSION 3.8)
project (djvu)
set(CMAKE_BUILD_TYPE Release)
include_directories("libdjvu")
add_executable(djvudump tools/djvudump.cpp)
Cmake defines lots of internal variables that affect what happens. You can define your own variables (e.g. to define a list of files to compile).
set(CMAKE_BUILD_TYPE Release) : set internal variable CMAKE_BUILD_TYPE to Release.
file(GLOB LIB_SOURCES "libdjvu/*.cpp") : set LIB_SOURCES variable to list of files in directory libdjvu that match glob patter *.cpp
${LIB_SOURCES} refers to a variable.
Wehn debugging you can print the value of a variable with MESSAGE(STATUS "CMAKE_CURRENT_BINARY_DIR:" ${CMAKE_CURRENT_BINARY_DIR})
include_directories("libdjvu") : adds libdjvu/ directory to list of directories compiler will search for include files (.h etc.).
add_library(libdjvu SHARED ${LIBDJVU_SOURCES}) : builds a shared (dll) library from source files defined by LIBDJVU_SOURCES variable. STATIC will build a static library.
add_executable(djvudump tools/djvudump.cpp) : this defines djvudump binary built from tools/djvudump.cpp . Cmake figures out from .cpp extension that this is C++ binary and will use C++ rules to compile and link the executable.
target_link_libraries(djvudump libdjvu) : tells to link executable djvudump with libdjuv library (defined with add_library() command)
add_definitions(-DWITH_OPENCV2)
add_definitions(-DOPENCV_VERSION=${OpenCV_VERSION})
target_compile_definitions(my_target PRIVATE FOO=1 BAR=1) : only for a specific target (e.g. a specific library of executable)
set_source_files_properties(foo.cpp PROPERTIES COMPILE_DEFINITIONS -DFOO=1) : only for a specific file
By default cmake creates libraries/executables in top-level directory. You can change it by setting those variables:
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
Only applies to single-configuration generators (e.g. make, ninja as opposed to e.g. msvc).
Effect: e.g. if is Debug, CMAKE_C_FLAGS_DEBUG gets added to CMAKE_C_FLAGS
Links:
# comment
cmake_minimum_required(VERSION 2.8)

message(STATUS "this is a message, for debugging")

# in Visual Studio this is .sln solution. In Xcode, this is .xcodeproj
# in make and ninja, this is a target
project(all)

## building a static library

# set LIB_SRC variable to list of files matching a list of file patterns
file (GLOB LIB_SRC
    “lib/*.c”
    “lib/*.h”
    “lib/readme.txt”
)

# “mystaticlib” is the name of the library, STATIC it’s static and ${LIB_SRC}
# is a list of source files to compile to build this library
add_library(mystaticlib STATIC ${LIB_SRC})

## build an executable that links with static library

file (GLOB EXE_SRC
    “src/*.cpp”
    “src/*.h”
)

# so that #include can find mystaticlib header files
include_directories(“lib”)

add_executable(my exe ${EXE_SRC})
# the executable links my staticlib
targer_link_libraries(my_exe mystaticlib)
# adds -DFOO -DBAR=3 only to target my_exe
set_property(
    TARGET my_exe
    PROPERTY COMPILE_DEFINITIONS
    FOO
    BAR=3
)

IF(WIN32)
    target_link_libraries(my_exe "gdiplus" "comctl32" "shlwapi" "Version")
ENDIF()
Cmake build types:
CMAKE_BUILD_TYPE:  DEBUG RELEASE RELWITHDEBINFO MINSIZEREL

Some variables are per-build type, e.g.:
CMAKE_C_FLAGS_[DEBUG | RELEASE …]

CMAKE_C_FLAGS_DEBUG gets added to CMAKE_C_FLAGS when generating DEBUG build

Cmake variables

Check if variable is true:
IF(WIN32)
    ….
ENDIF()

IF((MSVC_VERSION GREATER 1700) OR (MSVC_VERSION EQUAL 1700))
    …
ENDIF()
Using variable: “${MSVC_VERSION}
Important variables:
WIN32 - true for Windows (both 32 and 64 bit)
UNIX - true for unix including mac and cygwin
MSVC - true if using Visual Studio
MSVC_VERSION :
  1400 - VS 8 i.e. Visual Studio 2005
  1500 - VS 9 i.e. Visual Studio 2008
  1600 - VS 10 i.e. Visual Studio 2010
  1700 - VS 11 i.e. Visual Studio 2012
  1800 - VS 12 i.e. Visual Studio 2013
CMAKE_CL_64 - 64bit of Visual Studio

CMAKE_C_FLAGS
CMAKE_CXX_FLAGS

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:

Share on        

Need fast, offline access to 190+ programmer API docs? Try my app Documentalist for Windows