How to set working directory for Visual Studio 2017 RC CMake Project

Using currentDir, for example:

{
  "version": "0.2.1",
  "defaults": {},
  "configurations": [
    {
      "type": "default",
      "project": "CMakeLists.txt",
      "name": "testd.exe (Debug\\testd.exe)",
      "currentDir": "${workspaceRoot}\\app_home",
      "args": [
        "${workspaceRoot}\\app_home"
      ]
    }
  ]
}

The reason why VS_DEBUGGER_WORKING_DIRECTORY not working is:
When we use VisualStudio IDE to manager a project, it just a text editor. For example a cmake project, the IDE just running cmake command by params in CMakeSettings.json

Then if we choose ninja, it will not generate the .sln and .vcproj files, just using build.ninja to drive build process.
If we choose Visual Studio 2019, it will generate a .sln file and some .vcproj file, but these file just a middle step of building. The current VS-IDE window will not load these file, just use command line to utilize the vcproj/sln file to build.
The VS_DEBUGGER_WORKING_DIRECTORY would saved into these middle-step .vcproj files, that invisible of current IDE windows. (Remember: The current IDE windows just a text editor of CMakeLists.txt)

In other words, it doesn't matter between using VS-IDE with cmake and makefile.

Note: We can see the build process in Output window.

enter image description here

Note: Whether we choose Ninja or VisualStudio2019, the backend build tools are same, MSVC.

But how can we use VS-IDE property? Just open the middle-step .sln and Visual Studio will automatically sync these two windows.


As of writing (2017-03-23), it is not possible to set working directory via CMakeLists.txt. Here are some workarounds:

Using launch.vs.json

According to this bug report, you can add the setting inside your Debug and Launch Settings (right click the relevant CMakeLists.txt). This opens the launch.vs.json file, where you can add the working directory using the currentDir variable. Here's an example:

{
  "version": "0.2.1",
  "defaults": {},
  "configurations": [
    {
      "type": "default",
      "project": "CMakeLists.txt",
      "projectTarget": "path\\to\\target",
      "name": "My Awesome Project",
      "currentDir": "${workspaceRoot}/src"
    }
  ]
}

If you want you can go ahead and check that file in; it probably sits in .vs/launch.vs.json.

  • See the Visual C++ Team blog post. It doesn't mention currentDir though.
  • The syntax appears to be very similar to that used by Visual Studio Code, although the keywords are all different - VSCode uses cwd instead of currentDir, for example.

Using CMake >= 3.8.0 with VS_DEBUGGER_WORKING_DIRECTORY

See also: Does CMake offer a method to set the working directory for a given build system to use when running/debugging the project?

VS_DEBUGGER_WORKING_DIRECTORY is a new CMake target property in version 3.8.0. Set it like this:

set_target_properties(
    MyProject PROPERTIES
    VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/bin")

You need to use the old method of standalone CMake, generate .sln/.vcxproj files, open solution with Visual Studio, because the version of CMake integrated with Visual Studio 2017 is 3.6. Which leads to...

Wait until Visual Studio ships with CMake >= 3.8.0

It's unknown when this will happen; the team are currently looking at updating to CMake 3.7 so it'll be a while longer. However when this happens chances are it will support the VS_DEBUGGER_WORKING_DIRECTORY property.