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

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!

Related

Use integrated terminal for debugging C++

I would like to use the integrated terminal for debugging C++ files in VSCode on my Mac. I have the C/C++ extension added and enabled globally.
Here is the launch.json that I am using:
{
// 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": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb",
"preLaunchTask": "clang++ build active file"
}
]
}
I have tried adding "console": "integratedTerminal" to this with no change. When debugging a program, the terminal output says:
> Executing task: /usr/bin/clang++ -fdiagnostics-color=always -std=c++17 -g /Users/kris/learncpp/chapter_03/debugTest4.cpp -o /Users/kris/learncpp/chapter_03/debugTest4 <
Terminal will be reused by tasks, press any key to close it.
Hitting any key here closes the terminal and drops me into a bash shell. Is there another setting I am missing somewhere?

How to set gdb as debugger for the C/C++ extension pf VSCode on MacOS?

the C/C++ Extension of Microsoft for VSCode allows to set a launch.json file with which you can set how to debug and run your C++ code. By default it has lldb as debugger for MacOS.
I wonder how to set gdb as debugger instead of lldb.
I tried and it shows me:
Unable to start debugging. GDB exited unexpectedly with exit code 134 (0x86).
This is how my launch.json file looks like:
{
"version": "0.2.0",
"configurations": [
{
"name": "g++-9 - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"osx": {
"miDebuggerPath": "/usr/local/bin/gdb",
"MIMode": "gdb"
},
"preLaunchTask": "C/C++: g++-9 build active file"
}
]
}
Make sure you have gdb installed.
For that you can use:
brew install gdb
Then make the gdb binary is certified. Because:
... modern Darwin kernels restrict the capability to assume
control over another process (here, for the purpose of debugging it),
since that capability is a boon to malware. In order for said
taskgated to grant access to gdb, the latter must be fitted with
entitlements, which consist of digitally-signed metadata inside the
gdb binary.
From: https://sourceware.org/gdb/wiki/PermissionsDarwin
To create a certification follow these instructions:
https://sourceware.org/gdb/wiki/PermissionsDarwin
After doing so, certify the binary as instructed here.
Then try this for 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",
"preLaunchTask": "C/C++: g++ build active file"
}
]
}

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 configure Visual Studio Code for C++ on Windows?

I face problem with configuring launch.json file on my Windows and it shows error message
Debug adapter process has terminated unexpectedly.
I have set up "MinGW" and configured g++ compiler, and now Visual Studio Code compiles correctly. When I press Ctrl+Shift+B, it creates a.exe file in project folder.
My launch.json file:
{
"version": "0.2.0",
"configurations": [
{
"name": "C++ Launch (Windows)",
"type": "cppvsdbg",
"request": "launch",
"program": "${workspaceRoot}/a.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"environment": [],
"externalConsole": false
},
{
"name": "C++ Attach (Windows)",
"type": "cppvsdbg",
"request": "attach",
"processId": "${command.pickProcess}"
}
]
}
My task.json file:
{
"version": "0.1.0",
"command": "g++",
"isShellCommand": true,
"showOutput": "always",
"args": ["-std=c++11","-g", "main.cpp"]
}
It may be wrong, but I suppose you need to compile your .c/.cpp file with microsoft cl.exe compiler (not by gcc) to use microsoft debugger on it. To setup paths for cl, call the vcvarsall.bat with parameter x86 or x64 (depends on what you need) and then cast cl to your source file. By the way check this article (the part with json configuration exactly) to ensure, where you did wrong https://www.40tude.fr/blog/how-to-compile-cpp-code-with-vscode-cl/

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

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.