How does come code runner is so fast then task.json is there a way to achieve same execution speed with task.json? - vscode-code-runner

when i run code using code runner its super fast but it takes like 4 sec for inbuilt task to compile then run. i have my settings below which i think will be of help, if more is needed please let me know.
My task.json settings
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file C++20 Standard",
"command": "D:\\Programming\\msys64\\mingw64\\bin\\g++.exe",
"args": [
"-std=c++20",
"-pedantic-errors",
"-Wall",
"-Weffc++",
"-Wextra",
"-Wsign-conversion",
"-Werror",
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: D:\\Programming\\msys64\\mingw64\\bin\\g++.exe C++20 Standard"
}
]
}
and this is my Code runner setting
"cpp": "cd $dir && g++ -std=c++20 -pedantic-errors -Wall -Weffc++ -Wextra -Wsign-conversion -Werror $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",

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}"
}

How to set C++ standard in gcc compiler on Ubuntu VS Code

I'm running VS Code on freshly installed Ubuntu 22.04 LTS. Whatever I try, my language standard is stuck at c++17. I use gcc compiler.
To check the issue I run the following code:
#include <iostream>
int main()
{
if (__cplusplus == 201703L) std::cout << "C++17\n";
return 0;
}
Output is always the same: C++17
I've set "cppStandard": "c++23", in c_cpp_proporties.json.
I've set C++ standard in C/C++ Configurations settings to c++23.
I've set compiler arguments to -std=c++23.
I've been resetting VS Code, creating new files, reinstalling extensions, nothing.
Snippet from my tasks.json:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
Installed extensions:
C/C++ by Microsoft
C/C++ Extension Pack by Microsoft
C/C++ Themes by Microsoft
CMake Tools by Microsoft
Better C++ Syntax by Jeff Hykin
As many of the commentators pointed out, tasks.json is used for compiling. #Some programmer dude correctly explained that I should put argument inside it. Updating task.json with the following code is the solution:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-std=c++23",
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}

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.

Run instead of debugging in 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"
}
]
}

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"
]
}