cmake

gotcha's

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:

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:

  1. mkdir build: this creates a directory called “build” where cmake will put all the generated files.
  2. cd build: go into the build directory.
  3. cmake ..: run cmake, telling it to look in the parent directory (that’s where your CMakeLists.txt file is).
  4. 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.


edit this page