A simple CMake project

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

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

cmake command required description
cmake_minimum_required yes This command define the minimum version of CMake which you want to use. If've recommend CMake 3.x
 project no this is (in visual studio context) the name of the solution
 add_library no This command create a target call HelloWorldLibrary, which is a static lib/dll project with the same name in our solution
 add_executable no This command create a target called HelloWorldApp which is a exe project in our solution (you get it, with the same name 🙂
 target_link_libraries no Here we link our HelloWorldApp with our HelloWorldLibrary, because we want to use some function from the library in our exe

So you can use this snippet, copy it and paste it into a file which you should name 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)
    1. Use the CMake Gui application
      Open the app, choose the source-directory and binary directory. Click on generate an choose the compiler (Visual Studio) version which is installed on your system. Be aware that that you have to choose between win64 and win32. In my example i choose the so call generator Visual Studio 10 2010 Win64. Now you will see an 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 (f.e. 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
    2. Use the command prompt/console
      Here we need the same paramters 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 dont want to cd into your source-directory you could also use the paramter: -H<path to your source directory>
paramter 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 in to your repository! 

What is a target?

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

If you are coming from Visual Studio, a target is a Visual Studio project (*.vcxproject), 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 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 not act like a string, but i will explain the in a later post.

Things I want to know before I started with CMake

In this blog series i will show some tipps and tricks about cmake, which i want to know before i started with CMake.

My CMake experience

I’ve start using CMake approximately 2 years ago. I’m only developing software for Microsoft Windows with Visual Studio (most of the time VS 2010 x64) and using the build-in project management from Visual Studio. But with the growing size of my project Visual Studio’s mouse-click/declarative approach was very painful and inflexible (supporting multiple Visual Studio/VC++ version at the same time is nearly impossible or rather unmaintainable and that’s only one big point), and so i decide to checkout some build-systems. I quickly realized that the most used one is CMake. But at that time i did not really understand why most C and C++ developer are using it and if it is a good decision for a Windows only project to switch to CMake.
Long story short, switch to CMake, it doesn’t matter how big or small your project is or if it is a Windows-only project (or another single-OS project).
Don’t understand me wrong, CMake is not perfect, it has it’s cons, but the benefits are to huge to ignore.

What is CMake?

CMake is a buildsystem/script language which helps you to build your project. CMake is mainly used for C an C++ project at any size and is the de facto standard build system (starting with 3.8 it will also support C#). It is a platform independent language an run on many OS’s like Windows, MacOS, Linux, …

So what can i do with CMake?

Lots of stuff. You can use it for building your project, but you can also use it for packaging your app too (CPack) which support a wide range of package formats from zip to WiX toolset. CMake can generate makefiles, Visual Studio files, XCode, …., so your code is mainly independent of your used platform (OS specific build system) and you can switch between different compiler versions easily. You can download file, clone git repos etc..

What could you expect from the blog series?

I will start with an overview and will port a Visual Studio project to cmake. The following articles will more focus on tipps, tricks and hints and some coding examples.

If you have any wishes or suggestions, please contact me.

From Mantis to GitLab issues

In the last 5 years we used a bugtracker called mantis. Mantis is a great project and makes bugtracking really easy. Approximately one year ago we switch our projects from svn to git and we choosed GitLab (private instance) as our platform. GitLab is awesome, but that’s not the topic of that post. We quickly realize that GitLab also support issue tracking and it’s (surprisingly 😉 ) much more deeper integrated as we can ever do with mantis. GitLab offers some project importers f.e. GitHub, Bitbucket, … but Mantis wasn’t an option. So I’ve googled around a few days but i could not found a tool which was not marked as experimental. A few weeks later i’ve searched for an bug in cmake and saw that Kitware (the company behind cmake) also use gitlab and the gitlab issue tracker. I’ve click on the issue i’ve searched for and saw the following line

This issue was created automatically from an original Mantis Issue. Further discussion may take place here.

Kitware also used Mantis before GitLab and it seems that they have build a converter for it. I’ve send a mail about the tool to the mailing-list and i’ve got back an link to the tool which converts mantis issues to GitLab issues. The project has two python 2 scripts but only a “what is it”-readme file, but if you look into the code it is really easy to find out how the tool works. After 1 week of preparation i’ve converted all mantis-projects we have to GitLab issues thanks to this great tool from Kitware.

So if you ever come to the same situation, here is the link to the mantis-to-gitlab tool they have build

Kitware, thank you very much, you are awesome.