The errors do not show on VS Code - visual-studio-code

I'm setting up my VS Code for C++. I use version 1.31.1 x64 on windows 10, install C/C++ extension version 0.21.0. Below are my settings:
c_cpp_properties.json
{
"configurations": [
{
"name": "MinGW",
"includePath": [
"${workspaceFolder}/**",
"C:/MinGW/lib/gcc/mingw32/8.2.0/include/**"
],
"defines": [
"LOCAL"
],
"cStandard": "c11",
"cppStandard": "c++14",
"intelliSenseMode": "clang-x64",
"compilerPath": "C:/MinGW/bin/g++.exe"
}
],
"version": 4
}
task.json
{
"version": "2.0.0",
"tasks": [
{
"label": "C++ compile and run",
"type": "shell",
"command": "g++ \"${file}\" -O2 -static -std=c++14 -DLOCAL -o \"${fileDirname}/${fileBasenameNoExtension}.exe\" && \"${fileDirname}/${fileBasenameNoExtension}.exe\"",
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"clear": true,
"echo": false,
"panel": "shared"
}
}
]
}
My simple test program:
#include <iostream>
using namespace std;
int main() {
cout << "hello world"
}
I ran the code by above task. The code was missing a semicolon, but the errors were just shown in terminal tab, while nothing appears in problems tab, also on text editor. Did i miss something?

I found the setting to turn on the error squiggles:
Preferences -> Setting -> search for Intellisense -> choose Enable in C_Cpp: Error Squiggles.
You should check both User settings and Workspace settings.

Related

How to set C++ standard in gcc compiler on Ubuntu VS Code

I'm running VS Code on freshly installed Ubuntu 22.04 LTS. Whatever I try, my language standard is stuck at c++17. I use gcc compiler.
To check the issue I run the following code:
#include <iostream>
int main()
{
if (__cplusplus == 201703L) std::cout << "C++17\n";
return 0;
}
Output is always the same: C++17
I've set "cppStandard": "c++23", in c_cpp_proporties.json.
I've set C++ standard in C/C++ Configurations settings to c++23.
I've set compiler arguments to -std=c++23.
I've been resetting VS Code, creating new files, reinstalling extensions, nothing.
Snippet from my tasks.json:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
Installed extensions:
C/C++ by Microsoft
C/C++ Extension Pack by Microsoft
C/C++ Themes by Microsoft
CMake Tools by Microsoft
Better C++ Syntax by Jeff Hykin
As many of the commentators pointed out, tasks.json is used for compiling. #Some programmer dude correctly explained that I should put argument inside it. Updating task.json with the following code is the solution:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-std=c++23",
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}

Switch Apple clang to Homebrew clang in VS code configuration

I have 2 versions of clang in my system. One installed using brew and another one it was required by XCode (which is set by default and referred as "Apple clang"). Previous answers, here or guides [1], [2], [3] weren't useful for me.
BTW brew noted clang/llvm as a "keg-only" package. I've uninstalled xcode-tools, but for example later when I used otool a xcode window popped requiring to install xcode-tools... Anyway, I have this setup right now:
/usr/bin/clang <--- This one installed by XCode tools
/usr/local/Cellar/llvm/<version>/bin/clang <---- This one installed by homebrew
Now I am in a situation where I want to compile with the clang version installed by homebrew using VS code, but I didn't found any config files to switch from xcode clang to "homebrew" clang.
These are my config files so far:
tasks.json (manually changed command entry)
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: clang build active file",
"command": "/usr/local/Cellar/llvm/13.0.1_1/bin/clang",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: /usr/local/Cellar/llvm/13.0.1_1/bin/clang"
}
]
}
c_cpp_properties.json
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**",
"/usr/local/Cellar/llvm/13.0.1_1/include/**",
],
"defines": [],
"macFrameworkPath": [],
"compilerPath": "/usr/local/bin/gcc-11",
"cStandard": "gnu17",
"cppStandard": "gnu++17",
"intelliSenseMode": "macos-gcc-x64"
}
],
"version": 4
}
A sample version.c file
#include <clang-c/Index.h>
#include <stdio.h>
void show_clang_version(void) {
CXString version = clang_getClangVersion();
printf("%s\n", clang_getCString(version));
clang_disposeString(version);
}
int main( int argc, const char *const * argv )
{
show_clang_version();
return 0;
}
This is the error I get
Starting build...
/usr/local/Cellar/llvm/13.0.1_1/bin/clang -fdiagnostics-color=always -g /Users/bel/libclang-test/vs-project/version.c -o /Users/bel/libclang-test/vs-project/version
/Users/bel/libclang-test/vs-project/version.c:1:10: fatal error: 'clang-c/Index.h' file not found
#include <clang-c/Index.h>
^~~~~~~~~~~~~~~~~
1 error generated.

setup Vscode for debbuging Eigen

I am using the Eigen library (https://eigen.tuxfamily.org/dox/GettingStarted.html) for some computations.
The program that I am running is:
#include <iostream>
#include <Eigen/Dense>
using Eigen::MatrixXd;
int main()
{
MatrixXd m(2,2);
m(0,0) = 3;
m(1,0) = 2.5;
m(0,1) = -1;
m(1,1) = m(1,0) + m(0,1);
std::cout << m << std::endl;
}
In their documentation by recommend compiling with:
g++ -I /path/to/eigen/ my_program.cpp -o my_program
If I run this, it compiles and runs... But now I would like to able to debug through the code.
The .json files generated I am using are:
tasks.json
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-I",
"${eigen}",
"${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
{
// 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++ - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++ build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
and
c_cpp_properties.json
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/home/pc/eigen/"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c11",
"cppStandard": "c++11",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
This configuration just runs the code but does not stick the debugger to it.
Would really appreciate the help!
Ok, so this worked for me:
1º Download Eigen:
https://eigen.tuxfamily.org/index.php?title=Main_Page
2º Create a symbolic link to the eigen folder
sudo ln -s location/of/Eigen/Folder /usr/local/include/
3º Can compile code with:
g++ my_program.cpp -o my_program
4º To "better debug" Eigen use the custom python printer for gdb
https://gitlab.com/libeigen/eigen/-/blob/master/debug/gdb/printers.py
a) Create a hidden file called gdbinit
touch ~/.gdbinit
b) Place the following code in it:
python
import sys
sys.path.insert(0, '/path/to/eigen/printer/directory') #In my case it was /home/pc/eigen/debug/gdb
from printers import register_eigen_printers
register_eigen_printers (None)
end
Note: VScode worked straight out of the box (F5 and debug mode was on!)

Configuring launch.json, task.json and settings.json for debugging in VS Code with git bash as default terminal?

I am trying to configure the Debugger in VSCode
I looked through official documentation to setup the VSCode debugger for C/C++ but it's not working.
Documentation states the steps for setting up vscode debugger for
powershell in windows.
But I am trying to set-up debugger with git bash as my default integrated terminal in windows.
I have added git bash as default terminal but I am not able to setup debugger with git bash as integrated terminal.
Default configuration files :
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe build active file"
}
]
}
The property :
"externalConsole": false
is set to false as I want VSCode to use the integrated default bash terminal instead of using external terminal for debugging.
tasks.json :
{
"tasks": [
{
"type": "shell",
"label": "C/C++: g++.exe build active file",
"command": "C:\\MinGW\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
],
"version": "2.0.0"
}
c_cpp_properties.json
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "C:\\MinGW\\bin\\gcc.exe",
"cStandard": "c11",
"cppStandard": "gnu++14",
"intelliSenseMode": "clang-x86"
}
],
"version": 4
}
settings.json
{
"files.associations": {
"iostream": "cpp"
},
"C_Cpp.errorSquiggles": "Disabled"
}
With the above configuration, when I start the debugging it gives me the following error:
It seems like the command property in tasks.json is incorrect,
as bash converts
"C:\MinGW\bin\g++.exe" -> "C:MinGWbing++.exe"
and gives error: "no command found" because back-slash('\') in bash is an escape character.
Now changing above path in tasks.json to bash style :
"command": "C:/MinGW/bin/g++.exe"
resolves the above error, but now it gives same error for variable ${file} as this path-variable gets dynamically set to current open file .cpp.
I have been working on this debugging issue for last few days, but haven't found any workaround yet.
How can I change/update by configuration files to use git bash as default integrated terminal in VSCode for debugging.
It seems like the command property in tasks.json is incorrect, as bash converts
"C:\MinGW\bin\g++.exe" -> "C:MinGWbing++.exe"
Then try a cygwin-like path:
/c/MingW/bin/g++.exe
# or
/C/MingW/bin/g++.exe
Check if it is interpreted correctly by the git bash session then.

Input/output while debugging with GDB in WSL

I've started working with Visual Studio Code on Windows & I want to debug the following file
main.c (located at C:/Users/aibrakov/Projects/c directory):
#include <stdio.h>
int main(void) {
int x = 42; // add breakpoint here
printf("Hello World!\n");
getchar();
return 0;
}
Following this threads
C\C++ in VS Code with Linux Subsystem For Windows
Visual Studio Code and Bash on Ubuntu on Windows (WSL) GCC/GDB integration
I have
settings.json:
{
"window.zoomLevel": 0,
"terminal.integrated.shell.windows": "c:\\windows\\sysnative\\bash.exe"
}
tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"label": "Build project",
"type": "shell",
"command": "gcc",
"args": [
"-g", "main.c"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "C++ Launch",
"type": "cppdbg",
"request": "launch",
"program": "/mnt/c/Users/aibrakov/Projects/c/a.out",
"stopAtEntry": false,
"cwd": "/mnt/c/Users/aibrakov/Projects/c",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"pipeTransport": {
"pipeCwd": "",
"pipeProgram": "c:\\windows\\sysnative\\bash.exe",
"pipeArgs": [
"-c"
],
"debuggerPath": "/usr/bin/gdb"
},
"sourceFileMap": {
"/mnt/c": "c:\\"
}
}
]
}
Building project with Ctrl + Shift + B succeeds.
Debugging kinda works: it stops on breakpoints and DEBUG CONSOLE tab is accessible, but I don't get output (Hello World! in my example) and also can't send character which getchar awaits.
Is there a way to work with the terminal in which my program is running? Or am I missing something?