Sunday, 9 April 2017

Building and debugging C++ code in VSCode on Ubuntu

We have "Hello, world!" example in main.cpp file and want to build it with g++ compiler and debug it with gdb debugger in VSCode on Ubuntu.

Packages


C/C++ for Visual Studio Code (ms-vscode.cpptools)
C++ Intellisense (austin.code-gnu-global) (optional)

Building


We have to create and set up a task.

Open Command Palette (CTRL+SHIFT+P).
Type in task and select Tasks: Configure Task Runner and then Others.
tasks.json gets created in workspace's .vscode directory and shows up in the editor.

VSCode defines variables that can be used in tasks.json.

We can verify what will each of them expand into if we simply change args value for echo command in the default version of the config file.

tasks.json:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "0.1.0",
"command": "echo",
"isShellCommand": true,
"args": ["${file}"],
"showOutput": "always"
}

If we press combination CTRL+SHIFT+B, the output will be something like:
/home/user/dev/cpp/my_project/.vscode/tasks.json

Similarly, ${workspaceRoot} expands to the path to the workspace's root directory so we just have to append the name of the .cpp file to get its full path:

tasks.json:
...
"args": ["${workspaceRoot}/main.cpp"],
...

Running the task now gives the following output:
/home/user/dev/cpp/my_project/main.cpp

If we change command to g++...

tasks.json:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "0.1.0",
"command": "g++",
"isShellCommand": true,
"args": ["${workspaceRoot}/main.cpp"],
"showOutput": "always"
}

...after hitting CTRL+SHIFT+B g++ compiler compiles main.cpp and a.out appears in the my_project directory.

Running


If we hit F5, this launches the program.

launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "C++ Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceRoot}/a.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"environment": [],
"externalConsole": true,
"linux": {
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
},
"osx": {
"MIMode": "lldb"
},
"windows": {
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
},
{
"name": "C++ Attach",
"type": "cppdbg",
"request": "attach",
"program": "enter program name, for example ${workspaceRoot}/a.out",
"processId": "${command:pickProcess}",
"linux": {
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
},
"osx": {
"MIMode": "lldb"
},
"windows": {
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
}
]
}

Debugging


In order to enable adding breakpoints we have to enable creation of debug information when building the source code. It is enough if we add -g to g++ arguments:

tasks.json:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "0.1.0",
"command": "g++",
"isShellCommand": true,
"args": ["-g", "${workspaceRoot}/main.cpp"],
"showOutput": "always"
}

1 comment:

micheal pan said...

BE SMART AND BECOME RICH IN LESS THAN 3DAYS....It all depends on how fast 
you can be to get the new PROGRAMMED blank ATM card that is capable of
hacking into any ATM machine,anywhere in the world. I got to know about 
this BLANK ATM CARD when I was searching for job online about a month 
ago..It has really changed my life for good and now I can say I'm rich and 
I can never be poor again. The least money I get in a day with it is about 
$50,000.(fifty thousand USD) Every now and then I keeping pumping money 
into my account. Though is illegal,there is no risk of being caught 
,because it has been programmed in such a way that it is not traceable,it 
also has a technique that makes it impossible for the CCTVs to detect 
you..For details on how to get yours today, email the hackers on : (
atmmachinehackers1@gmail.com ). Tell your 
loved once too, and start to live large. That's the simple testimony of how 
my life changed for good...Love you all ...the email address again is ;
atmmachinehackers1@gmail.com