What is the difference between g++ and gcc?

后端 未结 10 2166
无人共我
无人共我 2020-11-22 00:34

What is the difference between g++ and gcc? Which one of them should be used for general c++ development?

相关标签:
10条回答
  • 2020-11-22 00:51

    GCC: GNU Compiler Collection

    • Referrers to all the different languages that are supported by the GNU compiler.

    gcc: GNU C      Compiler
    g++: GNU C++ Compiler

    The main differences:

    1. gcc will compile: *.c\*.cpp files as C and C++ respectively.
    2. g++ will compile: *.c\*.cpp files but they will all be treated as C++ files.
    3. Also if you use g++ to link the object files it automatically links in the std C++ libraries (gcc does not do this).
    4. gcc compiling C files has fewer predefined macros.
    5. gcc compiling *.cpp and g++ compiling *.c\*.cpp files has a few extra macros.

    Extra Macros when compiling *.cpp files:

    #define __GXX_WEAK__ 1
    #define __cplusplus 1
    #define __DEPRECATED 1
    #define __GNUG__ 4
    #define __EXCEPTIONS 1
    #define __private_extern__ extern
    
    0 讨论(0)
  • 2020-11-22 00:52

    “GCC” is a common shorthand term for the GNU Compiler Collection. This is both the most general name for the compiler, and the name used when the emphasis is on compiling C programs (as the abbreviation formerly stood for “GNU C Compiler”).

    When referring to C++ compilation, it is usual to call the compiler “G++”. Since there is only one compiler, it is also accurate to call it “GCC” no matter what the language context; however, the term “G++” is more useful when the emphasis is on compiling C++ programs.

    You could read more here.

    0 讨论(0)
  • 2020-11-22 00:53

    What is the difference between g++ and gcc?

    gcc has evolved from a single language "GNU C Compiler" to be a multi-language "GNU Compiler Collection". The term "GNU C Compiler" is still used sometimes in the context of C programming.

    The g++ is the C++ compiler for the GNU Compiler Collection. Like gnat is the Ada compiler for gcc. see Using the GNU Compiler Collection (GCC)

    For example, the Ubuntu 16.04 and 18.04 man g++ command returns the GCC(1) manual page.

    The Ubuntu 16.04 and 18.04 man gcc states that ...

    g++ accepts mostly the same options as gcc

    and that the default ...

    ... use of gcc does not add the C++ library. g++ is a program that calls GCC and automatically specifies linking against the C++ library. It treats .c, .h and .i files as C++ source files instead of C source files unless -x is used. This program is also useful when precompiling a C header file with a .h extension for use in C++ compilations.

    Search the gcc man pages for more details on the option variances between gcc and g++.

    Which one should be used for general c++ development?

    Technically, either gcc or g++ can be used for general C++ development with applicable option settings. However, the g++ default behavior is naturally aligned to a C++ development.

    The Ubuntu 18.04 'gcc' man page added, and Ubuntu 20.04 continues to have, the following paragraph:

    The usual way to run GCC is to run the executable called gcc, or machine-gcc when cross-compiling, or machine-gcc-version to run a specific version of GCC. When you compile C++ programs, you should invoke GCC as g++ instead.

    0 讨论(0)
  • 2020-11-22 01:06

    For c++ you should use g++.

    It's the same compiler (e.g. the GNU compiler collection). GCC or G++ just choose a different front-end with different default options.

    In a nutshell: if you use g++ the frontend will tell the linker that you may want to link with the C++ standard libraries. The gcc frontend won't do that (also it could link with them if you pass the right command line options).

    0 讨论(0)
提交回复
热议问题