CMake project, source and binary directory variables

Introduction

CMake variables like CMAKE_SOURCE_DIR are predefined variables in CMake that provide information about the build and source directory structure.

Directory Variables

  1. CMAKE_SOURCE_DIR:

    • Represents the top-level directory containing the root CMakeLists.txt file.
  2. CMAKE_BINARY_DIR:

    • Refers to the top-level build directory where CMake generates the build files.
  3. PROJECT_SOURCE_DIR:

    • Refers to the source directory of the current project (set by project() in CMakeLists.txt).
    • If there are multiple nested projects, this variable changes for each.
  4. PROJECT_BINARY_DIR:

    • Refers to the binary directory of the current project.
    • Similar to CMAKE_BINARY_DIR, but specific to the scope of the project.
  5. CMAKE_CURRENT_SOURCE_DIR:

    • Represents the directory containing the CMakeLists.txt file currently being processed.
    • Different from CMAKE_SOURCE_DIR, as it reflects the current directory in a hierarchical project.
  6. CMAKE_CURRENT_BINARY_DIR:

    • Points to the build directory corresponding to the CMAKE_CURRENT_SOURCE_DIR.

Summary

  • CMAKE_* variables are global and refer to the top-level project.
  • PROJECT_* and CMAKE_CURRENT_* variables adjust for subprojects and nested scopes.
  • These variables help maintain a clean separation between source and build directories, which is a core principle of CMake.
Tags ➡ C++

Subscribe

I notify you of my new posts

Latest Posts

Comments

0 comment