Run instead of debugging in Visual Studio Code - visual-studio-code

I would like to run my simple program in Visual Studio Code without debugging but all programs run in debugging mode.
I used CTRL + F5 (Run without debugging) but automatically go to debugging mode.
Could you please help me?
Tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\MinGW\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:\\MinGW\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: C:\\MinGW\\bin\\g++.exe"
}
]
}

Related

Command Line Arguments Issue

I have an executable file a.out in my workspace that I am trying to call with the argument hello.txt.
When I run ./a.out hello.txt in the terminal, it works. However, when try to do this with VSC, hello.txt is not getting passed to the executable.
I've verified my json files extensively, but I cannot figure out where the issue is coming from. Any help would be greatly appreciated. My tasks.json and launch.json are below:
tasks.json:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc-9 build active file",
"command": "/usr/bin/gcc-9",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/a.out"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
launch.json:
{
"name": "C Launch",
"request": "launch",
"program": "${workspaceFolder}/a.out",
"args": ["${workspaceFolder}/hello.txt"],
"cwd": "${workspaceFolder}"
}

vscode report that the active file is not a C or C++ source file while building a c file

The vscode report an error while building a c program. The error message is as below.
Cannot build and debug because the active file is not a C or C++ source file.
The terminal process failed to launch (exit code: -1).
The task config file is as below.
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/gcc",
"args": [
"-fdiagnostics-color=always",
"-g",
"/home/xxx/tmp/test/main.c"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
I have a c file named main.c under the folder /home/xxx/tmp/test which is the workspace folder. What might be the cause of the problem?
As seen in this reply from Sean McManus, you need to get rid of all ${file} references in your tasks.json, for example:
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
}
change to
"args": [
"-g",
"test.cpp",
"-o",
"libs/test.o"
],
"options": {
"cwd": "${fileDirname}"
}
And change type of build from cppBuild to shell.

vscode cpp build tasks.json type:"cppbuild" error

VSCode : 1.61.0
Linux: Ubuntu 20.04.3 LTS
I'm trying to build c++ program using by VSCode.
when the run build, VSCode showing this message.
I know build tasking need matching launch.json file's preLaunchTask with tasks.json file's label. and I'm already set it.
tasks.json
{
"tasks": [
{
"type": "cppbuild", // but "shell" is working
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
launch.json
{
// 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": "g++ - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++ build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
The problem is, if I change the "type" value in tasks.json, from shell to cppbuild, It doesn't work and show the error message like to.
showing error message when the run build
what is different "cppbuild" and "shell"? and What should I do how using "cppbuild" value?
Te values of cppbuild are fine. Try changing in launch.json the value:
"preLaunchTask": "C/C++: g++ build active file",
to:
"preLaunchTask": "cppbuild",
The difference between shell and cppbuild is that the first open the BASH and runs a command you write, the second, cppbuild, runs an specific binary (or program).

How to add multiple build tasks in Visual Studio Code?

I am trying to use Visual Studio Code to build a Haxe project. I would like to have two build commands, one to build with the -debug option, and one to build without it. Here is my tasks.json file code:
{
"version": "2.0.0",
"tasks":
[
{
"type": "lime",
"command": "test",
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "build: flash",
"command": "haxelib",
"group": {
"kind": "build",
"isDefault": true
},
"args": [
"run",
"lime",
"build",
"flash",
],
"problemMatcher": [
"$haxe-absolute",
"$haxe",
"$haxe-error",
"$haxe-trace"
]
},
{
"label": "debug: flash",
"command": "haxelib",
"args": [
"run",
"lime",
"build",
"flash",
"-debug",
"-Dfdb"
],
"problemMatcher": [
"$haxe-absolute",
"$haxe",
"$haxe-error",
"$haxe-trace"
]
}
]
}
This gives me two tasks named "lime: test flash -debug" and "build: flash". The "lime: test flash -debug" command works fine, but when I run "build:flash" nothing happens. The haxelib command shows up in the terminal, but nothing pops up. I've read both the tasks documentation here and the tasks.json schema here and I still can't figure out how to do this. Can anyone tell me how to do this? Thanks.

Visual Studio Code: running preLaunchTask with multiple tasks

I am trying to figure out how to run multiple tasks at once in the prelaunchtask of the launch.json file.
My code in the tasks.json is as follows:
"version": "2.0.0",
"tasks": [
{
"label": "CleanUp_Client",
"type": "shell",
"command": "rm",
"args": [
"-f",
"Client"
],
},
{
"label": "Client_Build",
"type": "shell",
"command": "g++",
"args": [
"-g",
"client.cpp",
"-o",
"Client",
"-lssl",
"-lcrypto"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": "$gcc"
}
]
In the launch.json for the preLaunchTask parameter if I only put the build task it works, however I want to run multiple tasks, in this case is the CleanUp_Client and Client_Build.
I tried adding another preLaunchTask - However it looks like you can only use that parameter once, so then I tried:
"preLaunchTask": "build" + "clean",
"preLaunchTask": "build"; "clean",
"preLaunchTask": "build" & "clean",
"preLaunchTask": "build" && "clean",
All with no success, not the correct syntax.
Also as a second part to this I would like to know how the group part of this works, and what it means for "isDefault": true.
For your reference: https://code.visualstudio.com/docs/editor/tasks
Here is something that will work. Basically you make another task in which you include all the other tasks that you want to run on your preLaunchTask with the dependsOn keyword.
Code for reference:
"tasks": [
{
"label": "CleanUp_Client",
"type": "shell",
"command": "rm",
"args": [
"-f",
"Client"
]
},
{
"label": "Client_Build",
"type": "shell",
"command": "g++",
"args": [
"-g",
"client.cpp",
"-o",
"Client",
"-lssl",
"-lcrypto"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": "$gcc"
},
{
"label": "Build",
"dependsOn": [
"CleanUp_Client",
"Client_Build"
]
}
]
In this case you would set your preLaunchTask to "Build" and it will run both tasks.
I am curious if anybody else knows an alternative or the correct syntax to just run several tasks from the launch.json preLaunchTask
I agree with #Revx0r answer, but there is important notice: you need to add to last task dependsOrder field, if you want to run it in sequence:
{
"label": "Build",
"dependsOrder": "sequence",
"dependsOn": [
"CleanUp_Client",
"Client_Build"
]
}