What is the difference between g++ and gcc?

来源:互联网 发布:php跨域请求 post 编辑:程序博客网 时间:2024/06/14 18:37

What is the difference between g++ and gcc?

GCC: GNU Compiler Collection, Referrers to all the different languages that are supported by the GNU compiler.
gcc: GNU C Compiler
g++: GNU C++ Compiler
The main differences:
1. gcc will compile: .c/.cpp files as C and C++ respectively.
2. g++ will compile: .c/.cpp files but they will all be treated as C++ files.
3. Also if you use g++ to link the object files it automatically links in the std C++ libraries (gcc does not do this).
4. gcc compiling C files has less predefined macros.
5. gcc compiling .cpp and g++ compiling *.c/.cpp files has a few extra macros.

introduction

gcc and g++ are the GNU compilers for C and C++ programs respectively and are commonly used today.However,
there exist some misunderstandings of gcc and g++ because they are not two completely independent compilers.
In this article, I will clarify several common misunderstandings on gcc and g++.

Misunderstanding 1: gcc compiles only C programs and g++ compiles both C and C++ programs

This misunderstanding is probably due to the naming of gcc. Gcc formerly stood for ‘GNU C Compiler’ when
gcc was first developed 1987 and when GCC could compile only C code. However, GCC of today is a compiler
collection instead of a single compiler for compiling a single language. Today’s GCC is an integration of several
versions of the compiler including C, C++, Objective-C, Ada, Fortran, Java and treelang. So the name gcc
stands for “GNU Compiler Collection” toay and gcc can compile programs written in any of these languages.
This means gcc can compile both C and C++ programs, and g++ can do the same. However, there are three
key differences when compiling the same program using gcc and g++:

First, gcc and g++ differentiate program languages by checking the file name extensions. gcc treats a .c file
as a C file and a .cpp file as a C++ file. However, g++ treats both .c and cpp files as C++ files. This may have
some unwanted implications, for instance, the following program with a file name new2.c will compile
successfully with gcc, but will fail with g++:

#include <stdio.h>        int main(void)        {            int new = 5;            printf("The value of new is: %d\n", new);            return new;        }  

Figure 1 shows the compiling result of gcc and g++.
这里写图片描述

Figure 1 Result of compiling a .c program with gcc and g++
This is because the word ‘new’ is a reserved keyword in C++, which can not be used as a variable name, but it’s
a legal variable name in C although it’s not a good variable name in any sense.

Second, when you use gcc to compile a .cpp program, if the program needs to link to the standand C++ library,
gcc will not link to the standand C++ library by default. You need to use the flag -lstdc++ with gcc to make it link
to the C++ library, such as, gcc new2.cpp -lstdc++. However, when compiling a .cpp file with g++, g++ will link to
the C++ library automatically.

Third, although C++ is a superset of C, g++ has different or more strict rules when parsing code. This means
a .c file may not always compile with g++ even if it does not use any C++ keywords, for example, Figure 2 shows
another example in which a C file with a file name new2.c compiles successfully with gcc but fails with g++.

#include <stdio.h>    int main(int argc, char* argv[]) {         if(argv == 0) return;         printString(argv);         return;    }    int printString(char* string) {         sprintf(string, "This is a test.\n");    }

这里写图片描述
Then, what is the relationship between gcc and g++? As explained above, GCC (we use GCC instead of gcc to
refer to the compiler collection here) is actually a collection of compilers instead of a single compiler for a specific
language. So, when we talk about GCC, we should understand it as a generic compiler. GCC has several front
ends for different languages, such as gcc for C, g++ for C++, and gnat for Ada, etc. Unfortunately, gcc, the front
end of GCC for C language, happens to have the same name of GCC, which referes to the GNU compiler
collection.This is the major reason why GCC is often misunderstood as only a C compiler.

Actually, when you pass a .cpp file to g++, g++ will call gcc for compiling, after the intermediate language
representation is generated, g++ will call the linker to link. The GCC generic compiler has three parts, front end,
middle end, and back end. The front end is the interface for different programming languages and is responsible
for parsing programs of different languages into a syntax tree, the middle end does code analysis and optimization
for the intermediate language representation generated by the front end. The back end will generate the final
machine executable code. So the compilers in the GCC have different front ends but the same middle and back
end, or the GCC core.

Here are the general compiling phases of gcc/g++:

  • Step 1: Preprocessor cpp parses and translates source code and generates .i file  
  • Step 2: Compiler egcs compiles .i file into assembly code
  • Step 3: Assembler as translates assembly code into object files (.o files) 
  • Step 4: Linker ld links object files to library functions and generates executable files (.exe files)

You may have ever seen or used cc as a C compiler. cc is only an old C compiler which belongs to the UNIX OS family
in the old days. cc is obsolete because gcc is a much better complier today and supports features of the latest
C/C++ standand. Most Linux OSes still have the cc command, but cc usually is only an alias and actually links to a
gcc compiler.

Misunderstanding 2: g++ is a preprocessor of gcc

This misunderstanding is probably due to the fact that g++ will call gcc during the compiling phase. However,
as explained previously, g++ actually is a complete compiler, it is not a preprocessor of any other compiler, it calls
gcc actullay means calls the GCC core, not the C compiler. Actually, G++ builds object code directly from your C++
source code, it does not generate any intermediate C version of the program. So, when we talk about gcc, g++,
or gnat, we should understand each of them is a complete compiler, they have a different front end but calls the
same back end, the GCC core.

As explained in Misunderstanding 1, When compiling C++ programs, the only diffrence between gcc and g++ is,
gcc does not link by default, but you can make gcc link to the C++ libraries if you pass gcc command the flag
-lstdc++. However, g++ will link to the C++ libraries automatically by default.

Misunderstanding 4: The keyword extern “C” will determine which compiler to call for compiling

The extern “C” will affect only linkage, it won’t turn C++ compiler to C compiler automatically. It only tells the
compiler, either gcc or g++, during the compile phase, to name the symbols for function names in C style, and
do not enforce name mangling, so the function can be called from client C programs. (Name mangling is enforced
when compiling C++ programs because C++ has function overloading, which cannot depend on only function
names to differentiate functions).

The following example shows the declaration, extern “C”, only affects the naming of symbols, the program is still
standard C++ program and, and this declaration will not affect which compiler to use at all.

//The C++ program new4.cpp:    #include <iostream>    using namespace std;    extern "C" {       void CppPrintf(void)        {              cout << "Hello\n";         }      }    int main(void)     {          CppPrintf();          return 0;     } 

Figure 3 shows the symbol names of the function CppPrintf() with declaration extern “C”:

You may see the naming of the function name is the same for both gcc and g++, which is simply the function name
with no name mangling (C style).

这里写图片描述

Figure 3. Symbol name for function CppPrintf() with declaration extern “C”

Now we remove the extern “C” and recompile it.

#include <iostream>    using namespace std;    void CppPrintf(void)     {           cout << "Hello\n";     }     int main(void)     {          CppPrintf();          return 0;     } 

Figure 4 shows the symbol names of the function CppPrintf() without declaration of extern “C”:

You can see the symbol name for the function name is still the same for both gcc and g++, but is different from the
naming in Figure 3. The names of functions are ‘mangled’ (C++ style).

这里写图片描述
Figure 4. Symbol name for function CppPrintf() with no declaration extern “C”

Finally, the suggestion is, use gcc for C program and g++ for C++ code. You may achieve the same result by
passing parameters or turning on or off some switches to the compiler command, however, there is no reason to take
a detour when you have a direct path, unless you do have a reason to compile a C program with g++ or a C++
program with gcc. Remember, we have two compilers, gcc and g++, instead of a single one, with a purpose.

Reference
1. What is the difference between g++ and gcc?
2. Difference Between GCC and G++
3. gcc和g++到底啥关系

0 0
原创粉丝点击