tags:

views:

5994

answers:

5

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

+33  A: 

gcc is 'Gnu Compiler Collection'. If you pass it a C++ file, it will invoke the C++ compiler ('g++') behind the scenes.

gcc is essentially the frontend for several compilers and the linker too.

Edit: As several people pointed out, this doesn't mean that 'gcc' and 'g++' are interchangeable for c++ files: gcc will invoke g++ with different arguments to what you'd get if you'd invoked g++ directly.

Beat me to it, you posted while I was typing.
Unkwntech
@Brian: I spend several years thinking that 'gpp' was the C++ backend...
gcc used to mean 'Gnu C Compiler'. That eventually changed when more languages were added.
Ferruccio
That's not entirely true. If you just run gcc on a C++ file, it will not 'just work' like g++ does. That's because it won't automatically link to the C++ std library, etc. Just use g++ for C++ files.
jonner
The actual compiler is "cc1" for C and "cc1plus" for C++; both gcc and g++ are drivers (which call the preprocessor/compiler/assembler/linker as needed).
CesarB
+8  A: 

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).

Nils Pipenbrinck
+1  A: 

The only notable difference is that i you pass a .c to gcc it will compile as C, whereas g++ will always treat it as C++

njsf
+3  A: 

Although the gcc and g++ commands do very similar things, g++ is designed to be the command you'd invoke to compile a C++ program; it's intended to automatically do the right thing.

Behind the scenes, they're really the same program. As I understand, both decide whether to compile a program as C or as C++ based on the filename extension. Both are capable of linking against the C++ standard library, but only g++ does this by default. So if you have a program written in C++ that doesn't happen to need to link against the standard library, gcc will happen to do the right thing; but then, so would g++. So there's really no reason not to use g++ for general C++ development.

zaphod
+24  A: 

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 less 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
Martin York
You can link the std C++ library in `gcc` by passing `-lstdc++` parameter.
Denilson Sá