Home > Software engineering >  How do I change the working directory for debugging in VS Code CMakeTools
How do I change the working directory for debugging in VS Code CMakeTools

Time:10-15

I have a VS Code project based on a CMakeLists.txt file, and using the CMakeTools extension I can successfully build the target executable.

However, I need to run the executable in a different directory to the build directory. I cannot find a setting to either:

  1. The built executable is placed in a different directory to the build directory and then run from there
  2. The build executable is run from a different working directory

How can I achieve my goal?

CodePudding user response:

You can change the output directory for the executable using the RUNTIME_OUTPUT_DIRECTORY property. When debugging, the executable is run in that directory. For example:

set_target_properties(my_target
    PROPERTIES
    RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
)

If you only want to change the current working directory (cwd), you can create your custom .vscode/launch.json. The content may depend on your OS and compiler:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Run CMake Target",
            "type": "cppvsdbg", // use cppdbg on linux
            "request": "launch",
            "program": "${command:cmake.launchTargetPath}",
            "cwd": "${workspaceFolder}/bin"
        }
    ]
}
  • Related