Source: C Language Chinese Network
Editor: strongerHuang
Source: C Language Chinese Network
Editor: strongerHuang
As of September 2020, the GCC compiler has been updated to version 10.2, and its functionality has expanded from initially only compiling C language to now supporting multiple programming languages, including C++.
In addition, the current GCC compiler also supports compiling programs in Go, Objective-C, Objective-C++, Fortran, Ada, D, and BRIG (HSAIL). Even GCC version 6 and earlier supported compiling Java programs. However, this tutorial primarily focuses on how to use the GCC compiler to compile and run C and C++ programs, so we will not go into detail about compiling other programming languages with the GCC compiler.
So, assuming we have edited C or C++ code, how can we call the GCC compiler to compile our program? It’s simple; the GCC compiler provides an interface for us to call it. For C or C++ programs, we can invoke the GCC compiler by executing the gcc or g++ commands. It’s worth noting that in practice, we are more accustomed to using the gcc command to compile C programs and the g++ command to compile C++ code. It’s important to emphasize that this is not the difference between gcc and g++; the gcc command can also compile C++ programs, and the g++ command can also be used to compile C programs. So, what is the difference between gcc and g++? Let’s explain in detail.
In fact, any program code supported by GCC can be compiled using the gcc command. One can understand that gcc is the general compilation command for the GCC compiler because it can automatically determine the programming language category based on the file extension, for example:
-
xxx.c: Compiles this file as a C language program by default;
-
xxx.cpp: Compiles this file as a C++ program by default.
-
xxx.m: Compiles this file as an Objective-C program by default;
-
xxx.go: Compiles this file as a Go language program by default;
Of course, the gcc command also provides users with an interface to “manually specify the compilation method” using the -x option. For example, gcc -xc xxx indicates compiling xxx file as C language code; while gcc -xc++ xxx indicates compiling xxx file as C++ code. Specific examples of the -x option will be provided later.
However, if you use the g++ command, regardless of the target file’s extension, it will always compile the file as C++ code. That is to say, for .c files, the gcc command treats them as C language code, while the g++ command treats them as C++ code. But for .cpp files, both gcc and g++ will compile them as C++ code. Some readers may think that since C++ is compatible with C, there should be no difference in compiling C programs using gcc or g++. However, this is not the case. Strictly speaking, the syntax requirements of the C++ standard and the C language standard differ. For example:
// In demo.c file#include <stdio.h>int main(){ const char * a = "abc"; printStr(a); return;}int printStr(const char* str){ printf(str);}
As shown above, this is a piece of non-standard C language code. If we compile using the gcc command, as shown below:
[root@bogon ~]# gcc -xc demo.c # or directly run gcc demo.c[root@bogon ~]#
We can see that there were no errors during the execution process. However, if we compile the same program using the g++ command:
[root@bogon ~]# g++ demo.cdemo.c: In function ‘int main()’:demo.c:5: error: ‘printStr’ was not declared in this scopedemo.c:6: error: return-statement with no value, in function returning ‘int’[root@bogon ~]#
We can see that the GCC compiler found 3 errors. Clearly, the C++ standard has stricter requirements for code writing standards. Furthermore, there are also differences in compiling and executing C++ programs using gcc and g++. It is important to note that many C++ programs will call existing functions or class objects from certain standard libraries, and the plain gcc command cannot automatically link these standard library files. For example:
// demo.cpp#include <iostream>#include <string>using namespace std;int main(){ string str = "C Language Chinese Network"; cout << str << endl; return 0;}
This is a very simple C++ program that defines a string object using the string class provided by the <string> header file and then outputs it using the cout output stream object. For this C++ code, if we compile using the g++ command, as shown below:
[root@bogon ~]# g++ demo.cpp[root@bogon ~]#
We can see that there were no errors during the entire compilation process. But if we use the gcc command:
[root@bogon ~]# gcc demo.cpp/tmp/ccIOnwra.o: In function `main’:demo.cpp:(.text+0x13): undefined reference to `std::allocator<char>::allocator()’# omitted many error messages
Readers can compile it themselves to see many error messages. The fundamental reason lies in the fact that this program uses the classes and objects provided by the standard libraries <iostream> and <string>, which gcc cannot find by default. If one wants to use the gcc command to compile and execute C++ programs, they need to manually add the -lstdc++ -shared-libgcc options when using the gcc command, indicating that gcc can link the necessary C++ standard libraries when compiling C++ programs. That is to say, we can compile the demo.cpp file like this:
[root@bogon ~]# gcc -xc++ -lstdc++ -shared-libgcc demo.cpp[root@bogon ~]#
Thus, demo.cpp has been successfully compiled.
Readers can think of the g++ command as equivalent to
gcc -xc++ -lstdc++ -shared-libgcc
command. Clearly, the latter is quite cumbersome to write, and most people prefer the former.
There are many other detailed differences between the gcc and g++ commands, which will not be elaborated here. After reading this section, readers just need to know that for compiling C language programs, we should use the gcc command, while for compiling C++ programs, it is recommended to use the g++ command, and that is sufficient.
Recommended Reading:
Complete Organization | Directory of 365 High-Quality Technical Articles
The Beauty of Algorithms: Stacks and Queues
The 10 Algorithms that Dominate the World
Thoroughly Understanding Cookies, Sessions, and Tokens
A Brief Talk on What Recursive Algorithms Are
Focusing on the Summary and Sharing of Server Backend Technology Stack Knowledge
Welcome to Follow and Communicate for Mutual Progress