Running custom commands with VS Code and CMake Tools
Asked Answered
V

1

6

I am using VS Code with the CMake Tools extension, and I would like to run a simple MPI program. Everything compiles just fine, and I can run my code in the terminal using

mpiexec -n 6 "path-to-my-workspace\build\my-executable.exe"

However, I would like to set things up so that this gets executed automatically when I press Control + Shift + P > "CMake: Run Without Debugging" (or Shift F5).

I read in the CMake Tools documentation that I could create new launch targets in a launch.json file. However, the VS Code documentation states that

The launch.json file is used to configure the debugger in Visual Studio Code.

I want this for debug, but also for release. Should I still use a launch.json file? I had the same problem in the past when I needed to pass arguments to my main function. What is the right way to do that in VS Code with CMake Tools?

Vengeful answered 20/2, 2020 at 11:7 Comment(1)
you may be interested in the "tip" stated at code.visualstudio.com/docs/editor/debugging#_run-modeRhomb
R
0

This sounds like two cases of terminology-related confusion.

  1. In VS Code, there are actions to "Run Code" and "Run Without Debugging". You can find more about the distinction between the two in this question. In a nutshell, "Run Code" runs the program inside a debugger program (like gdb), and and "Run Without Debugging" also runs the program, but doesn' run it inside a debugger program.

  2. The two most common build modes are "debug" and "release", where debug includes debugging symbols that enable running in a debugger, and release does not and instead is built with more optimizations. VS Code can run both types of builds without debugging, and can run debug builds in a debugger. For release builds, it can only run outside a debugger.

You specify that you only care to run outside a debugger.

To answer your question "Should I still use a launch.json file?", yes, you can still use a launch.json file.

For running with a debugger, note that you can use the cmake.debugConfig setting, which will integrate with the features that CMake Tools provides for debugging targets.

Also, note:

Tip: The Run action is always available, but not all debugger extensions support 'Run'. In this case, 'Run' will be the same as 'Debug'.

Another note: If the program you wanted to run wasn't a build output of your project, then you would instead probably want to be using tasks.json.

If you like docs, see https://github.com/microsoft/vscode-cmake-tools/blob/main/docs/debug-launch.md.

Rhomb answered 28/9, 2022 at 7:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.