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.
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 .
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.
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.
As of writing (2017-03-23), it is not possible to set working directory via CMakeLists.txt. Here are some workarounds:
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
.
currentDir
though.cwd
instead of currentDir
, for example.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...
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With