Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use installed CMake instead of embedded one in Visual Studio 2017

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?

like image 652
Jepessen Avatar asked Mar 11 '18 14:03

Jepessen


2 Answers

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:

  • CMakeSettings.json: cmakeExecutable only working for remote machines

Some Background Information

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

  • https://github.com/Microsoft/CMake/tree/cmake-daemon

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:

  • Issue #16998: Visual Studio 2017: merge Microsoft cmake-daemon branch to master

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.

like image 180
Florian Avatar answered Oct 27 '22 07:10

Florian


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

like image 32
mloskot Avatar answered Oct 27 '22 05:10

mloskot