Cmake Cheat Sheet



2a) Are there any good guides or cheat sheets to help with migrating from using GNU make directly. 2b) Are they any good best practice guides for CMake? That is beyond the suggestions in Makefile equivalent in CMake. I am gradually evolving my own style, but I would like to avoid horseless carriage type mistakes and needless complexity. CMake Cheat Sheet¶ cmake; Package; External Project; GNU Debugger Cheat Sheet. I put a “cheat sheet” version of this data at the end of the article for easy lookup, but if you want details on each system, read on! First, some automated tests require a running ROS system. Maybe you want to check that a service call returns correctly, or your node depends on other nodes while it’s being tested. WIN32 - true for Windows (both 32 and 64 bit) UNIX - true for unix including mac and cygwin MSVC - true if using Visual Studio MSVCVERSION: 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 CMAKECL64 - 64bit of Visual Studio CMAKECFLAGS CMAKECXXFLAGS. This is a non-exhaustive list of some common and useful invocations of the catkin command. All of the commands which do not explicitly specify a workspace path (with -workspace) are assumed to be run from within a directory contained by the target workspace.For thorough documentation, please see the chapters on each verb.

Contents

Creating distribution packages with CMake

CMake can automatically create packages for several distributions (e.g. Redhat/Fedora .rpm, Debian/Ubuntu .deb, etc), though the created packages will lack dependency information. To create these packages, add at the end of CMakeLists.txt:

Modify the variables to create an package with the appropriate name and metadata fields. After building the package, run:

Source: Harold Sitter's Checkinstall DEBs done the CMake way...

Good examples

https://github.com/videolan/x265

https://github.com/Qartar/qflags/blob/master/CMakeLists.txt

CMake, CPack, and Windows

Release

Example of using WIX and CPack: https://cmaketools.codeplex.com/SourceControl/latest#CMakeLists.txt

Uses: https://cmake.org/cmake/help/latest/command/include_external_msproject.html

Installing the MS Visual Studio runtime dependencies w/ WiX:http://stackoverflow.com/questions/34592378/redistributables-for-deploying-c-exe-developed-with-visual-studio-2015-on-wind

Tutorials

C++Now 2017: Daniel Pfeifer “Effective CMake'

Modern CMake: presentation.

External links

Create dlls on Windows without declspec() using new CMake export all feature: have CMake already export symbols for DLLs. https://gcc.gnu.org/wiki/Visibility provides an alternative.

Difference between PRIVATE, PUBLIC, and INTERFACE

CMake: dependencies between targets and files and custom commands

CMakeModularizationStatus – Boost C++ Libraries: Building Boost w/ CMake

nerdvegas/vfxcmake: Cmake Find modules for common vfx software, and general Cmake utility code: Modules for finding Nuke's SDK, Arnold, Houdini, OpenEXR, etc.

CategoryCheatSheet

at the request of a friend i’ve decided to dump some of my basic cmake knowledge. this isinformation i’ve picked up from supercollider, personal projects, and tutorial videos, but itdoesn’t seem to be immediately accessible unless you know what you’re looking for.

for each bit i’ll be using the most recent notation available, as far as i know it. i’m alsointentionally keeping this post short so it can serve as a cheat sheet - that’s why, for instance,i’m not listing every possible compiler id or command variation.

contents:

config and build steps

Cmake cheat sheet 2019

most usage of cmake will consist of two types of commands: configuration and building. i don’t thinkthese are exactly the official names, but what i mean is:

compiler ids

to figure out what compiler CMake is using, check CMAKE_CXX_COMPILER_ID or use a<CXX_COMPILER_ID:Foo> generator expression (see below)

compilerid
gccGNU
clang (LLVM)Clang
clang (Apple) - this is what comes with XCodeAppleClang
msvcMSVC
iccIntel

Cmake Cheat Sheet Free

build types

with CMake you can have several build types. the most important thing to know is that if you use anIDE project generator (like Visual Studio or XCode) you specify this in the build step, but forMakefiles you specify it in the config step.

each build type passes different flags to your compiler. for some ungodly reason it’s hard to findout what those are, so i’m listing them here. the only other place i can find this is on a stackoverflow answer. these are the flags for AppleClang,others should be similar.

you can find out what they are for your compiler by creating a minimum CMake project, running cmake., and reading CMakeCache.txt.

build typewhat it meansflags (AppleClang, GCC)flags (MSVC 2017)
None (empty)you didn’t pass a build type to a makefile generator/DWIN32 /D_WINDOWS /W3 /GR /EHsc
Debugdebug build, no optimization-g/MDd /Zi /Ob0 /Od /RTC1
Releaserelease build, full optimization-O3 -DNDEBUG/MD /O2 /Ob2 /DNDEBUG
RelWithDebInfo“release with debug info”. keeps debugging symbols and does optimization-O2 -g -DNDEBUG/MD /Zi /O2 /Ob1 /DNDEBUG
MinSizeRel“minimum size release”. optimized for small binary size-Os -DNDEBUG/MD /O1 /Ob1 /DNDEBUG

the ‘none’ flags will be passed for all build types.

don’t develop on Windows and don’t know what those flags mean? i looked them up for you:

  • /DFOO - define FOO in the preprocessor
  • /EHsc - catch C++ exceptions, assume extern 'C' functions never throw C++ exceptions
  • /GR - enable RTTI
  • /MD - make a multithreaded DLL
  • /MDd - make a debug multithreaded DLL
  • /O1 - optimize for size
  • /O2 - optimize for speed
  • /Ob0 - no auto-inlining
  • /Ob1 - only inline functions that are marked inline, and C++ member functions defined in a classdeclaration
  • /Ob2 - let compiler inline freely
  • /Od - no optimization
  • /RTC1 - run-time checking: report when a variable is used without being initialized, and stackframe run-time error checking. See theirsite for more details.
  • /W3 - use warning level 3 (out of 4), “production quality”
  • /Zi - generate “complete debugging information”, like -g for clang/gcc

passing flags to the compiler or linker

sometimes you want to force a flag to be passed to the compiler or linker. while some compilersalso let you specify these using environment variables, other times you just want to test outwhether adding a flag does what you want it to do. the following configuration-step flags are goodfor that:

  • CMAKE_C_FLAGS/CMAKE_CXX_FLAGS - used by the compiler
  • CMAKE_EXE_LINKER_FLAGS - used by the linker when linking executables
  • CMAKE_SHARED_LINKER_FLAGS - used by linker when linking shared object libraries
  • CMAKE_MODULE_LINKER_FLAGS - used by linker when linking modules (i don’t know what this means)
  • CMAKE_STATIC_LINKER_FLAGS - used by linker when linking static object libraries

you can also edit the cache directly to set these if you don’t want to go through an entireconfiguration step again.

you can also use the environment variables CFLAGS, CXXFLAGS, and LDFLAGS, but those only workon the first configuration and are therefore useless in my opinion.

using boost

find_package will take care of everything else for you: include directories, finding thelibraries, etc.

you can set some variables pre-command to configure what libs are linked. some of interest may be:

  • Boost_USE_MULTITHREADED - ON by default, set to OFF to use non-multithreaded libs
  • Boost_STATIC_LIBS - OFF by default, set to ON to force-use static libraries
  • Boost_DEBUG - get debug output from the find_package command

adding tests

enable_testing| add_test| CTest manual

after building, you can now you can execute ctest in the build directory to run the tests.

for a more sophisticated project you will want to hide this behind a flag so that end users don’thave to build your tests if they don’t want to.

setting compiler options

target_compile_definitions| target_compile_options

use target_compile_definitions for preprocessor definitions, target_compile_options for actualcompiler options.

these are close to real commands i’ve used:

Cmake Cheat Sheet

using generator expressions

Cmake Cheat Sheet

i think the docs here are quite good actually, and the list is too long to go over here.

generator expressions are fantastic; they let you succinctly write informational messages andconditional expressions. take the example i gave in the previous section:

the inner expression evaluates to either 1 or 0, and a generic expression like $<X:STR> will onlyevaulate to STR if X is 1. overall, this evaluates to the flag -Wno-psabi iff the compiler isgcc; otherwise it evaluates to nothing.

this is a lot simpler and clearer than writing:

note: unlike most of CMake, you can’t just coerce things on the left hand side of one of thesethings to true or false; it has to be exactly 0 or 1. there’s a special expression, $<BOOL:foo>,for just that purpose.

Cmake Cheat Sheet 2020

setting the c++ standard

note: added in cmake 3.1.

Cmake Cheat Sheet Template

Cmake cheat sheet

putting it all together

Cmake Release With Debug Info

a sample cmake file for a project using the boost unit testing lib: