Start Programming in C++
Before continuing, make sure you have installed the required software in Getting Started - Software Setup.
In this guide, you will learn:
- How to use the command-line interface.
- How to compile C++ programs.
- How to run your compiled programs.
The Command-Line Interface (CLI)
Demonstration on how to get up and running with the command line to compile and run C++ programs.
- Powerful method of interacting with the computer
- Accepts successive lines of typed commands (instructions)
- Commands may print (display) text output.
Accessing the CLI
| OS | Name | Icon |
|---|---|---|
| Windows | “Command Prompt” or “CMD” | |
| macOS | “Terminal” | ![]() |
| Linux | “Terminal,” “Shell,” or similar |
Example Commands
| Windows | macOS / Linux | Description |
|---|---|---|
| cd | cd | Change directory |
| dir | ls | List directory |
| copy | cp | Copy a file or directory |
| move | mv | Move a file or directory (also useful to rename files) |
| del | rm | Remove a file or directory |
| mkdir | mkdir | Make a new directory |
| cls | clear | Clear the CLI |
Parameters
- A parameter is an additional piece of information passed to the command.
- Parameters are space separated
- Examples (in Windows)
dir /?- Displays helpful information about the dir command including what additional parameters do.
dir /w /p- Displays a list of files and subdirectories in a directory in a wide list view and pauses after each screenful of information.
Compiling with GCC
Before continuing, you should have Visual Studio Code and g++ to make your first C++ program. Refer back to Getting Started if needed.
Important
When following along with the video below, type codium in the command line instead of code. Unlike the video, we are using VSCodium instead of Visual Studio Code.
A Simple C++ Program
Create a text file named
hello.cpp(cpp is the extension we give to C++ source code files).Type the following code into the file (displays “Hello World!” on the screen).
cpp#include <iostream> using namespace std; int main() { cout << "Hello World!" << endl; return 0; }
Compiling with g++
In the command-line interface:
- Change the directory to the location of
hello.cpp.- For example,
cd "C:\Users\Sean\235" - Hint: Put double quotes around any parameters with spaces.
- Hint: Hit tab while typing the path to autocomplete the rest of the directory name.
- For example,
- Run g++ with the following command:
g++ -Wall -Wextra -o world hello.cpp
- No output means it worked!
Understanding the Command
g++ -Wall -Wextra -o world hello.cpp
g++ is the name of the compiler program and the rest of the lines are parameters.
| Parameter | Parameter Description |
|---|---|
-Wall | Enables additional warnings about questionable constructions that are easily avoid. |
-Wextra | Enables even more helpful warnings. |
-o world | Name the executable file we are creating “world”. |
hello.cpp | The name of the input file (must be the last parameter). |
Additional Useful Parameters
-fmax-errors=5
- This will tell the compiler to output at most 5 errors.
- The number 5 can be changed to any number you want.
- Sometimes when we have one problem, it produces a ton of errors. Use this parameter to only show the first however many.
- Always look at the first error first.
-std=c++23
- This will tell the compiler to use the latest (2023) standard of C++.
- This may be necessary if you are using more recent features of the language.
- If your compiler doesn’t support the 2023 standard, you can try a previous standard:
-std=c++20for the 2020 standard of C++-std=c++17for the 2017 standard of C++.
-pedantic or -Wpedantic
- GCC compilers always try to compile your program if this is at all possible. However, in some cases, the C++ standard specifies that certain extensions are forbidden.
- Use this option to receive warnings when your code can compile but is NOT valid C++.
- Helps track down errors and ensures a better assignment grade.
-Wmissing-declarations
- Notifies you if you accidentally define a function without a prototype (i.e. forward declaration).
Run the Hello Program
- To run your newly-compiled program, type the name of the program
world - In macOS or Linux, you may need to put a
./before the program name../world
