Skip to content

Instantly share code, notes, and snippets.

@lixit
Created September 20, 2019 01:52
Show Gist options
  • Save lixit/fdb6a0176d8dc3462df6646a48c910a9 to your computer and use it in GitHub Desktop.
Save lixit/fdb6a0176d8dc3462df6646a48c910a9 to your computer and use it in GitHub Desktop.

Usage:

cmake [options] <path-to-source>
cmake [options] <path-to-existing-build>
cmake [options] -S <path-to-source> -B <path-to-build>

If specify a source directory, it (re)generates a build system in the current working directory
If specify an existing build directory, it regenerates the build system
Or specify source dir and build dir using -S and -B

CMake generator -> buildsystem -> buildtool

Source tree
contains source files. starts with a top-level file named CMakeLists.txt.

Build tree
contain buildsystem files and build output artifacts.
CMake will write a CMakeCache.txt file to identify the directory as a build tree
To maintain a pristine source tree, perform an out-of-source build by using a separate dedicated build tree.

Generator
This chooses the kind of buildsystem to generate. (For example: Unix Makefiles)

Options

CMake-buildsystem

A CMake-based buildsystem is organized as a set of high-level targets. Each target corresponds to an executable or library, or is a custom target.

  • Executables using the add_executable()
  • Libraries using the add_library()

Dependance between the binary targets are expressed using target_link_libraries()

add_library(archive archive.cpp zip.cpp lzma.cpp)
add_executable(zipapp zipapp.cpp)
target_link_libraries(zipapp archive)

Binary Library Types

By default, the add_library() command defines a STATIC library.

add_library(archive SHARED archive.cpp zip.cpp lzma.cpp)
add_library(archive STATIC archive.cpp zip.cpp lzma.cpp)

# MODULE is different because it is not linked
add_library(archive MODULE 7z.cpp)

# OBJECT library type defines a non-archival collection of object files resulting from compiling the given source code.
# They may be used as source inputs to other targets
add_library(archive OBJECT archive.cpp zip.cpp lzma.cpp)
add_library(archiveExtras STATIC $<TARGET_OBJECTS:archive> extras.cpp)
add_executable(test_exe $<TARGET_OBJECTS:archive> test.cpp)

Target Properties

  • INCLUDE_DIRECTORIES
  • COMPILE_DEFINITIONS
  • COMPILE_OPTIONSsss

CMake language

CMake Language source files in a project are organized into:

  • Directories (CMakeLists.txt),
  • Scripts (<script>.cmake), and
  • Modules (.cmake).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment