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!)
Related
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"
}
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 : 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).
Im new to stackoverflow and VS Code. When I tried to debug my code I got error message, and couldn't fix it.
I uploaded the error message
and;
here is mylaunch.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 - Etkin dosyayı derle ve dosyada hata ayıkla",
"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": "gdb için düzgün yazdırmayı etkinleştir",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe build active file"
}
]
}
and task.json:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\TDM-GCC-64\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:\\TDM-GCC-64\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Generated task by Debugger"
}
],
"version": "2.0.0"
}
my main.cpp file is simply printes "Hello"
I installed MinGW and its directory is : C:\MinGW<
I hope there is someone who can help me.
I encountered the same problem recently. It seems that the auto-generated tasks.json is not set correctly and the executable is created in the root directory. This can be fixed by changing the type from "cppbuild" to "shell".
{
"tasks": [
{
"type": "shell",
...
}
],
"version": "2.0.0"
}
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?