Loading...

STORIES

Bust Out a C++ Setup

Here's a quick and easy way to set up a C++ development environment on Debian GNU/Linux. Download and install the latest version, version 10 or Debian Buster, with the GNOME desktop environment (and be sure to download the unofficial Debian Buster version, with GNOME, and with non-free hardware support if your machine requires it), install it, and follow along.

Once you've got the system installed (and remember to add yourself as a super user), download the Debian package for Visual Studio Code and install it.

The GNU Compiler Collection should already be installed. Verify this by checking the version by running the following in the GNOME Terminal:

gcc --version

Install the GNU debugger:

sudo apt install gdb

Create a project folder by running the following:

mkdir ~/Projects
mkdir ~/Projects/hello
cd ~/Projects/hello

Start up Visual Studio Code.

You can opt out of sending usage data to Microsoft by selecting File, then Preferences and then Settings from the menu, and then searching for telemetry.

Open a new file by selecting File and then New File from the menu, and save it as hello.cpp in the project folder by selecting File and then Save from the menu, and add the following simple program code:

//
// Program to greet a user
//
#include <iostream>
#include <string>

using namespace std;

int main()
{
  string name;
  std::cout << "Enter your name: " << std::flush;
  std::cin >> name;
  std::cout << "Hello " << std::flush;
  std::cout << name << endl;
  return 0;
}

Download the Microsoft C/C++ extension for Visual Studio Code and install it, by selecting File, then Preferences and then Extensions from the main menu, and then selecting the dot-dot-dot icon from the sidebar and selecting Install from VISX. This extension adds intelligent code completion and debugging for C++ to Visual Studio Code:

To compile the source file, select Terminal from the menu and then Configure Default Build Task, and then select a compiler. This will generate a tasks.json file. Then select Terminal from the menu and then Run Build Task to generate an executable file that will be called hello.

To debug the source code, select Run and then Add Configuration from the menu, and then select cpp - build and debug active file. Add a breakpoint to a line in the code by clicking next to the line number. Select Run and then Start Debugging from the menu, to see the program execute and then halt at the breakpoint.

Continue running the program, input a name and see the greeting.

That's it. You've got a working C++ development environment set up.