The difference between GCC and g++ under Linux

Source: Internet
Author: User

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

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.