Open In App

Difference Between gcc and g++

Last Updated : 17 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The GNU Compiler Collection, abbreviated as GCC provides multiple compilers to compile source codes of different programming languages, mainly C and C++. In its command line interface, it provides two commands "gcc" and "g++" which are used to invoke the compiler to compile the given source code to executable file.

In this article, we will learn what are the main differences between the "gcc" and "g++" commands provided by GNU Compiler Collection.

What "gcc" Command Do?

The command "gcc" is used to invoke the corresponding compiler for multiple programming languages like C, C++, objective C, etc. It recognizes the complier that is to be used from the extension of the provided source file.

For example,

C language compiler can be called using:

gcc src.c -o sic

and C++ compiler can be called using:

gcc src.cpp -o src

Just like that, we can call other compilers included in the GCC. But there is a little problem with compiling C++ code with "gcc" command. It does not link the standard library of C++ by default, so the code that contains standard library features will not be able to compile. That is why it is mainly used to compile C code.

What "g++" Command Do?

Just like "gcc", the "g++" command calls the compiler but only for C++ programming language. It is specifically designed to work with C++ source codes and links all the standard libraries with the compiled program.

For example,

g++ src.cpp - src

Difference Between gcc and g++

The following table lists all the major differences between the gcc and g++ commands:

gcc

g++

'gcc' is used to compile C program.

'g++' is used to compile C++ program.

'gcc' can also compile .cpp files and they will be treated as C++ respectively.

'g++' can compile both .c or .cpp files but they will be treated as C++ files only.

'gcc' does not link standard C++ libraries by default.

'g++' automatically links the standard C++ library.

'gcc' compiles C++ files with more number of predefined macros. Some of them are #define __GXX_WEAK__ 1, #define __cplusplus 1, #define __DEPRECATED 1, etc.

'g++' compiles with more predefined macros.

'gcc' is mainly used for C language code compilation.

'g++' is mainly used for C++ code compilation.

Conclusion

In summary, GCC and G++ are both program compiler tools in the GNU Compiler Collection, each serving different purposes based on the programming language being compiled. While GCC is a versatile compiler that can handle many languages, G++ is used for C++ programming, providing specific features like automatic linking of C++ standard libraries.


Next Article

Similar Reads