microsoft / vscode-cmake-tools

CMake integration in Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=vector-of-bool.cmake-tools
MIT License
1.47k stars 451 forks source link

If there are multiple targets in a cmake project, how to set different args for each target? #3312

Open CChuancey opened 1 year ago

CChuancey commented 1 year ago
"cmake.debugConfig": {
  "args": [
    "-c",
    "/path/to/cfgfile"
  ]
}

Originally posted by @bobbrow in https://github.com/microsoft/vscode-cmake-tools/issues/121#issuecomment-887070667

bobbrow commented 1 year ago

Unfortunately, the "quick debugging" feature wasn't really designed for frequent switching between targets. The only way to do this today is to write a launch.json file and create a launch configuration for each of your targets.

CChuancey commented 1 year ago

Unfortunately, the "quick debugging" feature wasn't really designed for frequent switching between targets. The only way to do this today is to write a launch.json file and create a launch configuration for each of your targets.

Thanks for the reply, I tried to add the launch.json file, but when I run the program through the run button provided by cmake-tools, the parameters are not passed to the corresponding target. My launch.json file is as follows:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "test",
      "type": "cppdbg",
      "request": "launch",
      "program": "${workspaceFolder}/build/examles/01_recv",
      "args": [
        "1",
        "2"
      ],
      "MIMode": "gdb",
      "cwd": "${workspaceFolder}",
      "preLaunchTask": ""
    }
  ]
}
bobbrow commented 1 year ago

The run button provided by CMake Tools is not connected to launch.json. VS Code has a UI around that. Click on the "run and debug" icon in the side bar to open it up. Your configurations from launch.json should show up in the drop down control.

image

CChuancey commented 1 year ago

The run button provided by CMake Tools is not connected to launch.json. VS Code has a UI around that. Click on the "run and debug" icon in the side bar to open it up. Your configurations from launch.json should show up in the drop down control.

image

Thank you, I see. When I try to use this method to run the program, vscode has to start in the form of debug every time. Is there a way to configure it in launch.json to only run without debugging?

bobbrow commented 1 year ago

Unfortunately, run without debugging is not available in the C++ extension yet. 😢 https://github.com/microsoft/vscode-cpptools/issues/1201 (you can add another 👍 to that issue). If run without debugging was your goal, I'm sorry I led you to a dead end.

CMake Tools's run button doesn't go through the C++ extension, but doesn't support target-specific configurations. While typing this I had an idea about how the implementation could look.

"cmake.debugConfig": {
  "args": [],  // default config for all debug/launch operations
  "[<target-name>]" : {
    "args": [ ... ]
    // other config overrides for this target conforming to the 'debugConfig' setting spec
  }
}
CChuancey commented 1 year ago

Thank you, this is what I want too.

sun-xm commented 5 months ago

any estimation on when this feature will be online? It could be very useful.

starball5 commented 2 months ago

Related: #2370.

Related on Stack Overflow: In CMake Tools, how to have different args for launching different targets?