How to Check C++ Compiler Version in Linux
-
Check the C++ Compiler Version in Linux Using the
--version
Option -
Check the C++ Compiler Version in Linux Using the
cpp
Command With--version
- Check the C++ Compiler Version Directly
-
Check the C++ Compiler Version in Linux Using
cpp
With the-dM
Option - Conclusion
When working with C++ programming on a Linux system, it’s essential to know the version of the C++ compiler you are using. Different versions of the compiler may have varying features, optimizations, and compatibility, which can affect your code’s behavior and performance.
In this article, we’ll explore several ways to check the C++ compiler version in Linux.
Check the C++ Compiler Version in Linux Using the --version
Option
The GCC, which stands for GNU Compiler Collection, is a powerful compiler capable of compiling various programming languages, including C, C++, Objective C, and Objective C++. Specifically, for compiling C programs, the gcc
command is utilized, while the g++
command is employed for compiling C++ programs.
One of the most straightforward methods to determine the C++ compiler version is by using the --version
option with the g++
command.
-
Launch a terminal on your Linux system. Usually, you can find the terminal application in the applications menu or by searching for
Terminal
. -
In the terminal, type the following command and press Enter:
g++ --version
or
g++ -v
This command will show the version of the C++ compiler installed on your system.
-
Alternatively, you can type the following command and press Enter. Using
--version
withgcc
will also display theg++
version.gcc --version
It will give the version details of the GCC compiler installed on your system.
Check the C++ Compiler Version in Linux Using the cpp
Command With --version
The cpp
command is the C preprocessor, which is a part of the GCC (GNU Compiler Collection) suite. It can also be used to check the compiler version.
-
Launch a terminal on your Linux system.
-
Type the following command and press Enter:
cpp --version
This command will show the version of the C++ compiler.
Check the C++ Compiler Version Directly
If you have the g++
executable available, you can query its version directly using the strings
command:
-
Launch a terminal on your Linux system.
-
Type the following command and press Enter:
strings $(which g++) | grep "g++ version"
This command will extract and display the version information from the
g++
executable.
Check the C++ Compiler Version in Linux Using cpp
With the -dM
Option
Another way to check the C++ compiler version is by using the cpp
preprocessor, which is typically invoked by the g++
compiler. The -dM
option tells cpp
to print all predefined macros, including the __cplusplus
macro that specifies the C++ version.
Here’s how to check the C++ compiler version using this method:
-
Launch a terminal emulator on your Linux system.
-
Run the following command to print the
__cplusplus
macro, which indicates the C++ standard version:cpp -dM /dev/null | grep __cplusplus
The output will display the value of the
__cplusplus
macro, which corresponds to the C++ standard version.
Conclusion
Knowing the version of the C++ compiler you’re using is crucial for ensuring code compatibility and taking advantage of the latest features and optimizations. Linux provides multiple methods to check the C++ compiler version, including using the --version
option with g++
, gcc
, cpp
, or package manager commands.
Choose the method that suits your preference and workflow to easily determine the C++ compiler version on your Linux system.