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.

Path (Windows)

Mac and Linux also use a PATH, but you should not need to edit them by hand.

The path is the collection of directories where Windows looks for programs when you try to execute a program with a command like "main.exe" or "g++". It is stored in a variable called PATH - it is just a list of directories separated with semicolons:

C:\Program Files (x86)\Java\jdk1.7.0_07\bin;C:\Cygwin\bin;...

To see your current path, type PATH:

PATH

The path always includes the current folder. That is where Windows will look first for the program you tried to run. If it is not found, each directory in the PATH is checked to see if the program is there.

To get the c++ compiler working you need to make sure the folder containing g++ is on your path. It will be in the QT folder in a directory like C:\Qt\Tools\mingw810_64\bin. The exact version number (810_64) may be different, but it should be in a similar place (C:\QT\Tools\mingwXXX_XX\bin). This is the folder you need to add to the path to get the g++ command to work.

To add this folder to the path, open the Start menu, search for "environment", and find the Edit system environment variables panel:

Opening the Edit the System Environmental Variables control panel

Then click Environmental Variables:

Editing the Environmental Variables

Under the System variables section, find Path and Edit it:

Editing the Path

In the Path window, click New and then type the name of the folder that has your copy of g++ (the folder that looks like C:\Qt\Tools\mingwXXX_XX\bin):

Editing the Path

Then save the changes. You will have to close any open command prompt windows and reopen them to see the changes.

Using g++

Windows - To run g++ you must tell Windows where to find it - see Path section above

Mac - The compiler you will use is 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++

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

g++

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++11 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++11 Compiles the code using the 2011 version of the C++ language. If you don't specify this, your compiler will chose its own default, which might be c++11, 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