简体   繁体   中英

Set Visual Studio debugger working directory to executable output directory in CMake

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM