DEV Community

Minwook Je
Minwook Je

Posted on

CMake study

Step 0

https://cmake.org/cmake/help/latest/guide/tutorial/Before%20You%20Begin.html

  • -D: Define, preprocessing time
  • -l: link time
$ cmake -G # select generator

$ cmake -S # source root dir, default current working dir

$ cmake -B # build dir, output generated build system

$ cmake --build # run build system
Enter fullscreen mode Exit fullscreen mode
# make
cmake -B build
cmake --build build && ./build/hello
Enter fullscreen mode Exit fullscreen mode

Step 1

https://cmake.org/cmake/help/latest/guide/tutorial/Getting%20Started%20with%20CMake.html

CMakeLists.txt = CML = lists file

root CML

cmake_minimum_required(VERSION 3.23)

project(MyProjectName)
Enter fullscreen mode Exit fullscreen mode
  • add_executable() or add_library(): output type
  • target_sources(): link input -> output
  • target_link_libraries(): link outputs

add_executable()

This command creates a target.

Targets themselves are simply names, a handle to these collection of properties.

  • The artifact kind (executable, lib, header collection, etc)
  • Source files
  • Include dirs
  • Output File name
  • Dependencies
  • Flags (Compiler, Linker)
add_executable(MyProgram)
Enter fullscreen mode Exit fullscreen mode

target_sources()

Now that we have a name for our target, we can start associating properties with it like source files we want to build and link.

target_sources(MyProgram
  PRIVATE
    main.cxx
)
Enter fullscreen mode Exit fullscreen mode

Above PRIVATE(scope keyword) means main.cxx property only belongs to MyProgram and is not inheritable.

add_library()

We need to know about header files in order to build other parts of a given target.

add_library(MyLibrary)

target_sources(MyLibrary
  PRIVATE
    library_implementation.cxx

  PUBLIC
    FILE_SET myHeaders
    TYPE HEADERS # HEADERS or MODULES
    BASE_DIRS
      include
    FILES
      include/library_header.h
)
Enter fullscreen mode Exit fullscreen mode

PUBLIC: allows consumers of our library to "see" the library's header files.

Notably, if the FILE_SET name is the same as the type, we don't need to provide the TYPE field.

target_sources(MyLibrary
  PRIVATE
    library_implementation.cxx

  PUBLIC
    FILE_SET HEADERS
    BASE_DIRS
      include
    FILES
      include/library_header.h
)
Enter fullscreen mode Exit fullscreen mode

target_link_libraries()

It describes relationships between targets generally.

target_link_libraries(MyProgram
  PRIVATE
    MyLibrary
)
Enter fullscreen mode Exit fullscreen mode

scope

  • PRIVATE
  • INTERFACE
  • PUBLIC
  • The order here is only loosely relevant. That we call target_link_libraries() prior to defining MathFunctions with add_library() doesn't matter to CMake.

add_subdirectory()

allows us to incorporate CMLs located in subdirectories

When a CMakeLists.txt in a subdirectory is being processed by CMake all relative paths described in the subdirectory CML are relative to that subdirectory, not the top-level CML.

add_subdirectory(SubdirectoryName)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)