Everything you need to get started for the course "Programming in C++ for Biologists".
Visual Studio Code (VSC) works by opening a folder and operating on the files inside that folder.
In such folder, VSC requires a C++ file, typically called main.cpp
, and a CMakeLists.txt
file, which indicates how the C++ file needs to be compiled.
The easiest way to set up a correct folder is by downloading these files here:
Normally, if you followed all the instructions for Getting Started, you should already have these two files in a folder named cmake-project
.
In case you do not already have these files:
Alternatively, you can:
Every time you start a new programming project, you can repeat this and copy these files into a new folder.
If they haven’t been installed while Getting Started, you need to install the following extensions to make VSC work:
ms-vscode.cpptools
(C/C++)ms-vscode.cmake-tools
(CMake Tools)Install these by navigating to the left-hand side menu. Click on the Extensions symbol .
There, look for and install the extensions.
With our files downloaded and extensions installed, we can now use VSC to open our working folder.
Hello CMake
in the terminal panel.If the buttons are not available, this indicates that you are missing the CMake Tools extension. Please see the section above to install this.
An important tool in programming is to be able to break your program at a specific line, and execute the program step-by-step. Depending on the lecture track you choose, you might learn to do this during the course.
launch.json
file. Click on create launch.json file to do so. This will open a new launch.json
file in VSC.program
, to make sure that it points towards your executable. The location of that executable may vary depending on your operating system. The best way to find out is to press the ‘play’ button. This will show you the location of your executable: cmake-project
is our executable. On Windows, you can expect something like C:\vscode_example\build\Debug\cmake-project.exe).launch.json
file. You might encounter some errors while setting up the debugger. To fix them, follow the steps below.
If this error pops up in Visual Studio Code:
Unable to start debugging : The value of miDebuggerPath is invalid
You want to use gdb
as your debugger. Make sure you have it installed, and find its path with the command which -a gdb
in the terminal window. Copy the local path (probably something like /usr/bin/gdb
) into your launch.json
file, and update the relevant values like so:
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb"
If you get this error in Visual Studio Code’s debugging terminal:
warning: GDB: Failed to set controlling terminal: Operation not permitted
This is a common bug with gdb when it is not up-to-date. You will need to manually install the most updated stable version of gdb:
https://ftp.gnu.org/gnu/gdb/gdb-11.1.tar.xz
wget
and paste the link to the target file (example, wget https://ftp.gnu.org/gnu/gdb/gdb-11.1.tar.xz
). Enter.tar -xf
command (example, tar -xf gdb-11.1.tar.xz
).cd gdb-11.1/
../configure
.make
. If at this stage, you get the following error: configure: error: GMP is missing or unusable
, fix it as per the instructions below, then try again and resume to step 8.sudo cp gdb/gdb /usr/local/bin/gdb
.gdb --version
should show the correct version number (11.1
in this example).If you get this error while installing gdb with make
:
sudo apt install libgmp-dev
make
again. It should complete without error this time.A suggested way to organise your files involves splitting each chapter in a separate folder.
hello_world
folder in the projects/
folder.chapter1
.Open folder
step. This time, select your new folder.