I have two tasks build all and C/C++: build in my tasks.json defined below. build all calls C/C++: build. I have launch.json that calls tasks:
{
"version": "0.2.0",
"configurations": [
{
//...
//"preLaunchTask": "C/C++: build"
"preLaunchTask": "build all"
//...
}
]
}
If task calls C/C++: build, everything goes fine. If task calls build all nothing is happening. Why build all not calls C/C++: build?
tasks.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: build",
"command": "/usr/bin/g++",
"args": [
"-g",
"${workspaceFolder}/*.cpp",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: /usr/bin/g++"
},
{
"label": "build all",
"dependsOn": [
"C/C++: build"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
I have a CMake CPP project which has a dependency on the SDK environment. Before running the CMake I need to source the environment setup file.
build$source qemux86/environment-setup-i586-poky-linux
build$cmake ..
build$make
I created the tasks.json file which having the above commands.
{
"version": "2.0.0",
"tasks": [
{
"label": "cmake build",
"type": "shell",
"command": "source qemux86/environment-setup-i586-poky-linux;cmake ..",
"options": {
"cwd": "${workspaceFolder}/build"
}
},
{
"label": "make build",
"options": {
"cwd": "${workspaceFolder}/build"
},
"command": "make",
"args": [
"-j3"
]
},
{
"label": "Build",
"dependsOn": [
"cmake build",
"make build"
],
"dependsOrder": "sequence",
"problemMatcher": [],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
It's working fine for me, but is there any way to run the source command separately, currently, I am running both source, CMake command in a single task
I feel like this should have been very straightforward but googling leads me to various blogposts with no direct answer.
I want to have a few different build tasks:
1) Just Build with debugging on
2) Build with debugging off
3) Run the binary output file
4) Build with debugging off and run the binary output file.
{
"version": "2.0.0",
"tasks": [
{
"label": "Build with Clang and Debugging",
"type": "shell",
"command": "clang++",
"args": [
"-std=c++11",
"-stdlib=libc++",
"helloworld.cpp",
"-o",
"helloworld.out",
"--debug"
],
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "Build with Clang without Debugging",
"type": "shell",
"command": "clang++",
"args": [
"-std=c++11",
"-stdlib=libc++",
"helloworld.cpp",
"-o3",
"helloworld.out"
],
"group": {
"kind": "build",
"isDefault": false
}
}
]
}
I am not sure how to create the above tasks and run them. I tried creating a new task for building without debugging and there seems to be a syntax error. I also want to just run (by a keyword shortcut) without manually typing for instance, ./helloworld.out on the terminal.
And the "isDefault": false seems to be giving me some syntax error.
Is there a way to have all these tasks and assign a keyboard shortcut to each of them?
I'm setting up a portable enviroment based in VSCode that can run from a USBdrive. I've installed MinGW and VScode, at the root directory (D:) and created a folder that will contain the C++ env. configuration.
Edit:
This is intended to work on Windows.
So, I know that in order to compile and run a .cc file I have to run a Build Task or Task (I just understand the basic concept). I've tried to build the .json task that should do that but I'm not gettig any result.
I would like to understand the basics so I can create my own (and simple) .json tasks for other enviroments.
This is what I tried so far:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "Compile&Run",
"command":["D:\\mingw-w64\\x86_64-8.1.0-win32-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe"],
"args": [
"-g",
"${file}",
"-o", // I think that this is the problem
"${fileBasenameNoExtension}.out", ",", "./${fileBasenameNoExtension}.out"
],
//I do not fully understand what this next lines mean or do, they came by defaul.
"options": {
"cwd": "D:\\mingw-w64\\x86_64-8.1.0-win32-seh-rt_v6-rev0\\mingw64\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
This is the task.json I have in my linux system, I got it by searching templatesand managed to make it work. It does just what I need.
Creates and run a .out file.
{
"version": "2.0.0",
"tasks": [
{
"label": "debug",
"type": "shell",
"command": "",
"args": ["g++","-g", "${relativeFile}", "-o","a.exe"]
},
{
"label": "Compile and run",
"type": "shell",
"command": "",
"args": [
"g++","-g", "${relativeFile}", "-o","${fileBasenameNoExtension}.out", "&&", "clear" , "&&" , "./${fileBasenameNoExtension}.out"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": {
"owner": "cpp",
"fileLocation": ["relative", "${workspaceRoot}"],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
},
]
}
Since you pretty much want a template, I will post mine for you to reverse engineer.
This will probably get down voted since it's not a complete answer, but it's the only way I can help you.
{
{
"version": "2.0.0",
"tasks": [
{
"label": "Build & run",
"type": "shell",
"command": "path/to/bin/g++ main.cpp -o main.exe && main",
"problemMatcher":"$gcc",
"presentation": {
"echo": false,
"reveal": "always",
"focus": false,
"panel": "shared",
"showReuseMessage": false,
"clear": true
},
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
tasks.json is not the file you want to use for running/debugging an executable. For that, you want to use launch.json. You can create a launch task that takes a dependency on a build task.
Documentation here: https://code.visualstudio.com/Docs/editor/debugging#_launch-configurations
and here (C++ specific): https://code.visualstudio.com/docs/cpp/launch-json-reference
{
"version": "2.0.0",
"tasks": [
{
"label": "Compile and run",
"type": "shell",
"command": "g++",
"args": [
"-g",
"${file}",
"-o",
"ans.exe",
";",
"cls",
";",
"./ans.exe"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": {
"owner": "cpp",
"fileLocation": [
"relative",
"${workspaceRoot}"
],
"pattern": {
"regexp": "^(.):(\\d+):(\\d+):\\s+(warning|error):\\s+(.)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
},
]
}
This is working very fine
If someone is looking for a way to auto build the cpp files in VSCode before debug (on pressing F5), a simple solution is to add "preLaunchTask": "${defaultBuildTask}", in the launch.json file.
Here are my files in linux with g++ for example (you can remove the smfl libs)
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${workspaceFolder}/src/*.cpp",
"-o",
"${workspaceFolder}/bin/outputfile",
"-lsfml-audio",
"-lsfml-graphics",
"-lsfml-network",
"-lsfml-system",
"-lsfml-window"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: /usr/bin/g++"
}
]
}
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": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/bin/outputfile",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"preLaunchTask": "${defaultBuildTask}",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
I'm trying to run cmake with ctrl+shift+B like so:
{
"version": "2.0.0",
"tasks": [
{
"label": "cmake",
"type": "shell",
"options": {
"cwd": "${workspaceRoot}/build"
},
"command": "cmake ${workspaceRoot} -G \"MinGW Makefiles\"",
(...)
},
{
"label": "make",
"type": "shell",
"command": "mingw32-make.exe",
"options": {
"cwd": "${workspaceRoot}/build"
},
(...),
"dependsOn":["cmake"]
},
{
"label": "build",
"type": "shell",
"options": {
"cwd": "${workspaceRoot}/build"
},
"group": {
"kind": "build",
"isDefault": true
},
"dependsOn": ["make"]
}
]
}
But no matter what I do It runs on ${workspaceRoot} instead of the ${workspaceRoot}/build one:
Executing task in folder cpppractice: cmake C:\workspace\cpp\cpppractice -G "MinGW Makefiles"
Is there anyhting wrong with this approach? As far as I understand the cwd variable in the options item should work.
{
"label": "cmake",
"type": "shell",
"options": {
"cwd": "${workspaceRoot}/build"
},
"command": "cmake \"MinGW Makefiles\" ${workspaceRoot}",
},
This works, It seems that the "Executing task in folder cpppractice:" is not accurate and it is executing it in the correct place, as for why it did not work previously I think it's parsing the args incorrectly? I can't confirm, but here is the output:
Executing task in folder cpppractice: cmake "MinGW Makefiles"
C:\workspace\cpp\cpppractice <
-- Configuring done
-- Generating done
-- Build files have been written to: C:/workspace/cpp/cpppractice/build
Where previously it was compalining about not being able Use the generator "MinGW" which means that it separating the argument "MinGW Makefiles".
After some tinkering I found out that this is also an answer:
{
"label": "cmake",
"type": "shell",
"options": {
"cwd": "${workspaceRoot}/build"
},
"command": "cmake",
"args": [
"-G",
"'MinGW Makefiles'",
"./.."
],
...
},
I actually find the second approach a little bit cleaner but both work the same, So in order to pass an argument as a string you have to use single quotes like so:
...
"command":"echo",
"args": [
"'Za Warudo!'",
],
...
You pass arguments to cmake wrongly. The whole string cmake ${workspaceRoot} -G "MinGW Makefiles" is treated as a command name. Arguments must be listed in the args array.
{
"version": "2.0.0",
"tasks": [
{
"label": "cmake",
"type": "shell",
"options": {
"cwd": "${workspaceRoot}/build"
},
"command": "cmake",
"args": [
"${workspaceRoot}",
"-G",
"\"MinGW Makefiles\""
]
},
...
]
}