CMake for Visual Studio Developers

Visual Studio is a good Integrated Development Environment (IDE) for C++. However, its build system with projects and solutions is rather clunky.

  • The layout and content is not readable with its excessive XML tags. It’s more machine friendly. Often, trying to figure out changes made as part of a commit is a nightmare
  • The UI Project -> Properties to modify the project file is cluttered. Finding options in the various tabs and tree items can be a challenge.
  • Compatibility between different versions of Visual Studio is quite messy too. It’s high maintenance if you have to support multiple versions of Visual Studio.
  • Projects don’t transfer well when there is a change in working directory path.

CMake is an open-source, cross-platform family of tools designed to build, test and package software. CMake is used to control the software compilation process using simple platform and compiler independent configuration files, and generate native makefiles and workspaces that can be used in the compiler environment of your choice.

Its configuration file(s) CMakeLists.txt is simpler and cleaner, a good alternative to native Visual Studio projects and solutions. Except for the generation step, there is no impact on your development process and workflow.
Step 1: Generate Visual Studio workspace
Step 2: Open the generated Visual Studio solution and work in the IDE as usual.
Only when you have to make project changes do you have to edit the CMakeLists.txt and repeat step 1 to regenerate the Visual Studio workspace.

  • It being simple text, makes it readable. Viewing commit differences is just like source code.
  • Gives more flexibility to organize and structure your code and dependencies.
  • It provides a neutral interface, independent of Visual Studio nuances between versions making it very easy to support multiple versions of Visual Studio.
  • During generation CMake will adjust to any working directory path.
  • It’s a good toolchain to manage a cross platform build system.

However, for someone from a Windows only Visual Studio development environment (with not much experience with Linux and classic Makefile) CMake can be a little intimidating. The language is much more “friendly” to those familiar with Linux style scripting. The documentation though quite detailed, is difficult to parse until you know the basics and know what to look for. Being able to map the CMake commands to the Visual Studio IDE makes learning much easier. See CMake and Visual Studio for an introduction with an example.

So with CMake you have a better build system and the comfort of working in the familiar Visual Studio environment. It was all good, until Microsoft decided to obfuscate the CMake philosophy.

Visual Studio 2017 introduces built-in support for handling CMake projects. This makes it a lot simpler to develop C++ projects built with CMake without the need to generate VS projects and solutions from the command line. – Microsoft

Built-in support to invoke one command line statement to generate VS projects? There was nothing complicated there to simplify.
Anyway, I deciced to give it a try, only to find out that they have opened a can of worms that didn’t exist before.

  • It works in the context of Visual Studio’s new “Open Folder” feature rather than their established project/solution workspace. So you will have to learn the new environment.
  • There are additional settings files to override CMake configuration which will be a source of duplication and confusion.
  • It used the Ninja build system by default. To change it to VS projects, you have to adjust it in a settings file.
  • To launch the debugger for startup executable, you have to introduce a launch file.
  • It uses a MS version of CMake by default but can be changed in a settings file.
  • CTest is run from a new Test menu.

All these extra complexities were not required. All that they had to do, was to provide syntax highlighting for CMakeLists.txt which was missing. I did not find any reason to use their new built-in support.

CMake’s design is to be non intrusive. Microsoft being Microsoft has decided to blur those lines and make you dependent on their ecosystem. Use their built-in support at your own risk.

1 thought on “CMake for Visual Studio Developers

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s