Set breakpoints in C++ debugger (vs code) - c++

The issue I'm having is that when I run "C++ run/debug active file" the the code executes but doesn't stop at the breakpoints I set. I don't know a lot about how debuggers work but I'm guessing the the debugger isn't listening to the correct process? Or why would this happen?
In all honesty, I've been struggling to get all my dependencies correct and in the process may have broke intended functionality so it could be that I set up the scripts in a way that breaks it's intended use.
tasks.json looks like this:
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "g++",
"args": [
"main.cc",
"-o",
"main",
"-std=c++17",
"-I/usr/local/include/cairomm-1.16",
"-I/usr/local/lib/cairomm-1.16/include",
"-I/usr/include/cairo",
"-I/usr/include/glib-2.0",
"-I/usr/lib/x86_64-linux-gnu/glib-2.0/include",
"-I/usr/include/pixman-1",
"-I/usr/include/uuid",
"-I/usr/include/freetype2",
"-I/usr/include/libpng16",
"-I/usr/include/sigc++-3.0",
"-I/usr/lib/x86_64-linux-gnu/sigc++-3.0/include",
"-L/usr/local/lib",
"-lcairomm-1.16",
"-lcairo",
"-lsigc-3.0",
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "Task generated by Debugger."
},
{
"type": "cppbuild",
"label": "C/C++: g++-9 build active file",
"command": "/usr/bin/g++-9",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"-L/usr/local/lib",
"-lcairomm-1.16",
"-lcairo",
"-lsigc-3.0"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
]
}
launch.json looks like this:
{
"version": "0.0.0",
"configurations": [
{
"name": "gcc - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: gcc build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
c_cpp_properties.json looks like this:
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "g++",
"args": [
"main.cc",
"-o",
"main",
"-std=c++17",
"-I/usr/local/include/cairomm-1.16",
"-I/usr/local/lib/cairomm-1.16/include",
"-I/usr/include/cairo",
"-I/usr/include/glib-2.0",
"-I/usr/lib/x86_64-linux-gnu/glib-2.0/include",
"-I/usr/include/pixman-1",
"-I/usr/include/uuid",
"-I/usr/include/freetype2",
"-I/usr/include/libpng16",
"-I/usr/include/sigc++-3.0",
"-I/usr/lib/x86_64-linux-gnu/sigc++-3.0/include",
"-L/usr/local/lib",
"-lcairomm-1.16",
"-lcairo",
"-lsigc-3.0",
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "Task generated by Debugger."
},
{
"type": "cppbuild",
"label": "C/C++: g++-9 build active file",
"command": "/usr/bin/g++-9",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"-L/usr/local/lib",
"-lcairomm-1.16",
"-lcairo",
"-lsigc-3.0"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
]
}
I reworked it and went slower this time. I don't have a launch file but
Here's tasks:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-std=c++17",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"-I/usr/local/include/cairomm-1.16",
"-I/usr/local/lib/cairomm-1.16/include",
"-I/usr/include/cairo",
"-I/usr/include/glib-2.0",
"-I/usr/lib/x86_64-linux-gnu/glib-2.0/include",
"-I/usr/include/pixman-1",
"-I/usr/include/uuid",
"-I/usr/include/freetype2",
"-I/usr/include/libpng16",
"-I/usr/include/sigc++-3.0",
"-I/usr/lib/x86_64-linux-gnu/sigc++-3.0/include",
"-L/usr/local/lib",
"-lcairomm-1.16",
"-lcairo",
"-lsigc-3.0",
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
And here's c_cpp_properties
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/usr/local/lib",
"/usr/local/lib/cairomm-1.16/include",
"/usr/local/include/cairomm-1.16",
"/usr/include/sigc++-3.0",
"/usr/lib/x86_64-linux-gnu/sigc++-3.0/include",
"/usr/include/cairo",
"/usr/include/freetype2"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu17",
"cppStandard": "gnu++20",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}

I think there are a lot of unnecessary code in your tasks.json , try this out I am successfully able to set breakpoints during debugging .
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\MinGW\\bin\\g++.exe",
"args": [
"-g",
"${workspaceFolder}\\*.cpp",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: C:\\MinGW\\bin\\g++.exe"
}
]
}

Related

C++ Uniform initialization doesn't work with "expected ';' at end of declaration"

I'm the beginner of C++. I set development environment in vscode in Mac.
But I trapped in variable initialization.
Copy initialization and direct initialization are ok, but uniform initialization doesn't work.
#include <iostream>
int main() {
int a(0);
int b = 0;
int c{0};
int d = {0};
int e;
std::cout << "Hello World";
return 0;
}
In this code, int c{0}; doesn't work. It failed to build.
It printed error: expected ';' at end of declaration
What can I try to solve this problem??
I don't know if it helps, but I'll upload ".vscode".
// c_cpp_properties.json
{
"configurations": [
{
"name": "Mac",
"includePath": ["${workspaceFolder}/**"],
"defines": [],
"macFrameworkPath": [
"/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
// 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": "clang++ - Build and debug active file",
"type": "lldb",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb",
"preLaunchTask": "clang++ build active file"
}
]
}
// tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-std=c++17",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"&&",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": "build"
},
{
"type": "cppbuild",
"label": "C/C++: clang++ 활성 파일 빌드",
"command": "/usr/bin/clang++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "디버거에서 생성된 작업입니다."
}
]
}

How to invoke cmd.exe /c cls from within VS Code tasks.json?

I have a simple HelloWorld.cpp file and I want to run it with the following steps, each is run one after the other as follows.
Compile
Clear Integrated Terminal
Run the produced executable.
Unfortunately I fails to setup the second step (clearing the console window). What is the correct setup?
{
"version": "2.0.0",
"tasks": [
{
"label": "COMPILE",
"command": "cl.exe",
"args": [
"/Zi",
"/EHsc",
"/nologo",
"/Fe:",
"${fileBasenameNoExtension}.exe",
"${file}",
"/std:c++latest"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$msCompile"
]
},
{
"label": "CLEAN",
"command": "cmd.exe /c cls",
"dependsOn": "COMPILE"
},
{
"label": "RUN",
"command": "${fileBasenameNoExtension}.exe",
"dependsOn": "CLEAN",
"options": {
"cwd": "${fileDirname}"
},
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
there is a vscode setting with workbench.action.terminal.clear you'll find related settings there,
or in File >Preferences >Keyboard shortcuts. search for Terminal:clear and assign the key
Aha. I found the solution: Add "type": "shell" to the CLEAN step as follows.
{
"type": "shell",
"label": "CLEAN",
"command": "cls",
"dependsOn": "COMPILE"
}
The complete tasks.json.
{
"version": "2.0.0",
"tasks": [
{
"label": "COMPILE",
"command": "cl.exe",
"args": [
"/Zi",
"/EHsc",
"/nologo",
"/Fe:",
"${fileBasenameNoExtension}.exe",
"${file}",
"/std:c++latest"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$msCompile"
]
},
{
"type": "shell",
"label": "CLEAN",
"command": "cls",
"dependsOn": "COMPILE"
},
{
"label": "RUN",
"command": "${fileBasenameNoExtension}.exe",
"dependsOn": "CLEAN",
"options": {
"cwd": "${fileDirname}"
},
"group": {
"kind": "build",
"isDefault": true
}
}
]
}

How to fix "undefined symbols for architecture arm64"

I'm running VS Code on MacOS, and I'm using clang to compile a simple "Hello World!" program in C++. However, when I try to run my program, VS Code gives me the following error message: Undefined symbols for architecture arm64: followed by dozens of references to the std library. At the bottom of the terminal, it says:
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Build finished with error(s).
The terminal process terminated with exit code: -1.
(a) What does this mean? and (b) how can I fix it?
HelloWorld.cpp
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!" << endl;
return 0;
}
tasks.json
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: clang build active file",
"command": "/usr/bin/clang",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "clang - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/HelloWorld.cpp",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb",
"preLaunchTask": "C/C++: clang build active file"
}
]
}
Thanks!
If you have multiple CPP files in the project then you need to add "${fileDirname}/*.cpp"
tasks.json
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-std=c++17",
"-stdlib=libc++",
"-g",
"${fileDirname}/*.cpp",
// "${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
If you're having a vector; error from vscode also add the c_cpp_properties.json file in .vscode directory :
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
],
"macFrameworkPath": [
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}

Mac Os Visual Studio Code C++ Opengl Glad Glew

Link Project
https://github.com/nio74/templateGLad
Good day, I try Run Opengl app with glew and glad in Visual Studio Code(I wanted to use this Ide because it is multi platform but if you have any good suggestions it is welcome), but have this error:
> Executing task: C/C++: clang++ build active file <
Starting build...
Build finished with error:
Undefined symbols for architecture x86_64:
"_gladLoadGLLoader", referenced from:
_main in HelloWorld-22d65f.o
My tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-g",
"${workspaceFolder}/src/HelloWorld.cpp",
"-o",
"${workspaceFolder}",
"-I", "${workspaceFolder}/include/",
//"${workspaceFolder}/glad.c",
"-lglfw",
"-lglew"
],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: /usr/bin/clang++"
}
]
}
Updating, doing various research I managed to make it work, but I have some things to understand:
I can't understand the variables in tasks.Json
(
"--include-directory = include /",
"--include-directory = build /",
"--include = include / glad.c",
)
I tried to replace them with those suggested by the official guide (
"$ {workspaceFolder} / include",
"$ {workspaceFolder} / build",
"$ {fileDirname} /include/glad.c",
)
but they don't work.
tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"label": "HelloWorld",
"type": "che",
"command": "clang++",
"args": [
"-std=c++17",
"-stdlib=libc++",
"-lglfw",
"-lglew",
"--include-directory=include/",
"--include-directory=build/",
"--include=include/glad.c",
//"${workspaceFolder}/include",
//"${workspaceFolder}/build",
//"${fileDirname}/include/glad.c",
"-framework",
"OpenGL",
"-framework",
"IOKit",
"-framework",
"Cocoa",
"src/helloworld.cpp",
"-o",
"build/helloworld",
"--debug"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": []
}
]
}
launch.json
"version": "0.2.0",
"configurations": [
{
"name": "(lldb) Launch",
"preLaunchTask": "HelloWorld",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/helloworld",
"args": [],
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "lldb",
}
]
}
c_cpp_properties.json
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/src/",
"${workspaceFolder}/include/",
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1",
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include"
],
"defines": [],
"macFrameworkPath": [
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x86",
"browse": {
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
},
"compilerArgs": [
"-lglfw",
"-lglew"
]
}
],
"version": 4
}
Git:
https://github.com/nio74/templateGLad

how to build and run and delete executable file and dummy file in vscode

I want to build and run c++ source code. And then delete all except source code.
{
"tasks": [
{
"type": "shell",
"label": "g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"&",
"${fileDirname}/${fileBasenameNoExtension}",
"&",
"rm ",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "/usr/bin"
},
"group": {
"kind": "build",
"isDefault": true
}
}
],
"version": "2.0.0"
}
I except they build and run and then delete worthless files, but it is not working.
Taking a look at VScode tasks parameters, you see that we can chain them with dependsOn. Note that the required command can contain the full path to where your tool is located when it's not included in your OS path variable, but options.cwd should contain the path to where your tool should run, i.e. ${fileDirname}. Also you should study the presentation parameter and adjust it for the way you want to see errors.
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
],
"presentation": {
"reveal": "silent",
"panel": "shared"
},
"options": {
"cwd": "${fileDirname}"
},
},
{
"label": "cpp-run",
"type": "process",
"command": "${fileDirname}/${fileBasenameNoExtension}",
"dependsOn": [
"g++ build active file"
],
"presentation": {
"reveal": "always",
"panel": "shared"
},
"options": {
"cwd": "${fileDirname}"
},
},
{
"label": "cpp-test",
"type": "process",
"command": "rm",
"args": [
"${fileDirname}/${fileBasenameNoExtension}"
],
"group": {
"kind": "build",
"isDefault": true
},
"dependsOn": [
"cpp-run"
],
"presentation": {
"reveal": "never",
"panel": "shared"
},
"options": {
"cwd": "${fileDirname}"
},
}
],
}