How to set C++ standard in gcc compiler on Ubuntu VS Code - visual-studio-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"
}

Related

vscode report that the active file is not a C or C++ source file while building a c file

The vscode report an error while building a c program. The error message is as below.
Cannot build and debug because the active file is not a C or C++ source file.
The terminal process failed to launch (exit code: -1).
The task config file is as below.
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/gcc",
"args": [
"-fdiagnostics-color=always",
"-g",
"/home/xxx/tmp/test/main.c"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
I have a c file named main.c under the folder /home/xxx/tmp/test which is the workspace folder. What might be the cause of the problem?
As seen in this reply from Sean McManus, you need to get rid of all ${file} references in your tasks.json, for example:
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
}
change to
"args": [
"-g",
"test.cpp",
"-o",
"libs/test.o"
],
"options": {
"cwd": "${fileDirname}"
}
And change type of build from cppBuild to shell.

vscode cpp build tasks.json type:"cppbuild" error

VSCode : 1.61.0
Linux: Ubuntu 20.04.3 LTS
I'm trying to build c++ program using by VSCode.
when the run build, VSCode showing this message.
I know build tasking need matching launch.json file's preLaunchTask with tasks.json file's label. and I'm already set it.
tasks.json
{
"tasks": [
{
"type": "cppbuild", // but "shell" is working
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"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
{
// 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"
}
]
}
The problem is, if I change the "type" value in tasks.json, from shell to cppbuild, It doesn't work and show the error message like to.
showing error message when the run build
what is different "cppbuild" and "shell"? and What should I do how using "cppbuild" value?
Te values of cppbuild are fine. Try changing in launch.json the value:
"preLaunchTask": "C/C++: g++ build active file",
to:
"preLaunchTask": "cppbuild",
The difference between shell and cppbuild is that the first open the BASH and runs a command you write, the second, cppbuild, runs an specific binary (or program).

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!)

Run instead of debugging in Visual Studio Code

I would like to run my simple program in Visual Studio Code without debugging but all programs run in debugging mode.
I used CTRL + F5 (Run without debugging) but automatically go to debugging mode.
Could you please help me?
Tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\MinGW\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:\\MinGW\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: C:\\MinGW\\bin\\g++.exe"
}
]
}

The errors do not show on VS 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.