Install and use a C++ compiler
Understand compiler choices, select a language standard, and build a C++ program from the command line.
Learning objectives
- Distinguish an editor, compiler, and build system.
- Recognize common C++ compilers.
- Compile a source file using a chosen language standard.
- Read the basic parts of a compiler command.
Prerequisites: understand what a C++ source file is and the difference between building and running a program. Review From C++ source code to an executable and How a C++ program starts and executes first. You will also need a terminal or an IDE in which to run compiler commands.
Tools you need
A text editor helps you write source code. A compiler translates C++ source into object code. A linker combines object files and libraries into a program. An IDE may bundle an editor, compiler integration, debugger, and project tools, but it is not itself the C++ language.
Common compiler families include GCC, Clang, and Microsoft Visual C++ (MSVC). Install one using the package manager or toolchain distribution recommended for your operating system. GCC's project does not provide precompiled binaries for every platform; its official binaries page lists third-party providers for some systems. Clang release binaries are available through LLVM, and Clang is also supplied by some operating-system distributions. On Windows, install MSVC through the Visual Studio Installer by selecting the Desktop development with C++ workload. Follow the selected provider's current instructions because package names and supported versions change.
Source file and command
Save a program in a file such as hello.cpp. With GCC or Clang, a basic command is:
g++ -std=c++17 -Wall -Wextra -Wpedantic hello.cpp -o hello
Here, -std=c++17 selects the C++17 language mode, warning flags ask the compiler to report suspicious code, hello.cpp is the input source file, and -o hello names the output program. Run it from the terminal using the platform's executable invocation rules.
Why choose a standard?
C++ evolves through published language standards. C++17 is a stable baseline for this course. C++20 and later add newer facilities, but a feature is only available when the compiler and its standard library support it. Selecting a standard does not automatically install support that your compiler lacks.
Compile, then run
source code (.cpp)
↓ compiler + standard library
object code / executable
↓ operating system launches it
program runs and produces outputCompilation and execution are separate. A successful compile means the toolchain produced a program; it does not prove the program's logic is correct.
Common mistakes
- Saving a file as
.txtinstead of.cpp. - Running the command from a directory that does not contain the source file.
- Using a compiler command that is not installed or not on PATH.
- Assuming a code editor automatically compiles C++.
- Ignoring warnings because the program happened to run.
Practice
- Install a compiler using its official instructions and record its version.
- Compile a minimal program with C++17 and the warning flags shown.
- Introduce a missing semicolon and read the diagnostic.
- Compile the same program with a newer standard flag if your toolchain supports it; identify any difference.
Check your understanding
Does a successful compile guarantee correct output?
Answer: No. The compiler checks language rules and builds the program, but logical errors can still produce incorrect behavior.
Practice guidance and solutions
Show commands and reasoning
- Follow the official installation guide for your operating system. Verify a GCC installation with
g++ --version, Clang withclang++ --version, or MSVC from a Developer Command Prompt withcl. Record the exact compiler version because support differs between releases. - With GCC or Clang, use
g++ -std=c++17 -Wall -Wextra -Wpedantic hello.cpp -o hello(or replaceg++withclang++). With MSVC, a typical command iscl /std:c++17 /W4 hello.cpp. - Remove a semicolon after a statement such as
int value = 3;, rebuild, and inspect the first diagnostic near that line. Restore the semicolon and rebuild to confirm the fix. - Try
-std=c++20with GCC/Clang or/std:c++20with a supporting MSVC version. A successful flag selection does not guarantee every library feature is implemented; consult the compiler's feature-support documentation.
Summary
Use an editor to write code, a compiler and linker to build it, and a debugger to inspect runtime behavior. Choose a standard explicitly and enable useful warnings.