Running C++ Programs at the Command Line¶
First, make sure you have g++ (the compiler), make (the build tool), and valgrind (the memory leak checker) installed using this command:
$ sudo apt-get install g++ make valgrind
You don’t need to run this on the lab computers: those programs should already be installed on the lab computers.
Second, save a copy of the files makefile (make sure to save this with the
name makefile
) and cmpt_error.h in the folder on your computer where you
will be writing your program.
Third, using your favourite text editor (see below for some suggestions), save
the following in a file named hello.cpp
:
// hello.cpp
#include "cmpt_error.h"
#include <iostream>
int main() {
std::cout << "Hello, CMPT 125!\n";
} // main
We will compile (or build) our programs using make
, a standard Linux build
tool. To compile your program ($
is the command-line prompt) type this:
$ make hello
g++ -std=c++14 -Wall -Wextra -Werror -Wfatal-errors -Wno-sign-compare -Wnon-virtual-dtor -g hello.cpp -o hello
Notice that this calls g++
with many options. These options come from
makefile, so make sure it’s in the same folder as hello.cpp
.
Assuming there are no errors, run the resulting program like this:
$ ./hello
Notice you must type ./
first.
To run a program with valgrind, type this:
$ valgrind ./hello
You will see a big log message when the program ends; it will usually tell you if there are any memory errors.
If you want to time how long it takes a program to run, you can use the
time
command like this:
$ time ./hello
Programming Text Editors and IDEs¶
You should use a good programming editor. Some popular, and relatively easy to user editors, are:
- Sublime Text 3. This is the editor I usually use in class. While it is not technically free, it has an unlimited evaluation period.
- Atom is a free editor inspired by Sublime Text 3.
- Visual Studio Code is a free editor. again similar in style to Sublime Text 3.
There are many other editors (e.g. emacs, vi, BlueFish) and IDEs (e.g. CodeBlocks) available.