Differences between gcc and g ++

Source: Internet
Author: User

Inadvertently, GCC has developed to version 4.3. Although there are few questions outside the software development community, GCC is used in almost all open-source software and free software, therefore, its compilation performance will directly affect the development of thousands of projects, such as Linux, Firefox, and OpenOffice.org and Apache. Therefore, it is no longer necessary to put GCC at the core of open-source software. On the other hand, the emergence of GCC4.3 is attracting the hearts of programmers. If we have to use a word to describe the relationship between GCC and programmers, it is undoubtedly "with your mind ".
History
As the flagship project of free software, Richard Stallman started to write GCC more than a decade ago. He just regarded it as a compiler for only a C programming language; GCC only means gnu c Compiler. After so many years of development, GCC not only supports the C language, but also supports the Ada Language, C ++ language, Java language, Objective C language, Pascal Language, and COBOL language, and Mercury languages that support functional and logical programming. GCC doesn't just mean the gnu c Compiler, But it turns into the GNU Compiler Collection, which is also the meaning of the GNU Compiler family. On the other hand, when talking about GCC's support for various hardware platforms, it is simply a word: ubiquitous. Almost all hardware platforms that are somewhat practical, and even some hardware platforms that are not so practical.
Introduction to Gcc
In Linux, gcc (gnu c Compiler) is a powerful and superior multi-platform Compiler launched by GNU. It is one of the representative works of GNU. Gcc is a super compiler that can compile executable programs on multiple hardware platforms. Its execution efficiency is 20% higher than the average efficiency of general compilers ~ 30%.
Gcc is the only compiler in linux. Without gcc, there will be no linux. The importance of gcc cannot be said. It is worth studying. Well, let's start our gcc journey!


First, eliminate the gcc and g ++ misunderstandings.
Both gcc and g ++ are GNU compilers.

Misunderstanding 1: gcc can only compile c code, while g ++ can only compile c ++ code

Both can be used, but note the following:
1. suffix. c, gcc treats it as a C program, while g ++ treats it as a c ++ program; suffix. cpp, both of which are considered as c ++ programs. Note that although c ++ is a superset of c, their syntax requirements are different. For example:
# 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 ");
}
If it is OK according to the C syntax, but once the suffix is changed to cpp, three errors are immediately reported: "printString undefined ";
"Cannot convert 'Char ** 'to 'Char *";
"Return-statement with no value";
Corresponding to the red part. It can be seen that the syntax rules of C ++ are more rigorous.
2. in the compilation phase, g ++ calls gcc. For c ++ code, the two are equivalent, but because the gcc command cannot be automatically connected to the library used by the C ++ program, therefore, we usually use g ++ to complete the link. For the sake of unification, we simply use g ++ to compile/link all. This gives us the illusion that, it seems that cpp programs can only use g ++.

Misunderstanding 2: gcc does not define the _ cplusplus macro, while g ++ will

In fact, this macro only indicates that the compiler will interpret the code in the C or C ++ syntax. As mentioned above, if the suffix is. c. If the gcc compiler is used, the macro is undefined. Otherwise, the macro is defined.

Misunderstanding 3: only gcc can be used for compilation, and only g ++ can be used for Link

Strictly speaking, this sentence is not a mistake, but it obfuscated the concept. It should be said that gcc/g ++ can be used for compilation, the link can use g ++ or gcc-lstdc ++. Because the gcc command cannot be automatically connected to the library used by the C ++ program, g ++ is usually used to complete the connection. However, in the compilation phase, g ++ automatically calls gcc, which is equivalent to the two.

Misunderstanding 4: extern "C" is related to gcc/g ++

In fact, it does not matter. Whether it is gcc or g ++, when using extern "c", it is named as C. Otherwise, all are named in c ++ mode. The test is as follows:
Me. h:
Extern "C" void CppPrintf (void );

Me. cpp:
# Include <iostream>
# Include "me. h"
Using namespace std;
Void CppPrintf (void)
{
Cout <"Hello" n ";
}

Test. cpp:
# Include <stdlib. h>
# Include <stdio. h>
# Include "me. h"
Int main (void)
{
CppPrintf ();
Return 0;
}

1. Add extern "C" to me. h to see if the names of gcc and g ++ are different.

[Root @ root G ++] # g ++-S me. cpp
[Root @ root G ++] # less me. s
. Globl _ Z9CppPrintfv // note the name of this function
. Type CppPrintf, @ function
[Root @ root GCC] # gcc-S me. cpp
[Root @ root GCC] # less me. s
. Globl _ Z9CppPrintfv // note the name of this function
. Type CppPrintf, @ function
Identical!

2. Remove extern "C" from me. h to see if the gcc and g ++ naming are different.

[Root @ root GCC] # gcc-S me. cpp
[Root @ root GCC] # less me. s
. Globl _ Z9CppPrintfv // note the name of this function
. Type _ Z9CppPrintfv, @ function
[Root @ root G ++] # g ++-S me. cpp
[Root @ root G ++] # less me. s
. Globl _ Z9CppPrintfv // note the name of this function
. Type _ Z9CppPrintfv, @ function
Identical!
[Conclusion] The results are exactly the same. It can be seen that extern "C" is irrelevant to the gcc/g ++. The above test indirectly confirms the previous statement: In the compilation stage, g ++ calls gcc.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.