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. 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 ++
|
|
When we compile C/C ++ Code, some people use GCC and some people use g ++, so all kinds of statements come, such as C code using gcc, c ++ Code uses g ++, or GCC for compilation, and 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 to be 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 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. ----------------- Add it to favorites for future reference |
|