When we compiled C + + code, some people use GCC, some people use g++, so all kinds of statements come, such as C code with GCC, and C + + code with g++, or compiled with GCC, link with g++, for a moment I do not know which statement is correct, if you encounter the last extern "C", There are more differences, and here I want to finish, after all, the purpose of knowledge is to be more sober, not more confused. Myth One: gcc can only compile C code, g++ can only compile C + + code
Both are possible, but please note: 1. The suffix is. C, GCC treats it as a C program, and g++ as a C + + program; the suffix is. cpp, both of which are considered C + + programs, note that although C + + is a superset of C, there are different requirements for syntax, such as: #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 you follow the grammar rules of C, OK, no problem, but once the suffix is changed to CPP, three errors are reported immediately: "Printstring undefined"; "Cannot convert ' char** ' to ' char*"; "Return-statement with no value"; Corresponds to the part of the previous red callout, respectively. Visual C + + syntax rules are more rigorous. 2. During the compile phase, g++ calls GCC, which is equivalent to C + + code, but because the GCC command is not automatically linked to the libraries used by C + + programs, it is usually done with g++ to complete the link, for the sake of unification, simply compile/link all with g++, which gives an illusion, As if the CPP program can only be used g++.
Myth Two: GCC does not define __cplusplus macros, and g++ will
In fact, this macro simply indicates that the compiler will interpret the code in C or C + + syntax, as described above, if the suffix is. c, and the GCC compiler is used, the macro is undefined, otherwise it is defined.
Myth Three: Compile only with GCC, link only with g++ Strictly speaking, this sentence is not wrong, but it confuses the concept,This should be said: Compile can be used gcc/g++, and links can be used g++ or gcc-lstdc++。 Because GCC commands are not automatically joined to libraries that are used by C + + programs, you typically use g++ to complete joins. In the compilation phase, however, g++ automatically calls GCC, which is equivalent.
Myth four: extern "C" has relation with gcc/g++
In fact, no matter whether it is GCC or g++, with the extern "C", all are named in C to the symbol, otherwise, are named in C + +. 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. First add extern "C" to me.h, see what is different with GCC and g++ naming
[Email protected] g++]# g++-S Me.cpp [email protected] g++]# less ME.S . GLOBL _Z9CPPPRINTFV//Note the naming of this function . Type cppprintf, @function [Email protected] gcc]# gcc-s me.cpp [email protected] gcc]# less ME.S . GLOBL _Z9CPPPRINTFV//Note the naming of this function . Type cppprintf, @function Exactly the same!
2. Remove the extern "C" in the me.h to see what is different with GCC and g++ naming [Email protected] gcc]# gcc-s me.cpp [email protected] gcc]# less ME.S . GLOBL _Z9CPPPRINTFV//Note the naming of this function . Type _Z9CPPPRINTFV, @function [Email protected] g++]# g++-S Me.cpp [email protected] g++]# less ME.S . GLOBL _Z9CPPPRINTFV//Note the naming of this function . Type _Z9CPPPRINTFV, @function Exactly the same! "Conclusion" is identical, it can be seen that extern "C" is not related to the adoption of gcc/g++, the above test also indirectly confirms the previous saying: In the compilation phase, g++ is called GCC. |
|