CMake logo

This past summer I decided to take a C programming class to sharpen my coding skills.

But as my assignments became more complex they required multiple header and implementation files. I knew that it was time to look into different build systems. But it felt as if there were an endless number of options to choose from and settled for cmake.

I would like to break down a simple CMakeLists.txt file for your project and for my own reference. CMake has many features that I will not be covering here so you many want to refer to the CMake documentation

Installation

I would recommend following the official CMake installation guide as this may become out of date!

Click for Linux instructions

If you are on a unix/linx system you can install CMake using your systems package manager.

For Example:

sudo apt install make cmake
Click for Winows instructions

Windows Instructions go here!!!

Project Structure

I am not sure if there is a formal name for this type of project structure, but this is what I have seen used out in the wild most often for C and C++ projects.

Project Name
├── README
├── LICENCE	
├── CMakeLists.txt    // simple CMakeLists.txt
├── build             // Where your build files go
├── include           // header files
├── lib               // external libraries 
└── src               // source code 
    └── main.c

The Simple CMakeLists.txt

cmake_minimum_required(VERSION 3.24)
set(PROJECTNAME Name)
project(${Name})

set(CMAKE_C_STANDARD 23) 
set(CMAKE_C_STANDARD_REQUIRED ON) 

# list of c files to compile
set(SOURCES
  # Source files go here
)

# gcc compile options
add_compile_options(-Wall -Wextra -save-temps)

add_executable(${PROJECT_NAME} ${SOURCES})

target_include_directories(${Name}
    PRIVATE 
        ${PROJECT_SOURCE_DIR}/include
)

Above is what I have dubbed the simple CMakeLists.txt