Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 964
Null Avatar asked Jan 26 '17 00:01

Null


People also ask

Where does Visual Studio install CMake?

More precisely, the default path where you'll find the compiler is C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin . The compiler is cl.exe .

How does CMake work with Visual Studio?

Visual Studio adds CMake items to the Project menu, with commands for viewing and editing CMake scripts. The Solution Explorer displays the folder structure and files. Visual Studio runs CMake and generates the CMake cache file ( CMakeCache. txt ) for the default configuration.

How do I open a CMake file in Visual Studio?

On the Visual Studio main menu, choose File > Open > CMake. Navigate to the CMakeLists. txt file in the root of the bullet3 repo you just downloaded. As soon as you open the folder, your folder structure becomes visible in the Solution Explorer.


2 Answers

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.

like image 97
congusbongus Avatar answered Sep 23 '22 16:09

congusbongus


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.

like image 30
vrqq Avatar answered Sep 21 '22 16:09

vrqq