Command-Line Interface

Knowing how to use a command-line interface (CLI) can greatly enhance your productivity on a computer. Despite having limited graphics output and keyboard-only input, a single command can automate a task that may take days using a graphical user interface (GUI).

In Computer Science I, you already used a CLI when you entered commands to compile and run your programs. The most popular CLI program, and the one that you were most likely using, is bash. Your means of interacting with bash of course was through another program known as a terminal.

Bash commands fall into two categories: internal commands and external commands. There are only a few internal commands such as source and cd. Commands like these are also known as built-in commands; bash is pre-programmed to recognize these commands. If bash does not immediately recognize a given command, it will try to search for the appropriate program to run as specified in the first word in the command.

Commands such as ls, cp, and rm are external commands because bash delegates the task of running these commands to external programs. ls, for example, is actually a program that exists at /usr/bin/ls in the file system.

Whenever you run your compiled program in the terminal, you are running your program as an external command in bash.

Commands and terminal-bound programs that can be helpful for you include:

At the very least, you should know how to use the command-line C++ compiler. GNU C++ is what is available on most Linux distributions as well as Cloud9. On Cloud9, we will be using g++-5 as the compiler command.

Using the GNU C++ compiler (g++)

Let's recall the process of compiling and running a C++ program. First you start off with a source file like this one:

#include <iostream> #include <string> using namespace std; int main() { cout << "It works!"s << endl; return 0; }

Let's assume that this source file is saved in "/home/ubuntu/workspace/code/test.cpp". Now when we open the terminal, bash will run and automatically set our current directory at "/home/ubuntu/workspace". We would then use the following commands:

cd code g++-5 -std=c++14 -o test test.cpp ./test

The first command will bring us into the code directory, where test.cpp resides. The second command compiles test.cpp into a program named test, and the third command executes test.

The command "g++-5" is specific to the Cloud9 environment, in other operating systems it may be aliased as "g++" or "c++". In whatever case, it is the command for invoking the operating system's C++ compiler. The option "-std=c++14" means that we will use the C++14 standard of the C++ language. Our code will not compile if the compiler is using the wrong standard for our code (the default standard it uses is C++98).