G ++ and GCC misunderstandings

Source: Internet
Author: User

Both GCC and G ++ are GNU compilers.

Misunderstanding 1: GCC can only compile CCode, G ++ can only compile C ++ code
Both can be used, but note the following:
1. The suffix is. C. GCC treats it as C.ProgramAnd g ++ 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. C ++ syntax rules 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.

Differences between GCC and G ++
Me
When compiling C/C ++ Code, some people use GCC, some use g ++, so all kinds of statements come, such as C code using gcc, c ++ Code uses g ++ or compilation
GCC, the link uses g ++. At the moment, I do not know which statement is correct. If the previous extern
"C", there are more differences. Here I want to end it. After all, the purpose of knowledge is clearer, not more confused.

Misunderstanding 1: GCC can only compile C code, while G ++ can only compile C ++ code Both can be used, but note: 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 rules, 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"; corresponds to the section marked in red. 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] Completely the same, it can be seen that extern "C" is irrelevant to the use of GCC/g ++. The above test also indirectly confirms the previous statement: In the compilation stage, g ++ calls GCC.

Appendix:

 
Apt-Get install build-essential

C ++ will be installed here

Undefined reference to '_ gxx_personality_v0' appears when the. cpp file is compiled with GCC ',

There are two solutions:

To compile the C ++ program, you must add-lstdc ++ option. sample: gcc-lstdc ++-O test. C. Otherwise, an error is reported.Error

Undefined reference to '_ gxx_personality_v0'

GCC atest. cpp-O atest

This is not the case. You can:

Gcc-lstdc ++ atest. cpp-O atest

Or you can use g ++.

G ++ atest. cpp-O atest

Right-click the command to bring the terminal function to the file:

Sudo apt-Get install Nautilus-open-Terminal

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.