Before writing and running C++ programs, you need a C++ compiler. A compiler converts C++ source code into a form that the computer can execute.
A C++ compiler is a software tool that translates C++ source code into machine code or another intermediate form that can be executed by a computer.
For example, a C++ source file may contain:
hello.cpp
The compiler processes this source file and produces an executable program.
Computers cannot directly execute ordinary C++ source code. The source code must be processed by a compiler.
C++ Source Code
↓
Compiler
↓
Executable Program
↓
Execution
Several compilers can compile C++ programs.
The choice of compiler depends on the operating system and development environment you want to use.
GCC is a widely used compiler collection that supports C++ through its GNU C++ compiler, commonly invoked as g++.
A simple compilation command is:
g++ hello.cpp -o hello
This command compiles hello.cpp and creates an executable named hello on systems where this command is supported.
Clang is a compiler front end from the LLVM project. Its C++ compiler driver is commonly available through the clang++ command.
clang++ hello.cpp -o hello
The command compiles the C++ source file and produces an executable.
Microsoft Visual C++ (MSVC) is Microsoft's C++ compiler and development toolset. It is commonly used with Visual Studio on Windows.
Visual Studio provides an integrated environment for writing, compiling, debugging, and managing C++ projects.
IDE stands for Integrated Development Environment. An IDE provides tools for writing and developing software in one application.
An IDE may include:
Some commonly used development environments for C++ include:
Different IDEs provide different features and workflows.
On Windows, you can install a C++ development environment such as Visual Studio with the required C++ development tools.
Another option is to install a compiler such as GCC through a suitable Windows toolchain.
When installing Visual Studio for C++ development, select the workload that provides the C++ desktop development tools.
The installation normally includes the required compiler, libraries, debugging tools, and development components.
GCC can be installed through an appropriate compiler toolchain for your operating system.
After installation, the g++ command can be used to compile C++ programs if the compiler's executable directory is correctly configured.
After installing GCC, you can check whether the compiler is available from the terminal.
g++ --version
If the compiler is correctly installed and available in the system path, information about the installed compiler will be displayed.
If Clang is installed, you can check its version using:
clang++ --version
The command displays information about the installed Clang compiler.
Many Linux distributions provide GCC through their package management system. After installing the required compiler package, you can use the g++ command from the terminal.
g++ --version
The exact installation command depends on the Linux distribution.
On macOS, C++ development can be done using Apple's developer tools, which provide the Clang compiler.
The compiler can commonly be accessed through the clang++ command after the required developer tools are installed.
Create a file named hello.cpp.
#include <iostream>
int main() {
std::cout << "Hello, C++!";
return 0;
}
Save the file with the .cpp extension.
Open a terminal in the directory containing hello.cpp and run:
g++ hello.cpp -o hello
If compilation succeeds, an executable named hello is created according to the conventions of your operating system.
On Windows, if the executable is named hello.exe, it can be run from the terminal with:
hello.exe
Depending on the shell and current directory, you may also use:
.\hello.exe
On Linux and macOS, an executable can commonly be run from the current directory using:
./hello
The exact output and executable naming can depend on the platform and compiler command used.
You can compile and run a program as separate steps.
g++ hello.cpp -o hello./hello
The first command compiles the program. The second command runs the generated executable on systems using the ./ convention.
Compilers can be instructed to use a particular C++ language standard. For example:
g++ -std=c++17 hello.cpp -o hello
This asks GCC to compile the program using the C++17 language standard.
A compiler reports errors when the source code does not follow the language rules or when another compilation problem occurs.
Example:
#include <iostream>
int main() {
std::cout << "Hello"
return 0;
}
The missing semicolon after the output statement can cause a compilation error.
Compilers can also provide warnings about code that may be problematic or deserves attention even when compilation can continue.
Warnings are useful because they can help programmers find potential issues early.
A debugger allows you to execute a program step by step and inspect values while the program is running.
Common debugging features include:
A compiler translates source code into an executable or another compiled form.
An IDE is a development environment that can combine an editor, compiler integration, debugger, and other development tools.
Compiler → Compiles C++ Code
IDE → Editor + Compiler Tools + Debugger + Other Tools
A beginner can follow this simple workflow:
Try the following program after installing your compiler:
#include <iostream>
int main() {
std::cout << "Welcome to C++ Programming!";
std::cout << "\nI am learning C++.";
return 0;
}
Compile and run the program to verify that your C++ environment is working correctly.
Beginners may encounter problems such as:
Checking the compiler version is a useful first troubleshooting step.
| Step | Check |
|---|---|
| 1 | C++ compiler installed |
| 2 | Code editor or IDE available |
| 3 | Compiler command works |
| 4 | .cpp file can be created |
| 5 | Program compiles successfully |
| 6 | Program runs successfully |
Once a compiler and development environment are installed and working, you are ready to start writing C++ programs.
#include <iostream>
int main() {
std::cout << "My First C++ Program";
return 0;
}
In the next lesson, we will create and understand a complete first C++ program step by step.
Question: Which command is commonly used to check the GCC C++ compiler version?