- Download CLANG Compiler
- Download VScode
- After Downloading VScode, install the foollowing VSCode extensions:
-
- C/C++ IntelliSinse: It enables debugging features such as breakpoints
- Craete a folder called
.vscode
in your working directory. - The C/C++ IntelliSense Debugger settings can be set based on the
c_cpp_propoerties.json
andlaunch.json
files which the developer must create in the ".vscode" folder. The files content are explained below.
This file contains the compiler/autocomplete specific information for C/C++ files and it is used by the IntelliSense (the C/C++ extension). The structure of the file and its content can be found at VScode documentation. However, a simple example is given below for the linux environment.
"configurations": [
{
"name": "Linux", // Operating System
"includePath": [ // Paths used for AutoCompletion (where the code is actually written)
"${workspaceFolder}/src/**"
],
"defines": [ // Define macros (this is defined even before the macros in the code)
"MOHIDO_MOHIDO"
],
"compilerPath": "/usr/bin/clang", // Where to find the compiler
"cStandard": "c17", // C 17 support
"cppStandard": "c++17", // C++ 17 support
"intelliSenseMode": "clang-x64" // What is the compiler type
}
This file contains the necessary information for the debugger. For instance, where to find the program to debug and the type of the debugger. To read more about the file, please visit the launch.json page. However, again, an example is provided below:
"configurations": [
{
"name": "(Windows) Launch", // The name of the debugger (how you want it to be viewed in VSCode
"type": "cppvsdbg", // Debugger type (cppvsdbg, cppdbg ... etc)
"request": "launch", // must be "launch" or "attach".
"program": "${workspaceFolder}/apptodebug.exe", // Where to find the desired application
"args": [], // Arguments to pass to the application
"stopAtEntry": false, // Breaks when the program starts
"cwd": "${workspaceFolder}/bin/", // Where to find the debug files (output by the compiler)
"environment": [], // Any specific environment variables for the run of the application
"console":"newExternalWindow", // Only for windows (create a new external console window)
}
There are two ways to compile the project and build the application files (.exe, .so, .dll ...etc):
- To us the terminal and compile every time using the clang command and link the necessary libraries, code files and flags.
Then run the application from VScode by either pressing "F5" or through the menu (
Run -> Start Debugging
). - We can automate the process by creating a
build.sh
orbuild.bat
files and a VSCode task to run them from the VSCode editor instead of typing the steps manually mentioned previously in step (1
).
VScode editor can run bash scripts by the press of a button. The process can be done through the menu (Terminal -> Run Task...
).
However, currently, there is no vscode task for running our custom .sh
or .bat
files. Thus, the need to define that task emerges.
To define a VScode task, create a file in the .vscode
folder called tasks.json
. An example of the content for shell scripts are given below:
{
"tasks": {
"type": "shell", // Task type (shell)
"label": "The Cool Task Name", // Task name (what do you want it to be called)
"windows": { // Where to find the shell file on windows
"command": "${workspaceFolder}/buildscripts/build.bat"
},
"linux": { // Where to find the shell file on Linux
"command": "${workspaceFolder}/buildscripts/build.sh"
},
"options": {
"cwd": "${workspaceFolder}/buildscripts" // Where are the scripts
},
"problemMatcher": [
"$gcc"
],
"group": "build", // So the developer can run it with "ctrl+shift+b"
"detail": "Task generated by Debugger." // Additional description for other developers to understand the task
}
}
}
Now, the setting up is complete and the developers can build then run their debugger through vscode on multiple platforms.
- Click on the menu:
Terminal -> Run Task...
then choose the task you want to perform. This is only done if you havetasks.json
file in the.vscode
folder. - Click on the menu:
Run -> Start Debugging
or pressF5
to start the application on debugging mode.
- In the configuration, the developer can add predifined macros. However, bear in mind, when using the
_DEBUG
macro, some C++ libraries, such asiostream
, will not work. The simplest solution is to remove that macro since it will not impact the debugger nor intelliSense in any means. An example of shown linking errors:
libcpmtd.lib(xlocale.obj) : error LNK2001: unresolved external ...