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

cmakeworking-directory

I have a project with the following structure:

project_name/CMakeLists.txt
project_name/src
project_name/resources
...
project_name-build/configuration_name/project_name.exe

I want my application to be run in the root project directory project_name so it can directly access resources.

Does CMake provide a method to specify this property, or will I have to manually set it in each build environment I use?

I've looked around in the documentation and haven't found anything other than the possibility of setting up a post-build event to run my project from the desired directory which is less than desirable. I also found that the working directory setting for Visual Studio is saved in a per-user file (.vcxproj.user) which I don't believe CMake generates (which points to the answer being probably no).

Best Answer

Since CMake 3.8, there is the VS_DEBUGGER_WORKING_DIRECTORY target property, which allows you to set the debugger working directory for a target in Visual Studio.

Usage example:

set_property(TARGET MyTarget PROPERTY VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/bin")