MRC/Tutorials/Vscode
To make the use of VScode more comfortable for you we've created a script, which if you run it not only installs VScode, but also installs a few of its many extensions. These extensions extend the functionality of VScode. There are extensions to make it understand CMake, Git and C++.
To install VScode:
wget -O install.bash https://raw.githubusercontent.com/KdVos/MRC_vscode_install/main/vscode_install.bash source ./install.bash
Setting up VScode for your project
Now you have installed a proper IDE, you can start to do some real programming! In previous tutorials, we created a C++ project called my_project and went through a little bit of work to get it to build using CMake. Now, that work will pay off: VScode 'understands' CMake, so we can directly load the project.
In a terminal go to your project. Within the root directory type the following:
(the root directory is the directory containing CMakeLists.txt)
code .
This will open vscode in the Current directory. It will ask you whether you trust the authors of this directory, since we are the authors we will trust ourselves. Afterwards, when you are prompted to "select a kit" select the [unspecified] option. In the sidebar the current directory and the files contained in it are shown. Click on your CMakeLists.txt to open this file.
One of the most powerfull features of vscode is the command palette. Almost all functionality of your IDE and its extensions are available through this window. To open it type: `ctrl+shift+p`. (sometimes this doesn't work directly, you can try restarting vscode in this case)
To tell vscode that you like to use your cmake file, open the command palette and type `Cmake: Configure:` and hit enter. VScode will now remember that you are using CMake to build your program.
If we now open our .cpp file, we can try to compile our program within vscode. Again open up your command palette, but this time type `Cmake: Build`. In the terminal in the lower part of your screen you will see that our project is succesfully built.
To run your program type `Cmake: Run without debugging` and our program will execute.
Since typing our commands in the command palette can get boring after some time, key bindings were created to make our lives easier. If we again open the command palette and type `Cmake:Build` we see that we could have also used the F7 key to build our project. If we close the command palette again and use the F7 key we see that this is indeed the case.
Using VScode
From this point on you don't really have to leave VScode any more. You can edit the source file in VScode, edit the CMakeLists.txt, run CMake and even run your program. That's pretty awesome!
But you ain't seen nothing yet. Double click on example.cpp to edit it. Remove the #include statement at the top, and type it again, but slowly. You will see that as soon as you start typing #include, VScode pops up a window with some suggestions. The more you type, the more specific it becomes. You can select an option using the arrow keys, and press enter or tab to confirm. This is called auto-completion and will save you a lot of typing. Continue typing. When you get to *<em*, you will see that VScode even understands the location of header files, and auto-completes them for you.
Now lets make a mistake on purpose: misspell return and use F7 to compile the project. You will see a list of issues pop up on the bottom. You can double click on issues to directly jump to the error, which is especially useful if your project gets bigger.
Debugging your project
As you might know, the life of a programmer and software developer sometimes largerly consists of debugging our own code. Ofcourse, VScode gives you the tools to do this.
You can add a breakpoint to your file by clicking the space before a line number.
You can run your program till an error occurs. Or you can pause execution during a run.
First, make sure you have built your project in debug mode using "Cmake: Select Variant". Afterwards, you can start debugging by typing "CMake: Debug" in your command palette.
Note, however, that your code performs better when you change your build variant back to Release mode.
Making your life easier
So far we've only used the file explorer sidebar. But if you look closely at the left side of your vscode window you'll discover a range of other functionalities. This tutorial will highlight a few of them, but is definitley not exhaustive.
GIT
In a later tutorial we will teach you how to use GIT. A version control system which enables easy collaboration on your code. VScode offers a built-in Graphical User Interface (GUI) which is accessed through the sidebar. Just click the third icon, which looks like a crossroads.
TODO-tree
Since software development can sometimes become a bit of a complex operation, in which a large amount of things need to be implemented across multiple files and functions. We've added an extension so you can easily keep track of your todos, bugs and thing to be fixed. To access it just click the tree icon, it should be the second to last icon in your sidebar.
If we go back to our project files, and add a comment saying:
`// #TODO: this needs to be fixed ASAP`
You will see an item pop up in your todo-tree. This way it is easier to keep track of your todos, and you can make sure you don't forget about them.
Concluding Remarks
As said before, Vscode offers you a library of hunderds of extensions, not all are useful to us, but some can improve your software developement experience significantly. This tutorial is, therefore, all but exhaustive. There are probably functionalities and extensions we missed, but experimenting a bit can probably lead to an even better experience.