I am trying to setup visual studio code to build/run and debug c++ files using g++ compiler on macOS. However when I build or debug the code, I can see that output file is created and i can run it as well however i am not able to debug it in vscode, for debugging gives weird behaviour.
So far I have been able to write tasks which builds the .cpp file and and executes it. The output comes into the terminal of vscode. However when I try with debug a new Terminal opens and no breakpoint is hit.
Here is the tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "Build with g++",
"type": "shell",
"command": "g++",
"args": [
"-Wall",
"-Wextra",
"-Wpedantic",
"-std=c++11",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "run",
"type": "shell",
"command": "cd ${fileDirname}/ && ./${fileBasenameNoExtension}",
"dependsOn": ["Build with g++"]
}
]
}
and here is my launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "(lldb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"showDisplayString": false,
"environment": [],
"externalConsole": true,
"internalConsoleOptions": "openOnSessionStart",
"MIMode": "lldb",
"logging": {
"moduleLoad": false,
"programOutput": true,
"trace": false
},
"preLaunchTask": "Build with g++",
"osx": {
"MIMode": "lldb"
}
}
]
}
I expect that when i hit debug or press f5 the terminal in the vscode should run and I am able to debug the program.
As #alan-birtles pointed out adding -g would be the solution.
Also I found out here that there is no way to use integrated terminal of vscode for c++ as lldb doesn't support it.
Here are the final configuration.
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "(lldb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"showDisplayString": false,
"environment": [],
"externalConsole": true,
"internalConsoleOptions": "openOnSessionStart",
"MIMode": "lldb",
"logging": {
"moduleLoad": false,
"programOutput": true,
"trace": false
},
"preLaunchTask": "Build with g++",
"osx": {
"MIMode": "lldb"
}
}
]
}
task.json
{
"version": "2.0.0",
"tasks": [
{
"label": "Build with g++",
"type": "shell",
"command": "g++",
"args": [
"-g",
"-Wall",
"-Wextra",
"-Wpedantic",
"-std=c++11",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "run",
"type": "shell",
"command": "cd ${fileDirname}/ && ./${fileBasenameNoExtension}",
"dependsOn": ["Build with g++"]
}
]
}
Because your VSCode Debugger Extension has bugger.
Successfully Setup C++ Debug with VSCode on mac:
I use clang/gcc as compiler, Code-Runner(VSCode Extension) as runner, C/C++ Clang Command Adopter(VSCode Extension) as to provide static detection, CodeLLDB(VSCode Extension) to debug C++.
Do Not Download Microsoft C/C++ (Identifier: ms-vscode.cpptools, for C/C++
IntelliSense, debugging, and code browsing. Microsoft Officially produced Extension), Because This CPPtools and that CodeLLDb conflict.
To avoid any issues, please do not download any VSCode Extensions yet.
Here is my configuration about debug C++ with VSCode on mac:
VSCode v1.63.2;
Code Runner v0.11.6 (Author: Jun Han)
C/C++ Clang Command Adapter v0.2.4 (Author: Yasuaki MITANI)
CodeLLDB v1.4.5 (Author: Vadim Chugunov)
Clang v9.0.0 gcc v4.2.1 Intel CPU macOS 10.12
Details of successful configuration refer to here:
Debug C++11 manually with VSCode(mac)
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 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 getting an issue where I can build and run a project with c++17 flags enabled, but I can't actually debug it. The debugger launches in vsCode, but all the c++ 17 features are red squiggled, and those lines never execute properly. I'm guessing it's a problem with my launch.json, but I can't figure out what it is that I'm missing...
I'm running windows 10, and here's the relevant configuration in vsCode:
tasks.json:
{
"tasks": [
{
"type": "cppbuild",
"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": [
"-std=c++17",
"-g",
"${fileDirname}\\*.cpp",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Generated task 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++.exe - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": ["-std=c++17"],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe build active file"
}
]
}
Welp - not much of an answer, but the issue isn't happening when I just use cl.exe as the compiler instead of gcc. I've since just stopped using gcc for this project after trying to get it to work for about an hour.
https://code.visualstudio.com/docs/cpp/config-msvc
I am new to Visual Studio Code. I am trying to debug a simple C++ code.
I edited my launch.json to be able to debug the app like this:
{
"version": "0.2.0",
"configurations": [
{
"name": "(lldb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/Calculator",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "lldb"
}
]
}
When I start debugging, the terminal is opened in the correct folder but the program is not executed. So the Visual Code does not stop in the breakpoints I want to check in the program.
In my task.json I have the following code:
{
"version": "2.0.0",
"tasks": [
{
"label": "Echo vars",
"command": "echo",
"args": [
"${env:USERNAME}",
"workspaceFolder = ${workspaceFolder}"
],
"type": "shell",
"problemMatcher": []
},
{
"label": "build",
"type": "shell",
"command": "g++ -g Calculator.cpp -o Calculator",
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": "$gcc"
}
]
}
Can anybody help me on this?
Thanks in advance
As mentioned in the comments: if your Calculator.cpp is not compiled with debug symbols ie. g++ called without -g flag, you will not be able to debug it.
Hence, add to your launch.json a prelaunchTask entry which will make sure your build task which is compiling your source with debug symbols is always executed prior to launching the debugger.
"environment": [],
"externalConsole": true,
"MIMode": "lldb",
"preLaunchTask": "build"
edit your launch.json to add debugger log output to your project:
"logging": { "engineLogging": true, "trace": false, "traceResponse": false }
When I run the debug, the output from my program isn't displayed on the integrated terminal.
This actually used to work a few days ago. I haven't changed any config files and yet, perhaps due to an update, this has changed.
Note that there is no "console" option with lldb.
This also creates a major issue that I can't provide input to my running program.
This is what I see in the integrated terminal:
[1] + Done "/usr/bin/lldb-mi" --interpreter=mi --tty=${DbgTerm} 0<"/tmp/Microsoft-MIEngine-In-871ntlpp.93i" 1>"/tmp/Microsoft-MIEngine-Out-msz8v24g.agq"
And that's it. The case is the same when using "externalConsole":true.
How do I keep whatever output is currently in the debug terminal inside it, but also see the normal program output and be able to give input in the integrated terminal?
Also, this behavior leads me to think, currently, what is even the point of having the integrated terminal? Nothing is happening there.
here are my config files:
"version": "0.2.0",
"configurations": [
{
"name": "clang++ build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "clang++ build active file",
"miDebuggerPath": "/usr/bin/lldb-mi"
}
]
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": [
"$gcc"
],
"group": "build"
}
]
unfortunately
https://code.visualstudio.com/docs/cpp/launch-json-reference
macOS: When set to true, it will spawn an external console through lldb-mi. When set to false, the output can be seen in VS Code's debugConsole. Due to limitations within lldb-mi, integratedTerminal support is not available.