4

Visual Studio's debugger's default working directory is $(ProjectDir).

What I really want is it to be set to $(TargetDir) (where the .exe I am running is located).

This answer provides the correct syntax, so I tried the following:

if (CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
  set_target_properties(${PROJECT_NAME} PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY ${PROJECT_BUILD_DIR}/Debug)
endif()

However this hard-codes Debug, which I don't like.

I tried $<CONFIG> at the end but that was invalid in Visual Studio (the expression is not evaluated).

I also tried $(TargetDir), but that doesn't work. But if I manually delete the variable $(TargetDir) and paste it back in, it works.

How can I set the debugger working directory to the executable output directory without hard-coding it?

tambre
  • 4,625
  • 4
  • 42
  • 55
Startec
  • 12,496
  • 23
  • 93
  • 160

1 Answers1

3

This can be accomplished by setting the debugger working directory as follows:

set_property(TARGET my_target PROPERTY VS_DEBUGGER_WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR})

CMAKE_CFG_INTDIR resolves to the build variable, which provides the current target configuration in Visual Studio.

tambre
  • 4,625
  • 4
  • 42
  • 55