This is my launch.json file of VsCode :
{
"version": "0.2.0",
"configurations": [
{
"name": "C++ Launch (GDB)",
"type": "cppdbg",
"request": "launch",
"targetArchitecture": "x86",
"program": "${workspaceRoot}\\${fileBasename}.exe",
"miDebuggerPath":"C:\\mingw-w64\\bin\\gdb.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"externalConsole": true,
"preLaunchTask": "g++"
}
]
}
And this one if my tasks.json file :
{
"version": "2.0.0",
"tasks": [
{
"label": "<TASK_NAME>",
"type": "shell",
"command": "make",
// use options.cwd property if the Makefile is not in the project root ${workspaceRoot} dir
"options": {
"cwd": "${workspaceRoot}/<DIR_WITH_MAKEFILE>"
},
// start the build without prompting for task selection, use "group": "build" otherwise
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared"
},
// arg passing example: in this case is executed make QUIET=0
"args": ["QUIET=0"],
// Use the standard less compilation problem matcher.
"problemMatcher": {
"owner": "cpp",
"fileLocation": ["absolute"],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
}
]}
They are worked before and today not. When I run task terminal shows this:
and also it is building :
and then when i debug (F5) program it is shows this :
Please help me how I run c++ program. Thanks for answers !
You have configured a pre launch task called g++ but have no task of that name. You need to change the name of your build task to g++.
You just need to change the value of label to match the name of the task from tasks.json
Related
Background:
I am using vscode on windows to remote debug a linux based server project writen by c++ and built by makefile;
the Binary executable files made by the makefile could be launched by shell or scripts:
./executable_files projects.cfg, and the projects.cfg is a file to tell the project some parameters like port, time_out etc.
task.json:
{
"version": "2.0.0",
"tasks": [
{
"label": "clean",
"type": "shell",
"command": "make",
"args": [
"clean"
],
"problemMatcher": [],
"group": "build"
},
{
"label": "make",
"type": "shell",
"command": "make",
"problemMatcher": [],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) 启动",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/executable_files",
"args": [
"-c",
"${workspaceFolder}/projects.cfg"
],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
I have already set a breakpoints at the entry of the main function
Problems:
when I hit the F5 to debug this projects, it can not even go into the main function, and the debug function do not work to debug by step
enter image description here
Help:
so how can I fix this probelm?
I have some tests which create/read/write/delete a file and I always use the same file name in each of them, therefore I need to run them sequentially to avoid simultaneous operations on the same file. In the command line I can just do it like this
cargo test -- --test-threads=1
however in the VSCode Run/Debug menu it doesn't seem to work. I'm using the autogenerated configuration with Rust Analyzer, plus these extra arguments for the sequential run.
This is my launch.json:
{
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug executable 'my-project'",
"cargo": {
"args": [
"build",
"--bin=my-project",
"--package=my-project"
],
"filter": {
"name": "my-project",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in executable 'my-project'",
"cargo": {
"args": [
"test",
"--no-run",
"--bin my-project",
"--package my-project",
"--", // here I've added the arguments
"--test-threads=1", // here I've added the arguments
],
"filter": {
"name": "my-project",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
}
]
}
And this is the output I get when running the second command (the one which calls cargo test):
Running `cargo test --no-run --bin my-project --package my-project --message-format=json -- --test-threads=1`...
error: Found argument '--bin my-project' which wasn't expected, or isn't valid in this context
Did you mean '--bin'?
If you tried to supply `--bin my-project` as a value rather than a flag, use `-- --bin my-project`
USAGE:
cargo.exe test --no-run --bin [<NAME>]
For more information try --help
VSCode uses a two steps process:
it calls cargo test --no-run to compile the test executable and
it calls the test executable directly.
You should put --test-threads=1 in the last args array:
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in executable 'my-project'",
"cargo": {
"args": [
"test",
"--no-run",
"--bin=my-project",
"--package=my-project",
],
"filter": {
"name": "my-project",
"kind": "bin"
}
},
"args": [ "--test-threads=1" ],
"cwd": "${workspaceFolder}"
}
I want the debugger not to be started when there are compilation errors in the code. This situation occurs when I have a successful build, then I make some changes that do not compile, and when I am launching the debugger, build fails, but the debugger is still launched with an old executable.
I know about Features -> Debug -> On task errors option, but it does not change this behavior.
I have the latest version of VS Code on Windows, standard C++ extension and mingw.
This is my pretty much standard configuration:
.code-workspace
{
"folders": [
{
"path": ".."
}
],
"settings": {
"debug.onTaskErrors": "abort"
}
}
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe - debug",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "D:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe debug build"
},
]
}
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe debug build",
"command": "D:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
"args": ["-std=c++17","-Wall","-Wextra","-D","LOCAL","-g","${file}","-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: \"D:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe\""
},
]
}
What else do I need to do?
I create a script, it delete the binary first, then run compile.
If build fail, new exe will not exists, debugger will fail to start.
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 am working with c++ in vscode. I have to configure launch.json file everytime I create and work with a new cpp file. Otherwise, it executes the previous file which I worked on.
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/nxtPrime.cpp.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "C:\\mingw\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "echo"
}
]
}
Is there any simple way so that I need not edit launch.json file to give the name of new cpp file every time??
Thanks in advance.
I'm using VS Code writing C# programs who are kind of relatable to C++
I also chose the Framework "net5.0" and below, nothing of .NET Core even if that name appears in the config.
When you create a new project two files are created on the folder .vscode the launch.json and task.json, the first one is for you, just tweak it a little in the path.
For launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/${relativeFileDirname}/bin/Debug/net5.0/${relativeFileDirname}.dll",
"args": [],
"cwd": "${workspaceFolder}/${relativeFileDirname}",
"console": "internalConsole",
"stopAtEntry": false
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach"
}
]
}
For the task.json:
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/${relativeFileDirname}/${relativeFileDirname}.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/${workspaceFolderBasename}/${workspaceFolderBasename}.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"--project",
"${workspaceFolder}/${workspaceFolderBasename}/${workspaceFolderBasename}.csproj"
],
"problemMatcher": "$msCompile"
}
]
}
I tested it using the variable references from the page up to date 2022-04-06 that #Mark recommended above: https://code.visualstudio.com/docs/editor/variables-reference