gotcha's
- CMake is not:
- A compiler
- A build system
- Cmake indeed:
- Generates the required files for the build system that exists on your operating system, for example on linux it may generate make files, if you're using VSCode it will generate solution files.
- Is an abstraction above the compiler that let's you write CMake instructions on how to build your project without binding yourself to a specific build system such as make
- Makes it possible for you to compile the same code on multiple operating systems, because different operating systems may use different build systems
compiling a program
Because cmake
provides a solution to a complex problem, it is highly configurable, and usually a highly configurable systems make doing simple things a little more involved, indeed this manifests itself in cmake while trying to build a simple project. Here's the breakdown
With cmake we start by generating the build system (on my machine this will create a makefile)
Next we can build the project
For further useful scripts with cmake check my page on them, if you want to learn how to integrate external libraries with cmake check my page on that here
informal rundown
cmake is a tool that helps you build your code. think of it like a project manager for your code—it doesn’t do the actual compiling, but it sets everything up so your compiler can do its thing. and the best part? it works across different platforms like windows, linux, and macos.
cmake isn’t a compiler by itself. it’s more like a translator between your project’s setup and whatever compiler you’re using (like gcc, clang, or visual studio). so instead of writing custom build files for each platform (ugh, painful), you use cmake to create them for you automatically. neat, right?
why should you bother with cmake?
because setting up a project manually for each operating system induces a ton of overhead. with cmake, you write one set of instructions, and it figures out the rest. no more messing with different file formats for windows vs linux vs mac.
for example, on linux, cmake can create makefiles (which you run with the make
command). on windows, it can generate visual studio solutions. you don’t have to worry about the details—cmake handles it.
ok, how does it work?
cmake uses a file called CMakeLists.txt
. this file tells cmake how to build your project. here’s a super simple example:
cmake_minimum_required(VERSION 3.10)
project(my_project)
# use c++17
set(CMAKE_CXX_STANDARD 17)
# define the executable
add_executable(my_app main.cpp)
here’s the rundown:
cmake_minimum_required
: this tells cmake the minimum version it needs to run.project
: this names your project (in this case, "my_project").set(CMAKE_CXX_STANDARD 17)
: you're telling cmake to use c++17.add_executable
: this says “hey, turnmain.cpp
into an executable file calledmy_app
.”
now what?
after you’ve written your CMakeLists.txt
, you tell cmake to generate the necessary build files. on linux, you’d do something like this:
mkdir build
cd build
cmake ..
make
here’s what’s happening:
- mkdir build: this creates a directory called “build” where cmake will put all the generated files.
- cd build: go into the build directory.
- cmake ..: run cmake, telling it to look in the parent directory (that’s where your
CMakeLists.txt
file is). - make: now you can run
make
to actually compile your code.
boom! your project is built.
what if my project is more complicated?
cmake can handle that too. if you’ve got a bigger project with dependencies (like using libraries for graphics or networking), cmake can find and include those libraries for you. for example, let’s say you want to use OpenGL
and Boost
:
find_package(OpenGL REQUIRED)
find_package(Boost REQUIRED COMPONENTS system filesystem)
this tells cmake to look for opengl and boost on your system and link them to your project automatically.
wrapping up
cmake is one of those tools that saves you tons of time, especially if you're dealing with different platforms. it simplifies the whole process of compiling and linking your code, so you can focus on coding rather than figuring out how to set up your project for windows, linux, or mac.