Duanxx C + + learning: gcc and g++

Source: Internet
Author: User

In fact, before this, I always thought that gcc and g++ is a thing, just have two different names only, today in Linux under the compilation of a C code error only to find the difference between GCC and g++.

First describe the error you encountered today:


Because before writing the program, write is CPP file, and then directly use g++ compile, no error, also did not care about these problems, today there is a C file, but contains after the STL vector syntax, think of using GCC to compile, the result error, error is: Can't find the vector.



What is gcc/g++

First, explain: gcc and gcc are two different things.

Gcc:gnu Compiler Collection (GUN compiler Collection), it can compile C, C + +, JAV, Fortran, Pascal, Object-c, Ada and other languages.


GCC is the gun C Compiler (C compiler) in GCC

G++ is the gun C + + Compiler (c + + compiler) in GCC

An interesting fact is that, in essence, gcc and g++ are not compilers, nor are they collections of compilers , they are just a drive, depending on the type of file to be compiled in the parameter, call the corresponding gun compiler, for example, to compile a C file with GCC, there are several steps:

Step1:call a preprocessor, like CPP.

Step2:call an actual compiler, like cc or cc1.

Step3:call an assembler, like as.

Step4:call a linker, like LD

because The compiler can be replaced , so GCC can not only compile C files

So, more accurately, GCC calls C compiler, and g++ calls C + + compiler




the main differences between GCC and g++

1. For *.c and *.cpp files, gcc is compiled as C and CPP files respectively (c and CPP are not the same syntax strength)


2. For *.c and *.cpp files, g++ is Unified as CPP file compilation


3. When compiling files using g++, g++ automatically links the standard library STL, and GCC does not automatically link the STL


4. GCC when compiling c file, can use the predetermined macros is relatively few


5. When compiling CPP files, gcc/g++ when compiling C files and CPP files (when both GCC and g++ invoke the CPP file compiler), some additional macros are added as follows:


#define __GXX_WEAK__ 1#define __cplusplus 1#define __deprecated 1#define __gnug__ 4#define __EXCEPTIONS 1#define __privat e_extern__ extern

6. When compiling C + + files with gcc, parameter –lstdc++ is required in order to be able to use STL, but this does not represent gcc–lstdc++ and g++ are equivalent , and they're not just the difference.




Main Parameters

-g-turn on debugging (so GDB gives morefriendly output)

-wall-turns on most warnings

-O Or-o2-turn on optimizations

-O <name>-Name of the output file

-c-output an Object file (. O)

-i<include path>-Specify an Includedirectory

-l<library path>-Specify a libdirectory

-l<library>-Link with librarylib<library>.a

Use example: g++-ohelloworld-i/homes/me/randomplace/include HelloWorld. C


Duanxx C + + learning: gcc and g++

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.