Terminal Commands & Running C++ Programs
Basic Command-line Commands for the Terminal
In this course we'd like you to work from a command-line terminal window, sometimes called a shell window. It lets you type commands to run programs.
BASH is the most common default shell, and when you run a terminal you will see a prompt like this:
$
The $ indicates this is a shell prompt. Everything before the $ is information about what computer you are using and what directory you are in. Everything after the $ is what you, the user, types.
The shell includes a complete programming language and dozens of commands. Here is a brief summary of basic commands that are often used in the terminal:
pwd: prints the present working directoryls: lists the files and folders in the current directorycd a2: change to directory a2rm old.cpp: delete the file old.cppcp a1.cpp a1: copy file a1.cpp to the folder a1man g++: display the manual page for the g++ commandless a1.cpp: display contents of a file one page at a timecat a1.cpp: display contents of a file (unpaged)time ./a1: displays running time of program a1
You can find many tutorials and help pages on the web for learning about the Linux command-line. For example, this tutorial discusses many basic Linux commands, with helpful examples.
It is also common to manipulate files and folders interactively in the desktop graphical user interface (GUI). Just open the folder you want to change, and use the typical drag-and-drop actions to move, copy, rename, etc. files and folders.
Running C++ Programs in the Command Line Terminal
To create a C++ program in this course, you'll need a text editor (to edit the source code files), a compiler to make executable programs (g++), a build tool to call the compiler with all the right options (make), and a memory leak checker to help catch errors (valgrind). Make sure to install these tools.
Next, save a copy of the files from here into the same folder:
hello_world.cpp: a simple C++ program that outputs a message.makefile: which has the options for how we will call the compiler. Important: it must be named exactlymakefile, and must be in the same folder ashello_world.cpp.cmpt_error.h: helper files that contains the error function for stopping your program when an error is recognized.
To check that you're in the correct folder with the right files, use cat to print the contents of hello_world.cpp:
$ cat hello_world.cpp
// hello_world.cpp
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!\n";
return 0;
}
Try typing cat makefile and cat cmpt_error.h as well.
We compile (or build) our programs using make:
$ make hello g++ -std=c++17 -Wall -Wextra -Werror -Wfatal-errors -Wno-sign-compare -Wnon-virtual-dtor -g hello.cpp -o hello
Notice g++ is called with many options. These come from makefile, so make sure it's in the same folder as hello_world.cpp.
Assuming there are no compiling errors, run the resulting program like this:
$ ./hello_world
Notice you must type ./ first before the name.
To run a program with valgrind, type this:
$ valgrind ./hello_world
You will see a long message when the program ends. If there are any memory errors, they will be reported here (hello_world shouldn't have any errors).
If you want to time how long it takes a program to run, you can use the time command like this:
$ time ./hello_world Hello World! ________________________________________________________ Executed in 3.41 secs fish external usr time 1300.00 micros 104.00 micros 1196.00 micros sys time 142.00 micros 142.00 micros 0.00 micros
This example uses the Fish shell, and your output may be formatted differently if you are using a different shell.
File Redirection Using < and >
The ls command lists the files and folders in the current directory, e.g.:
$ ls a1.h a1_sol* a1_sol.cpp a1.txt cmpt_error.h makefile
The output of ls is printed to the screen, which is known as standard output. In C++, functions like printf and cout always print to standard output.
You can easily write the output of a command use the > re-direction operator:
$ ls > listing.txt $ cat listing.txt a1.h a1_sol* a1_sol.cpp a1.txt cmpt_error.h listing.txt makefile
The command ls > listing.txt re-directs the standard output of ls into the file listing.txt.
You can also re-direct standard input. That means you can, for example, use a text file of input as the input to a program that reads from standard input (i.e. the keyboard).
Suppose the program ./age asks the user for their name and age. You could run it like this:
$ ./age What's your name? Bob How old are you? 20 Hi Bob, 20 is a great age!
The program waits while the user types Bob (and then presses return), and also while the user types 21 (and then presses return).
Another way to run this program is to first create a file called, say, test_input.txt with this content:
Chris 21
Then you can use the < re-direction operator to have ./age get its input from test_input.txt:
$ ./age < sample_input.txt What's your name? How old are you? Hi Chris, 20 is a great age!
This can be very useful for testing - it saves a lot of typing!
More Information
The Linux command shell is complete programming environment with its own language. We will only use bits and pieces of it during this course. If you are curious learn more, here is a free book about using the Linux command shell. You can find many other resources online.