CMAKE Configuration For A Small-Scale Project

Echo Yin
Women in Technology
3 min readAug 14, 2023

[Personal Devlog: August 14, 2023]

CMake serves as a meta-configuration build system designed for C/C++. It operates within the realm of meta-configuration, a method that finalizes project configuration by amalgamating pertinent details about the project and user preferences from diverse sources.

Project Context

This CMAKE example is configured under the context of Clion and Windows platform, which has project configuration for build toolchain, such as Ninja, as well as independent build output directory setting.

A simple CMAKE structure for vulkan API can be like this:

MyProject/
├── CMakeLists.txt //root
├── application/
│ ├── CMakeLists.txt
│ ├── EntryPoint.cpp
├── src/
│ ├──CMakeLists.txt //source
│ └── library_source.cpp
│ └── include/
│ ├── library_header.h
│ └── other_header.h
├── plugins/
│ ├──CMakeLists.txt //plugins
│ ├──vulkan
│ ├──other_plugins
├──test/
│ ├──CMakeLists.txt // tests
└──── test.cpp

Project build folder can be entirely separate from source code, leaving the source folder clean and easier to manage.

MyProjectBuild/
└── build-debug/

The root CMakeLists.txt must contain these basic commands

cmake_minimum_required(VERSION #.##.#)
project(${ProjectName}
VERSION #.#
DESCRIPTION ""
LANGUAGES CXX)
Set(CMAKE_CXX_STANDARD ##)

How to include the code in subdirectories such as src, application and plugins?

First of all, all the subdirectories can have a CMakeLists.txt to manage the content inside the corresponding folder.

How does the root CMakeLists.txt know the existence of subdirectory CMakeLists.txt?

By using add_subdirectory() command.

This will let the root CMakeLists.txt know the existence of the included subdirectory.

And all the other subdirectories included in the root CMakeLists.txt are also aware of each other.

For example:

add_directory(src)
add_directory(plugins)

How to let CMake know where to find entry point/main.cpp for building an executable?

In this example, the main.cpp is under application/ folder.

In the CMakeLists.txt under this folder, use add_executable() command.

For example:

add_executable(Entry main.cpp)

How to include plugins from third-party plugins? How to let code under src/ to be able to include plugins’ headers?

Third-party plugins such as vulkan and glfw have CMake support.

In the root CMakeLists.txt file, look for target_include_directories() command, and the library name is the first parameter inside this commands’ brackets.

For example:


add_library(plugin_library
${SOURCE_LIST}
${HEADER_LIST})

The library name is plugin_library.

In the CMakeLists.txt under src/ folder, simply add the library name to the command target_link_libraries()

For example:

target_link_libraries(src_library PUBLIC plugin_library)

How to build a dynamic library from CMakeLists.txt in the src/ folder?

First include all the file folder inside a list.

For example:

file(GLOB HEADER_LIST ${INCLUDE_DIR}/*.h ${INCLUDE_DIR}/*.hpp)

This command will include all the header files under a command string called HEADER_LIST.

And also use add_library() command to include all the files needed to build a dynamic library.

--

--