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 am trying to compile all files in a certain directory, but g++ is giving an error.
It says "invalid argument". I am using Windows 11. Here is my directory structure
Here is my tasks.json file -
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "g++",
"args": [
"-fdiagnostics-color=always",
"-c",
"${fileDirname}\\*.cpp",
//"-g",
//"-Wall",
//"-m64",
//"-I",
//"include",
//"-I",
//"C:\\SDL2-w64\\include",
//"-o",
//"bin\\debug\\${fileBasenameNoExtension}.exe",
//"-l",
//"C:\\SDL2-w64\\lib",
//"-lmingw32",
//"-lSDL2main",
//"-lSDL2",
//"-lSDL_image",
//"&&",
//"start",
//"bin\\release\\main"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "compiler: C:\\MinGW\\bin\\g++.exe"
}
]
}
For any further details comment it. ANY help would be appreciated.
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "C/C++: g++.exe build active file",
"command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
"args": ["-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe"],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
}
},{
"label": "Run task...",
"type": "process",
"command": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"windows": {
"command": "${fileDirname}\\${fileBasenameNoExtension}.exe"
},
"group": "build",
"options": {
"cwd": "${workspaceFolder}"
},
"dependsOn":[
"C/C++: g++.exe build active file"
]
}
]
}
I went through how to build and run and delete executable file and dummy file in vscode and wrote the above script to build and run .cpp files.
Although, .cpp files are being compiled into .exe but it's not running with the above command.
To, run .exe file i need to run manually by typing this ./path_to_file.exe in the terminal.
It's running fine, but i would like run the file when i press ctrl+shift+B.
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\""
]
},
...
]
}