How to set working directory for Visual Studio 201

2019-04-08 22:53发布

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.

1条回答
仙女界的扛把子
2楼-- · 2019-04-08 23:36

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.

查看更多
登录 后发表回答