17

I use Visual Studio 2017 RC to open a CMake project and then I find the working directory is always the output directory.

Is there any way to set the working directory to somewhere other than the directory of output file?

(Because there is no .sln file, I cannot set the working directory in the old way)

Update I am not calling programs from CMake scripts. I am running the target program in Visual Studio. And I want to change the working directory for the target program.

Null
  • 657
  • 2
  • 6
  • 15
  • **For which purpose** you want to change the working directory? If you run programs within `add_custom_target()` call, you may specify *WORKING_DIRECTORY* option for it. – Tsyvarev Jan 26 '17 at 07:50
  • @Tsyvarev I want to change the working directory of the output program so that some resource(such as images) can be shared between different configurations (Debug/Release). – Null Jan 26 '17 at 08:06
  • `I want to change the working directory of the output program` - Please, **provide the code** how you call this program from CMake script. (Do that **in the question post** itself, not in the comment). BTW, *build directory* (*CMAKE_BINARY_DIR*) is the same for all configurations. There are several artefacts (like libraries and executables) which are created in configuration-dependent directory). – Tsyvarev Jan 26 '17 at 08:16
  • 1
    @Tsyvarev You maybe misunderstand me. **I am not calling the program from CMake script.** I am running the target program in Visual Studio. I think it runs the CMake script and target program separately. And I want to change the working directory for the target program. – Null Jan 26 '17 at 08:39
  • Well, this explains a lot. `I am running the target program in Visual Studio.`, `And I want to change the working directory for the target program.` - Please, add this into the **question post** itself, so your question will be clear without reading the comments. – Tsyvarev Jan 26 '17 at 08:51
  • @Tsyvarev ok, thank for your advice. – Null Jan 26 '17 at 08:59

2 Answers2

34

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.

Community
  • 1
  • 1
congusbongus
  • 13,359
  • 7
  • 71
  • 99
  • Editing `launch.vs.json`worked for me. (Visual Studio 2017 Community) – Felix Nov 07 '17 at 16:01
  • 1
    Visual Studio 2017 15.6.1 now have cmake >= 3.8 support, and it still `VS_DEBUGGER_WORKING_DIRECTORY` unaware. `launch.vs.json` remains the only option! – tower120 Mar 13 '18 at 15:33
  • 3
    For those, who are using VS to build over cmake for a remote system (e.g. Ubuntu 18.04), the VS_DEBUGGER_WORKING_DIRECTORY solution does not work, although using a cmake version >= 3.8.0. You have to use the launch.vs.json approach and set the "cwd" field instead of "currentDir". Also pay attention to use the correct macros for replacements of your local directories. E.g. the remote workspace root can be found with ${debugInfo.remoteWorkspaceRoot}. This was tested with VS Community 2017 , version 15.9.16. – Weana Dec 17 '19 at 09:22
  • 1
    **Documentation** for cmake json is here: https://learn.microsoft.com/en-us/cpp/build/configure-cmake-debugging-sessions?view=msvc-170 – c z May 10 '22 at 14:55
3

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.

vrqq
  • 448
  • 3
  • 8