First a C + + program was written, hello,world!
#include <iostream>
using namespace Std;
void Main ()
... {
cout << "hello,world!" <<endl;
Return
}
With the command: Gcc-o test test.cpp Compile, there is a problem. Then analyze, which is to run GCC separately.
Gcc-c-O test.o test.cpp. Executed successfully.
Gcc-o Test TEST.O. A bunch of link errors appear.
g++-O test test.o. Executed successfully.
It is found that there are some differences between GCC and g++.
GCC and g++ are the GNU C & C + + compilers, gcc/g++ a total of 4 steps when doing the compile work
1. Preprocessing, generating. I files [
Preprocessor CPP] 2. The preprocessed file is not converted to assembly language, and the file is generated. s[
Compiler CCL] 3. There is a compilation into the target code (machine code) to generate. o files [
Assembler as] 4. Connect the target code and generate the executable program [
linker ld], Eg: Merge the print.o file connection into the hello.o file
1: Both can compile C and C + + code, 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 is a difference between the two requirements for syntax. C + + syntax rules are more rigorous.
(2) . during the compile phase, g++ calls GCC, which is equivalent for C + + code, but because GCC commands are not automatically connected to libraries used by C + + programs, you usually use g++ to complete the link , and for the sake of unification, simply compile/ Links are all used g++, which gives an illusion, as if the CPP program can only use g++-like.
2: for __cplusplus macros , in fact, this macro simply signifies 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 .
5 |
compilation can be made with gcc/g++, and links can be g++ or gcc-lstdc++. Because GCC commands are not automatically joined to libraries used by C + + programs, you typically use g++ to complete joins。 In the compilation phase, however, g++ automatically calls GCC, which is equivalent.
The difference between GCC and g++ under Linux