splaytreez
splaytreez

Reputation: 844

Configure vscode to build and run c++ in one terminal

I installed mingw64 toolchain with MSYS2, and managed to successfully run my code from vscode. However, running it creates two terminals, one for building and one for running the generated file: C/C++: g++.exe build active file and cppdbg: main.exe.

So I'm looking for a way to configure vscode to build and run the app in one terminal, or maybe redirect the compiler output to the output tab. Anything similar to how it's done in other languages.

Also, how can I configure it to run without debugging by default? In launch.json, "configurations" require a "type" parameter, which has only one option, "cppdbg" - why is there no release option?

Upvotes: 1

Views: 1518

Answers (1)

AzuxirenLeadGuy
AzuxirenLeadGuy

Reputation: 2870

You are asking two questions and I can answer both.

  1. How do I build and debug the application, by making sure that it is launched only when the build is successful?

  2. How do I configure it to run the application similarly?

Let me answer 2 first.

To solve this, you need the dependsOn property of tasks. For example, this is the content of my tasks.json file:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": 
    [
        {
            "label": "Debug_Build",
            "type": "shell",
            "command": "g++ -g ./src/main.cpp -o ./bin/a.out",
        },
        {
            "label": "Run main()",
            "type": "shell",
            "command": "./bin/a.out",
            "dependsOn":"Debug_Build" //Previous task is run first, and then this one if previous was successful.
        }
    ]
}

Notice that the second task (which is responsible for running the program) has dependsOn property which behaves exactly as you require: It will run only when the dependsOn task is successful (Which here is the build task)

To run the tasks, you can use the Command Palette to Run

Tasks>Run main()

to launch your task. Personally, I prefer using the extension Tasks, which creates a button for each task in the tasks.json file on the status bar of VS Code.

Now to answer 1, we use a similar property in the launch configuration json file: preLaunchTask

This will be exactly what you need: It will run the debugging only if the preLaunchTask was successful.

My launch.json file is as follows:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/bin/a.out",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "Debug_Build", //This is the part you need
        }
    ]
}

So to summarize, you will need to create tasks.json and launch.json files in your workspace. In the tasks.json file, declare a "build" task, declare the "run" task which depends on the "build" task using the dependsOn property. Finally, in the launch.json file, refer the "build" task in the preLaunch property.

Upvotes: 1

Related Questions