12

I've a CMake project. It uses the last version of Boost (1.66.0) that are supported in actual installed CMake version (3.11.0 rc2) but not in previous one (3.10.0).

If I build it with CMake from command line, everything is ok, but if I open the folder in Visual Studio 2017, I obtain an error because Visual Studio uses a CMake installation that's not mine, but is the one embededed with its installation: in the output panel the full cmake command path is C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO\2017\COMMUNITY\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\CMAKE\CMake\bin\cmake.exe, that's not the version that I've installed and it is also the previous version (3.10.0) so project it does not compile.

Is there a way to tell to Visual Studio to use my CMake installation instead of its one?

Jepessen
  • 11,744
  • 14
  • 82
  • 149

4 Answers4

7

No (with the exception of the trick shown below), you can only use your own CMake version when doing Visual C++ for Linux Development with CMake on a remote machine with a CMakeSettings.json like this:

{
      "name": "Linux-Debug",
      "generator": "Unix Makefiles",
      "remoteMachineName": "${defaultRemoteMachineName}",
      "configurationType": "Debug",
      "remoteCMakeListsRoot": "/var/tmp/src/${workspaceHash}/${name}",
      "cmakeExecutable": "/usr/local/bin/cmake",
      "buildRoot": "${env.LOCALAPPDATA}\\CMakeBuilds\\${workspaceHash}\\build\\${name}",
      "remoteBuildRoot": "/var/tmp/build/${workspaceHash}/build/${name}",
      "remoteCopySources": true,
      "remoteCopySourcesOutputVerbosity": "Normal",
      "remoteCopySourcesConcurrentCopies": "10",
      "cmakeCommandArgs": "",
      "buildCommandArgs": "",
      "ctestCommandArgs": "",
      "inheritEnvironments": [ "linux-x64" ]
}

But you can give support to a feature request utilizing the cmakeExecutable property more generally:

Some Background Information

As with @usr1234567's answer Visual Studio 2017 uses - as of Version 15.6.1 - it's own branch of CMake:

That the version shipped with Visual Studio 2017 is not an official build you can see be calling:

> "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake" --version
cmake version 3.10.18011902-MSVC_2

So I'm not sure if a official CMake release would nicely/fully integrate into Visual Studio 2017. But there is already a request to merge the Microsoft specific changes back to CMake's main branch:

EDIT: Possible Workaround

A short test has shown that I could trick Visual Studio into taking your installed version by doing a simple renaming of Visual Studio's CMake folder and replacing it a symbolic link to your systems installed CMake version (from a cmd prompt with a administrative rights):

> ren "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake" _CMake
...
> mklink /d "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake" "C:\Program Files\CMake"
...

Warning: You have to undo this with before you update Visual Studio 2017. Otherwise the VS2017 udpate process will replace/overwrite your original CMake installation.

Florian
  • 39,996
  • 9
  • 133
  • 149
  • I was thinking the same thing about the workaround, but I'd like to have a more clean and supported solution. It's strange that the option is supported for some target and not for others, in my opinion, even if I understand that a remote building needs a remote cmake installation... – Jepessen Mar 12 '18 at 08:35
  • @Jepessen I totally agree. I just think that the CMake support in Visual Studio is still in its early stages. With each VS2017 release they are adding more and more features. For a clean solution we just have to give support to those feature requests not yet implemented and wait for their implementation. – Florian Mar 12 '18 at 08:42
  • @Jepessen And the remote CMake makes sense, since you want to build for the remote platform and probably won't have the correct cross-compiling C++ toolchain on your Windows PC. – Florian Mar 12 '18 at 08:44
  • 1
    The workaround with a renamed CMake folder might lead to errors. Microsoft patches their CMake. I don't know what they actually patch and what feature you break with this. – usr1234567 May 04 '18 at 19:17
  • 1
    @usr1234567 The MS specific branch is merged into the master, so the future versions of CMake will work just fine by overwriting the folder – Yorick de Wid May 05 '19 at 14:22
1

You have already learned from other answers that currently, that is using VS2017 15.6.6 and VS2017 15.7.0 Preview 3.0, you can not force Visual Studio to use your preferred installation of CMake.

However, you can approach your actual problem differently and work around it.

It uses the last version of Boost (1.66.0) that are supported in actual installed CMake version (3.11.0 rc2) but not in previous one (3.10.0).

Simply, download newer version of FindBoost.cmake and install it inside CMAKE_BINARY_DIR, then point your CMakeLists.txt to prefer it:

if (CMAKE_VERSION VERSION_LESS 3.11)
  # Latest FindBoost.cmake has likely been updated to detect Boost version not yet released
  if (NOT EXISTS "${CMAKE_BINARY_DIR}/cmake/FindBoost.cmake")
    message(STATUS "Downloading FindBoost.cmake from https://gitlab.kitware.com/cmake/ release branch")
    file(DOWNLOAD
      "https://gitlab.kitware.com/cmake/cmake/raw/release/Modules/FindBoost.cmake"
      "${CMAKE_BINARY_DIR}/cmake/FindBoost.cmake")
  endif()
  list(INSERT CMAKE_MODULE_PATH 0 ${CMAKE_BINARY_DIR}/cmake)
endif()

I've used this approach for quite a while now for Boost.GIL, https://github.com/boostorg/gil

mloskot
  • 37,086
  • 11
  • 109
  • 136
  • This answer is not compatible with Visual studio 2017 with embedded cmake version 3.12 and vcpkg which has boost 1.69, because last version of FindBoost.cmake uses some functions which are not available in cmake 3.12 – Seyed Mehran Siadati Nov 07 '19 at 11:00
  • 1
    @SeyedMehranSiadati This is not a generic version-agnostic answer. I clearly specified what versions this answer applies to. It is not guaranteed to work with later versions of VS and CMake. – mloskot Nov 07 '19 at 13:42
0

in Visual Studio 2022, setting the cmakeExecutable option in CMakeSettings.json will make VS use your preferred cmake. (https://stackoverflow.com/a/58774277/16550663)

You can also hide the your machine-specific path using environment variable. (https://stackoverflow.com/a/72612017/16550663)

irous
  • 401
  • 3
  • 8
-1

For Visual Studio 2015 it was not supported to use an external CMake.
Source: Marians answer to Dmitry's question / comment.

The wording in a recent blog post (January 2018)

In our effort to make sure you have access to the latest features of CMake, we have upgraded the version of CMake that ships with Visual Studio from 3.9 to 3.10.

doesn't indicate this has not changed.

Update: There as a recent preview from Visual Studio including CMake 3.11.

usr1234567
  • 21,601
  • 16
  • 108
  • 128
  • @MSHGOL I only found web resources covering 2015. So, you can assume this holds for 2017, too. This changed in 2018. – usr1234567 Oct 19 '19 at 11:48