I’ve tried to build a small Visual Studio project with CMake before I explain the basic principles of CMake itself.

Below you can see one of the simplest CMake files you can write.

cmake_minimum_required (VERSION 2.8.11)
project (MySolutionName) # MySolutionName could be anything, it's not really important

add_library (HelloWorldLibrary hello.cpp) # HelloWorldLibrary is the name of our dll-project

add_executable (HelloWorldApp main.cpp) # HelloWorldApp is the name of our exe project
target_link_libraries (HelloWorldApp PUBLIC HelloWorldLibrary) # now we have to link our executable (HelloWorldApp) with our library (HelloWorldLibrary)
CMAKE COMMAND REQUIRED DESCRIPTION
cmake_minimum_required yes Defines the minimum version of CMake which you want to use. I recommend CMake 3.x.
project no This is (in Visual Studio context) the name of the solution.
add_library no Creates a target called HelloWorldLibrary, which is a static lib/dll project with the same name in our solution.
add_executable no Creates a target called HelloWorldApp, which is an exe project in our solution (you get it, with the same name 🙂).
target_link_libraries no Links our HelloWorldApp with our HelloWorldLibrary, because we want to use some functions from the library in our exe.

So you can use this snippet, copy it, and paste it into a file named CMakeLists.txt.

  1. Create a directory.

  2. Create a file named CMakeLists.txt.

  3. Copy/Paste the code from above.

  4. Add the missing code files (main.cpp, etc.), all in the same directory.

  5. Now you have 2 options to create your Visual Studio project in Windows (I prefer the console approach):

    • Use the CMake GUI application:

      Open the app, choose the source directory and binary directory. Click on generate and choose the compiler (Visual Studio) version which is installed on your system. Be aware that you have to choose between win64 and win32. In my example, I chose the generator “Visual Studio 10 2010 Win64.” Now you will see a console output in the bottom section of the window. If something went wrong, please read the error messages in the console output section. If everything is fine, you can navigate (e.g., with the Windows Explorer) to your binary directory and open your new solution file, named after your project name, in my example MySolutionName.sln. Then you can compile it normally with Visual Studio.

    • Use the command prompt/console:

      Here we need the same parameters as in the GUI approach, but in a console fashion. You should cd into your source directory and use the following command line parameters:

      cmake -B<path to binary directory> -G "Visual Studio 10 2010 Win64"
      

      If you don’t want to cd into your source directory, you could also use the parameter:

      -H<path to your source directory>
      
Parameter Description
Source directory The directory where your CMakeLists.txt lives.
Binary directory The directory where your binaries, Visual Studio project files, and some additional files will go. These files are temporary and should not be checked into your repository!

What is a target? Link to heading

If you are starting with CMake, you will relatively often use a target, but what is a target?

If you are coming from Visual Studio, a target is a Visual Studio project (*.vcxproj), so if your target is named MyAwesomeApp, your Visual Studio project will also be named MyAwesomeApp. The target concept is based on the principle of makefile targets (as are many other things in CMake). A target is generally an entry point, something like a void main() function for your application, but you can have as many as you want.

The target is also the only type in CMake which does not act like a string, but I will explain that in a later post.