Using Cmake With Visual Studio
Visual Studio 2017 comes with a CMake integration that allows one to just open a folder that contains a CMakeLists.txtand Visual will use it to define the project build.


Conan can also be used in this setup to install dependencies. Let`s say that we are going to build an application that dependson an existing Conan package called hello/[email protected]/testing
. For the purpose of this example, you can quickly create this package by typingin your terminal:
CMake produces Visual Studio solutions seamlessly. This post will map CMake commands to the Visual Studio IDE with an example which makes learning much easier. It is primarily intended for a C developer using Visual Studio. Most references here is based on Visual Studio 2010 but will apply equally well to other versions. Visual Studio runs cmake.exe and generates the CMake cache file (CMakeCache.txt) for the default (x64 Debug) configuration. The CMake command line is displayed in the Output Window, along with additional output from CMake.
The project we want to develop will be a simple application with these 3 files in the same folder:
- Featuring Clang-enhanced compiler, Dinkumware standard library, MSBuild/CMake/Ninja support, and popular libraries like Boost and Eigen. Develop Windows and iOS applications with a single codebase and responsive UI; Enjoy the award winning Visual Designer using the CBuilder VCL and FireMonkey frameworks for maximum productivity.
- I've covered using CMake on Linux, but now it's time to show how to use CMake with Visual Studio on Windows. This video covers generating a VS Solution and t.
If we open Visual Studio 2017 (with CMake support installed), and select “Open Folder” from the menu, and select the above folder,we will see something like the following error:
As expected, our CMakeLists.txt is using an include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
, and that file doesn’t exist yet,because Conan has not yet installed the dependencies of this project. Visual Studio 2017 uses different build folders for eachconfiguration. In this case, the default configuration at startup is x64-Debug
. This means that we need to install thedependencies that match this configuration. Assuming that our default profile is using Visual Studio 2017 for x64 (it should typically bethe default one created by Conan if VS2017 is present), then all we need to specify is the -sbuild_type=Debug
setting:
Now, you should be able to regenerate the CMake project from the IDE, Menu->CMake, build it, select the “example” executable to run, and runit.
Now, let’s say that you want to build the Release application. You switch configuration from the IDE, and then the above error happensagain. The dependencies for Release mode need to be installed too:
The process can be extended to x86 (passing -sarch=x86
in the command line), or to other configurations. For production usage,Conan profiles are highly recommended.
Using cmake-conan¶
The cmake-conan project in https://github.com/conan-io/cmake-conan is a CMake script that runs an execute_process
that automaticallylaunches conan install to install dependencies. The settings passed in the command line will be derived from the current CMakeconfiguration, that will match the Visual Studio one. This script can be used to further automate the installation task:
This code will manage to download the cmake-conan CMake script, and use it automatically, calling a conan install automatically.
There could be an issue, though, for the Release
configuration. Internally, the Visual Studio 2017 defines the configurationType
AsRelWithDebInfo
for Release
builds. But Conan default settings (in the Conan settings.yml file), only have Debug
and Release
defined. It is possible to modify the settings.yml file, and add those extra build types. Then you should create the hello
packagefor those settings. And most existing packages, specially in central repositories, are built only for Debug and Release modes.
An easier approach is to change the CMake configuration in Visual: go to the Menu -> CMake -> Change CMake Configuration. That should openthe CMakeSettings.json file, and there you can change the configurationType
to Release
:
Visual Studio Update Cmake
Note that the above CMake code is only valid for consuming existing packages. If you are also creating a package, youwould need to make sure the right CMake code is executed, please check https://github.com/conan-io/cmake-conan/blob/master/README.md
Using tasks with tasks.vs.json¶
Another alternative is using file tasksfeature of Visual Studio 2017. This way you can install dependencies by running conan install as task directly in the IDE.
All you need is to right click on your conanfile.py -> Configure Tasks (see thelink above) and add thefollowing to your tasks.vs.json.
Warning
The file tasks.vs.json is added to your local .vs folder so it is not supposed to be added to your version control system.
Cmake With Visual Studio Compiler
Then just right click on your conanfile.py and launch your conan install and regenerate your CMakeLists.txt.