0

Is there any way in visual studio to allow your projects CMakeLists.txt files to control everything and not have VS override values?

Meaning that if I set CMAKE_INSTALL_PREFIX in my CMakeLists.txt file visual studio overrides this value. I know I can set CMakeSettings.json in visual studio to the location I would like it but I would like to control my entire build system in cmake and not have to maintain cmake and visual studio.

NOTE: I am using VS 17.

Matthew
  • 800
  • 3
  • 12
  • 35
  • What do you need the install path for? You normally don't need that for dev/build/test/dev/build/test cycle. I often set the build directory to a common directory and run from there. – Mark Ingram Feb 08 '19 at 21:08
  • 3
    " if I set `CMAKE_INSTALL_PREFIX` in my `CMakeLists.txt` file visual studio overrides this value." - It depends on **how exactly** do you set this variable. Show your code. See e.g. [that my answer](https://stackoverflow.com/a/39485990/3440745) about setting default install prefix – Tsyvarev Feb 08 '19 at 21:11
  • @Tsyvarev your idea does not appear to work. I have tried placing it before and after project and neither seems to work. It keeps installing in VS install path as set on the command line by VS. – Matthew Feb 09 '19 at 00:22
  • @Tsyvarev Looks like I made a mistake your comment has worked and is installing in the correct folder. – Matthew Feb 09 '19 at 00:54

2 Answers2

0

This is available in Visual Studio 2019 only, and not on VS 2022.

You could create a "Open Existing Cache" configuration in the CMakeSettings.json. Visual Studio will only allow you to build or install, but it will not generate or alter the CMake cache in any way.

To leverage this you feature can either:

  • Use the menu item File -> Open -> CMake and select the CMakeCache.txt file of the build directory you want to work on. Remember that Visual Studio will automatically open the source directory specified in the CMakeCache.txt file.

  • Create a new "Existing Cache" configuraiton by right clicking on the JSON text editor when editing CMakeSettings.json, or by adding a new configuration on the CMake Settings UI editor. There are two configuration templates:

    • Open Existing cache: use this template when you build/debug on your Windows box;
    • Remote Open Existing cache: use this when you build/debug on a remote Unix machine;
Luca Cappa
  • 1,925
  • 1
  • 13
  • 23
0

Prior to VS 2017, you would generate the visual studio solutions from the CMakeLists.txt using cmake. See Using CMake to generate Visual Studio C++ project files.

In this workflow, VS would not overwrite anything, as it was a loose coupling between CMake and VS. You can still do the same with newer versions of VS and get the full benefits of both CMake and VS.

ap-osd
  • 2,624
  • 16
  • 16