A simple guide to building a C program with CMake
Ever thought building a C program was hard? Not anymore. Here’s a simple and comprehensive guide to build C programs using CMake.
A simple guide to building a C program with CMake
Ever thought building a C program was hard? Not anymore. Here’s a simple and comprehensive guide to build C programs using CMake.
A standard C project folder is typically setup in this way:
- main.c (or whatever your program name is)
- CMakeLists.txt
- build/
Now, you probably have completed writing your main.c, if you are looking at this right now. So, here’s how to setup the CMakeLists.txt file.
First of all, these 4 lines should be included:
- Set the minimum version of CMake
- Define the project name and language
- Set the standard (this is not compulsory)*
- Specify the executable name and the source files needed to build it
Now, let’s write the text file.
The text file should be saved within the same folder that main.c is. (the project folder)
cmake_minimum_required(VERSION 4.10)
project(MyCProject C)
set(CMAKE_C_STANDARD 23)
add_executable(my_program main.c)
You can check the version of CMake available on your PC, by using:
cmake --version
If CMake is not installed on your PC, you can visit https://cmake.org/download/ and follow the instructions to install it.
Next, create a folder within the project folder called build .
Building on Windows
If you have the Visual Studio Developer Tools for C/C++, the rest is easy.
- Open up ‘Developer Command Prompt for VS ….’ and navigate to your
buildfolder usingcd. - Generate the build files using
cmake ..and compile the program usingcmake --build, and now you can find the executable inside the build folder.
If you are using MinGW (GCC) on Windows, then follow these steps.
- Open your standard terminal inside the
buildfolder, and generate the build files using,cmake -G "MinGW Makefiles" .. - Now, compile the program using
cmake --buildas before, and you would be able to find the executable inside the build folder.
Building on Linux
As Linux usually only has the GCC compiler pre-installed on most systems, you do not need to worry about differentiating between what compiler to use for this.
- Open your standard terminal inside the
buildfolder, and generate the build files using,cmake .. - Now, compile the program using
cmake --buildas before, and you would be able to find the executable inside the build folder.
Additional steps (read this if you get errors)
Some libraries that are not part of the standard C library are automatically linked on Windows when using MSVC or MinGW. But on Linux, non-standard C libraries are strictly separated and should be linked explicitly. So, even if you are building on Windows, it is recommended to mention to link the non-standard C libraries used in your code anyway, in the CMakeLists.txt file. (The libary names are not always the same, but most common libaries share the same library names. For system level tasks, the library names will most probably differ.)
As an example, here’s how to link the math library.
target_link_libraries(MyCProject m)
MyCProject is the project name, and m is the library name for the math library.
How do I know the what library to link?
If you’re on Linux, you can simply type in man function_name directly into the terminal, and find the related information.
As an example, I want to use the pow function, which is in the math library. You can enter man pow into the terminal, hit enter, and you’ll get something like this (it goes on longer, but these lines are what we want):
NAME
pow, powf, powl - power functions
LIBRARY
Math library (libm, -lm)
SYNOPSIS
#include <math.h>
In SYNOPSIS the way to include the header file to the library is given, which you should’ve probably already done.
In LIBRARY , we have -lm. The library name will always be the part without the -l , which is m, or if we consider libm, without the lib.
You can also access using https://man7.org/linux/man-pages/index.html, but it is recommended to use the terminal command.
If you’re on Windows, for system level functions take guidance from https://learn.microsoft.com/en-us/windows/win32/. Other functions will most probably be automatically handled by MSVC or MinGW. (if not, https://man7.org/linux/man-pages/index.html will help most of the time)
As an example, if you need to link the WSAStartup function, you can find the following line from the example: (https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-wsastartup)
#pragma comment(lib, "ws2_32.lib")
The libary name is ws2_32. You can link it using the CMakeLists.txt, using the following method. (you need the if clause, because they are specific for Windows)
if(WIN32)
target_link_libraries(MyCProject PRIVATE ws2_32)
endif()
On either OS, if the compiler gives you an error, sort out what libraries need to be included, and link them. Simple (complex) as that.
There is a modern way to find libraries. You can use find_package(LibraryName), and create a target like target_link_libraries(MyCProject PRIVATE LibraryName::Something), but I won’t include anymore, since this was meant to be a “simple guide”. If none of the above works, then this is what you need.
메타데이터
- post_id
- f03145eb9c0e
- slug
- a-simple-guide-to-building-a-c-program-with-cmake-f03145eb9c0e
- url
- https://medium.com/@lochanalmr/a-simple-guide-to-building-a-c-program-with-cmake-f03145eb9c0e
- canonical_url
- https://medium.com/@lochanalmr/a-simple-guide-to-building-a-c-program-with-cmake-f03145eb9c0e
- author_url
- https://medium.com/@lochanalmr
- status
- ok
- fetched_at
- 2026-08-08 06:14:34