Debugging c++ built with a makefile in Visual Studio Code - c++

I have an existing code base that builds with a makefile and I'd like to use Visual Studio Code to run it.
I started by opening my project directory with Visual Studio Code.
Then, for building I created a task as per http://code.visualstudio.com/docs/languages/cpp :
{
"version": "0.1.0",
"command": "bash",
"isShellCommand": true,
"args":["-c", "cd build && make"]
}
which works fine. I then downloaded the C++ extension and created a launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "C++ Launch",
"type": "cppdbg",
"request": "launch",
"targetArchitecture": "x64",
"program": "${workspaceRoot}/build/myProgram",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"environment": [],
"externalConsole": true,
"linux": {
"MIMode": "gdb"
},
"osx": {
"MIMode": "lldb"
},
"windows": {
"MIMode": "gdb"
}
},
// attach here, which I don't care about
]
}
This launch file allows me to start my program using the built-in debugger. I know that it works because I see all the symbol files getting loaded and I can pause the program.
However, VS Code doesn't "see" my source files; when I pause it says
Source /Unknown Source is not available.
I also can't set breakpoints in the source. I see that some symbols get loaded though as I can see my function names on the call stack when I pause execution.
So what did I miss? Do I absolutely have to do as they say here ( http://code.visualstudio.com/docs/languages/cpp ) and make a g++ task where I'll have to manually input all my include paths and linker inputs? The point of using a makefile in the first place was to not have to do this tedious stuff...
Thanks a lot to anyone who knows how to wrangle VS code.

You should be able to debug by just using tasks.json and launch.json.
Are you sure you have the -g option set in your makefile?
Another thing - you can have your task be executed on launch by adding
"preLaunchTask": "bash" to your your launch.json.
Hope this helps!

At windows; Mingw, gdb and -g parameter at compilation of the executable are necessary with c/c++ extension. "preLaunchTask": "bash" is not necessary in launch.json.

Related

Unable to start debugging. Not implemented. Error on VS code

Hi i recently started using VS code for my C++ course. I am trying to get the debugger to work but even with hours of trying to figure out what's wrong i still get the following error "Unable to start debugging. Not implemented." I am a bit unsure on what that means let alone how to solve it. My launch.json file looks like this. Thank you in advance for the help.
{
// 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": "(Windows) Launch",
"type": "cppvsdbg",
"request": "launch",
"program": "Assigment_1.C++",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"console": "externalTerminal"
}
]
}
I don't have enough Karma to post in the comment thread but I wanted to say that I don't think it matters that you're using vs code instead of visual studio. It says in the debug console that the C/C++ extionsion is compatible with both, and this is the .json that is automatically generated by vs code. I'm having the same issue trying to run C. I've reached out to microsoft support. Let me know if you've figured it out, thanks.
Alternatively, it seems to work if you just download the code runner extension and then right click and choose run code instead. At least it did for me using C. Still trying to figure out how to use it with the debugger however.
I had the same problem and I have solved this problem using these steps.
First create your C++ folder (mine is cpp test) and create the main.cpp file.
Then after making the main method just click f5 and the vs code will pop up a box asking whether C++(GDB) or C++(Windows).
refer the photo
Now click on C++(GDB/LLDB).
Now click on g++.exe (Build and Debug active file).
Now the vs code will generate the lanch.json file.
Then go to the main.cpp file, Just click f5 and it will run without any problem. It worked for me.
(Please make sure that you have installed cygwin64 on your computer, mingw is also possible)
The json file below worked for me
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "D:/mingw/bin/gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]

How to configure Visual Studio Code C++ compilation for custom Makefile?

I have a very simple c++ project with my own Makefile that has such commands "make a.out", "make depend" and a compiling command "make submit", plus a "make clean" which deletes the a.out and any cores in the directory.
I want to configure my VSCode so that when I press run, I would see the result of "make clean" "make a.out" followed by "./a.out". I have already installed the C/C++ extension and had my gcc installed.
These are very simple command-line commands but I just can't make the VScode work. The error says a.out is not found. In my launch.json, I have below scripts
{
// 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": "(lldb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "a.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb"
}
]
}
What should I change the launch.json and how should I configure my task.json? Is there anything else I need to do? Thanks!

How to configure Visual Studio Code to debug with cygwin, cmake and gcc

I have to use cygwin and cmake for the build of the release and debug.
(The cygwin package has all needed tools/libs for the build process (cmake, gcc, boost))
I've already found this
Which now allows me to run cmake and the build scripts inside the console of visual studio code and the executable is generated properly.
And I've already installed the extension C/C++ for Visual Studio Code.
But how to configure visual studio code for debugging?
After building via this, all you need for debugging is a launch.json of the following structure:
{
// 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": "(gdb) Starten",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/PATH_2_YOUR_DEBUG.exe",//This you need to define
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\cygwinPath\\bin\\gdb.exe",//This you need to define
"setupCommands": [
{
"description": "Automatische Strukturierung und Einrückung für \"gdb\" aktivieren",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
Afterwards you can set a breakpoint in the source of PATH_2_YOUR_DEBUG.exe and press F5.

How to use the debugger in visual studio code?

I am using Visual Studio Code for C++ coding on Ubuntu 18.04.
I found that for the debugging, I should always build an a.out file and then use the debugger. If I run the code directly by ctrl+alt+n then the code runs completely but it won't give me an a.out file.
Is there any way that I can automate this process?
I have also noticed that there is no build option available.
Here is the launch.json file:
{
// 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": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "g++ build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
You could create custom build+launch task for running your C++ code through a debugger.
Let's say we have this sample code:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
cout << "Hello World!" << endl;
vector<int> numbers{ 10, 20, 30 };
for (int n : numbers)
{
cout << n << endl;
}
}
First, make sure to have Microsoft's C++ extension installed.
Next, create a custom build task.
This tells VS Code how to compile your code.
Open the Command Palette
Select Tasks: Configure Task
Select C/C++: g++: build active file
You can other compilers that are available on your system
That would create or open an existing .vscode/tasks.json file
Configure it
{
"type": "shell",
"label": "build-my-app",
"command": "/usr/bin/g++",
"args": [
"-g",
"-std=c++11",
"${workspaceFolder}/path/to/test.cpp",
"-o",
"${workspaceFolder}/path/to/test.out"
],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": [
"$gcc"
],
"group": "build"
}
Set label to uniquely name your custom task
Set the compile flags like -g, -std=xxx, -l<lib>, -I<header>
Set the output filename instead of a.out
Next, create a custom launch task.
This tells VS Code how to run your code through the debugger.
Open the Command Palette
Select Debug: Open launch.json
That would create or open an existing .vscode/launch.json file
Configure it
{
"name": "run-my-app",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/path/to/test.out",
"preLaunchTask": "build-my-app",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb"
}
Set name to uniquely name your launch task
Set preLaunchTask to the build task label you created earlier
Set program to the output filename you set in the build task
Finally, whenever you want to run your app, just open the Debug/Run panel and select your launch task from the dropdown (find the name you set in launch.json). Set your breakpoints then click the Play button.
You'll get the debugger controls for stepping through your code, the variables and call stack information, and the console output in the Debug Console.
Maybe you should try to compile using the the flag -g
Something like
g++ -g file.cpp
This command should generate an excutable and a directory that contains information that the debugger can read.
I am no sure if this is appropiate for VS debugger since I use gdb (gnu debugger) but maybe will work

Visual Studio Code - Remote Debugging Bazel C++ - Unable to read file 'vscode-remote://dev- file

I'm trying to do remote debugging of bazel project in docker.
Local plugins: Remote Development
Docker plugins (defined in devcontainer.json): ms-vscode.cpptools
My launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug",
"type": "cppdbg",
"MIMode": "gdb",
"request": "launch",
"args": [],
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"program": "/root/.cache/bazel/_bazel_root/e53bbb0b0da4e26d24b415310219b953/execroot/tf_serving/bazel-out/k8-dbg/bin/tensorflow_serving/fplugin/fserve_cc",
"externalConsole": false,
}
]
}
However I'm getting an error:
Unable to open 'fserve.cc': Unable to read file 'vscode-remote://dev-container+2f55736572732f6d67756d6f77736b2f776f726b2f6f766d732d63/proc/self/cwd/tensorflow_serving/fplugin/fserve.cc
In local development I would add sourceFileMap, however it doesn't seem to work for remote docker development. Any ideas?
when you try to compile with bazel, you will have 4 soft link(bazel-bin, bazel-{source-folder}, bazel-out, bazel-testlog)
modify this attribute wi
"cwd": "${workspaceFolder}/bazel-",
I'm not sure that your problem is due to remote debugging. I believe that the following info will work whether or not you're in a remote environment.
Bazel generates a few symlinks in your project's root folder that contain useful data (for more see the docs):
bazel-bin is a symlink to a folder in the output directory that contains generated code and binaries for the latest configuration of the build you've run.
bazel-{folder-name} is a symlink to the execRoot, which is how bazel sees your project's source code. {folder-name} is the name of the folder your project is in.
bazel-out is a symlink to bazel's output root.
bazel-testlog is a symlink to the outputs from test runs
If you're not using bazel's runfiles library
To provide VSCode with sources at the right location, modify the "cwd" attribute in launch.json to:
"cwd": "${workspaceFolder}/bazel-{folder-name}",`
If you are using runfiles
You're using runfiles if your program requires files in a data attribute in any of its dependencies
First, set "cwd" to the root of the runfiles tree for your binary, and then set "sourceFileMap" in a way that maps the current working directory to the execRoot symlink. For me (on linux), /proc/self/cwd maps correctly, but that might not be true for you. I was able to tell that I got it wrong because VSCode complained about being unable to read a file at /proc/self/cwd/external/external-project/src/file.cpp.
"cwd": "${workspaceFolder}/bazel-bin/path/to/executable/executable.runfiles/{workspace-name}`,
"sourceFileMap": {
"/proc/self/cwd": "${workspaceFolder}/bazel-{folder-name}`,
},
{workspace-name} can be found in WORKSPACE, it will look like workspace(name = "my-project"). If there is no workspace name defined in that file, it defaults to __main__.
I might also recommend changing the "program" attribute to a more convenient location. If the last thing you did was build your binary with bazel, you can find the built executable at bazel-bin/path/to/executable/executable
A full example:
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch Debug",
"type": "cppdbg",
"request": "launch",
// path to the most recently built version of executable
// this should be built with `bazel build -c dbg //path/to:executable`
"program": "${workspaceFolder}/bazel-bin/path/to/executable/executable",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}/bazel-bin/path/to/executable/executable.runfiles/{workspace-name}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"sourceFileMap": {
"/proc/self/cwd": "${workspaceFolder}/bazel-{folder-name}"
}
}
]
}
Most of this is covered in this useful post (which also shows how to automatically build your executable using tasks.json):
https://shanee.io/blog/2019/05/28/bazel-with-visual-studio-code/