Try Documentalist,
my app that offers fast, offline access to 190+ programmer API docs.
Running cmake
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 basics
basic structure of CMakeLists.txt file
cmake_minimum_required (VERSION 3.8)
project (djvu)
set(CMAKE_BUILD_TYPE Release)
include_directories("libdjvu")
add_executable(djvudump tools/djvudump.cpp)
using variables
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})
C++ include directories
include_directories("libdjvu")
: adds libdjvu/
directory to list of directories compiler will search for include files (.h etc.).
building a library
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.
building an executable
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)
setting C pre-processor defines
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
changing output directory
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)
Important cmake variables
CMAKE_BUILD_TYPE : empty, Debug, Release, RelWithDebInfo and MinSizeRel
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
- https://izzys.casa/2019/02/everything-you-never-wanted-to-know-about-cmake/
- http://www.brianlheim.com/2018/04/09/cmake-cheat-sheet.html
- https://cgold.readthedocs.io/en/latest/
- https://cliutils.gitlab.io/modern-cmake/
- https://codingnest.com/basic-cmake/#fnref6
- https://pabloariasal.github.io/2018/02/19/its-time-to-do-cmake-right/
- https://www.youtube.com/watch?v=rLopVhns4Zs&feature=youtu.be : effective CMake
- https://github.com/crezefire/cxp : modern CMake template for C++ cross-platform apps
- http://thetoeb.de/2016/08/30/modern-cmake-presentation/
- http://derekmolloy.ie/hello-world-introductions-to-cmake/
- https://www.slideshare.net/DanielPfeifer1/cmake-48475415
- https://rix0r.nl/blog/2015/08/13/cmake-guide/
- http://preshing.com/20170511/how-to-build-a-cmake-based-project/
- http://preshing.com/20170522/learn-cmakes-scripting-language-in-15-minutes/
- https://asmbits.blogspot.com/2017/06/idiomatic-cmake.html
- http://www.goodbits.ca/index.php/2017/09/25/building-an-apple-osx-kernel-module-with-cmake-cc/
- https://github.com/buggins/coolreader/blob/master/CMakeLists.txt
- https://github.com/koreader/koreader-base/blob/e1974cbc8841dbdff2f837da1d2cde6ec6b107e3/thirdparty/djvulibre/CMakeLists.txt
- https://stackoverflow.com/questions/46553436/building-with-cmake-ninja-and-clang-on-windows
- https://www.reddit.com/r/cpp/comments/67av0m/is_there_a_better_way_to_marry_cmake_clang_and_vs/
- https://stackoverflow.com/questions/38171878/how-do-i-tell-cmake-to-use-clang-on-windows
- https://www.reddit.com/r/cpp/comments/67av0m/is_there_a_better_way_to_marry_cmake_clang_and_vs/
- https://github.com/webmproject/libwebm/blob/master/CMakeLists.txt
- http://bastian.rieck.me/blog/posts/2018/cmake_tips/
older
Links:
- http://stackoverflow.com/questions/2368811/how-to-set-warning-level-in-cmake
- https://github.com/libgit2/libgit2/blob/development/CMakeLists.txt : useful stuff for windows
- https://rix0r.nl/blog/2015/08/13/cmake-guide/
- http://www.slideshare.net/DanielPfeifer1/cmake-48475415
# 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