Basic steps to build your project with CMake

Disclaimer: Sorry, this not a tutorial on CMake or on how to build your first CMakeLists.txt file, but basically just a short reminder for me in case I forget the syntax/steps for building my projects 😉 .


The basic directory structure for my projects usually looks something like this:

C:\Project X
|   ChangeLog.txt
|   ...
+-- build
\-- src
    |   CMakeLists.txt
    +-- Application
    |       Application.cpp
    |       Application.h
    |       CMakeLists.txt
    |       ...
    \-- Etc.

At the top level are some common files (ReadMe.txt, etc.).

The directory build 1 will contain the resulting files of an out-of-source build and is created by hand before CMake runs the first time.

The actual source code is below src.


Open a command prompt, go to your project folder, and create the build folder:

C:\ProjectX> mkdir build

Then step into it:

C:\ProjectX> cd build

Run CMake from there to generate the Visual Studio solution and project files.
(In this example: Using the generator for Microsoft Visual Studio 2017 to create a 64-bit solution.)

C:\ProjectX\build> cmake ..\src -G "Visual Studio 15 2017 Win64"

Now you could double-click the generated *.sln file to start the Visual Studio IDE and initiate the build from there, or you could run CMake once again and use it this time to build one of the projects (*.vcxproj) from the command line.
(In this case: Building all, with the configuration set to a Release version.)

C:\ProjectX\build> cmake --build . --target ALL_BUILD --config Release

  1. Don’t bother to put the build folder under your version control system: All it contains is automatically generated (and will be re-created again) by the build process. ↩︎