Command Line Tutorial

You should become familiar with how to compile and work from the command line. Below is a quick reference and instructions on how to use the gcc c++ compiler (g++) from the Windows command line. g++ is included as part of the QTCreator bundled download. If you are on a Mac/Linux box, some commands will differ, but many will be the same.

Opening a command prompt:

Windows : You can use either the traditional Command Prompt or PowerShell. To open, go to the start menu and search for either "Command Prompt" or "PowerShell". You can also shift-right click inside a folder in Windows and get an option to "Open command prompt here" ("Open PowerShell window here" on more recent versions of Windows)

Mac : Use spotlight to search for Terminal and open a terminal window.

Command line quick reference

GoalCommandExample
Display the name of the current folderpwd
pwd
Change into a subdirectory of the current foldercd DIRNAME
cd cs161
Go up a directory level
.. means "up one directory level"
. means "the current director
cd ..
cd ..
Go to top level of the filesystem (or of the current drive in Windows)cd /
cd /
Switch to different drive
Windows only
DRIVE_LETTER:
C:
See what is in this directory

For Mac/Linux
ls

For Mac/Linux, to see more info
ls -la

For Windows
dir

ls

ls -la

dir

Compile a file
SEE G++ SECTION
g++ -g -o PROGRAM_NAME FILENAME_1 FILENAME_2...
g++ -g -o myprogram.exe assign3.cpp
or

g++ -g -o myprogram.exe assign3.cpp helper.cpp
Run a program
Program must be in current directory or on the PATH

Windows command prompt
PROGRAM_NAME

Windows power shell
.\PROGRAM_NAME

Mac/Linux
./PROGRAM_NAME

Windows command prompt

myprogram.exe

Windows power shell

.\myprogram.exe

Mac/Linux

./myprogram.exe

Speeding things up

In most places times, pressing tab will try to autocomplete what you have typed so far. Hit tab multiple times to cycle through possible completions. Use up arrow and down arrow to reuse recently typed commands.

Compiler Setup

You should have already installed the toolchain that includes g++ as part of setting up VSCode. If not, check out these instructions for installing the compiler. Make sure to follow the instructions for PC, Mac, or Linux as appropriate.

Using g++

Any time you want to compile files, you use the program g++:

g++

If this does not work, you do not have the compiler installed correctly.

Mac Users: The compiler you will use is actually called clang. But, it sets itself up to "hijack" calls to g++ So you can just type g++ as shown below, or you can type clang++ instead.

Run by itself will tell you "no input files". You did not tell it what file to compile. To specify files, list one or more file names after the g++:

g++ filename.cpp

Or

g++ filename.cpp otherfile.cpp

This will make a program called a.exe in Windows or just a on Mac/Linux. To give the program a better name we can add -o PROGRAM_NAME (-o is for "output file name") before the name of the file to compile:

g++ -o myprogram.exe filename.cpp 

This makes the program that is build be called myprogram.exe.

To run your program, type myprogram.exe (On Linux/Mac, type ./myprogram.exe)

myprogram.exe

g++ Options

g++ supports many options. Simply include them after g++:

g++ -g -Wall -std=c++17 main.cpp -o myprogram.exe

Here are some important ones to recognize:

-g Compile with debug information built in. Necessary for meaningful debugging but bloats the executable, make it much slower to run. Normally you

-std=c++17 Compiles the code using the 2017 version of the C++ language. If you don't specify this, your compiler will ch0ose its own default, which might be c++17, c++17, c++20, etc...

-O0 Capital Oh followed by zero. No optimization (default)

-O2 All reasonable code optimizations - compile time is slower, but program will run significantly faster (building release in QTCreator).

-O3 Even more optimization - these may make the executable much larger in an effort to speed things up.

-Wall Turn on most warnings.

-o Output file name.

-c Create object files – do not link into application.

-I folder Look in given folder (may be relative using. or .. or absolute starting with C:...) for header files that are included.

In general, they can go in any order. If two things conflict (-O0 and -O3), last one usually wins.

For more complete details on compiling from the command line, refer to section 1.5-1.8 of this reference: https://www3.ntu.edu.sg/home/ehchua/programming/cpp/gcc_make.html

IO Redirection

Input/output redirection allows you to send program output to a file or a file's contents to a program as input.

Program output to file

Use > FILENAME to send output to a file. This runs myprogram.exe sends all of its output to the file out.txt instead of the console.

myProgram.exe > out.txt

Note: This includes any input prompts... you have to know what needs to be typed in as input to the program and do so without the prompts!

Use a file as program input

Use < FILENAME to send a file to a program as input. This runs myprogram.exe and whenever it needs input, gets it from in.txt instead of the console.

myProgram.exe < in.txt