Difference Between G++ and Gcc

What is the difference between g++ and gcc?

gcc and g++ are compiler-drivers of the GNU Compiler Collection (which was once upon a time just the GNU C Compiler).

Even though they automatically determine which backends (cc1 cc1plus ...) to call depending on the file-type, unless overridden with -x language, they have some differences.

The probably most important difference in their defaults is which libraries they link against automatically.

According to GCC's online documentation link options and how g++ is invoked, g++ is equivalent to gcc -xc++ -lstdc++ -shared-libgcc (the 1st is a compiler option, the 2nd two are linker options). This can be checked by running both with the -v option (it displays the backend toolchain commands being run).

What's the difference between gcc and g++/gcc-c++?

gcc will compile C source files as C and C++ source files as C++ if the file has an appropriate extension; however it will not link in the C++ library automatically.

g++ will automatically include the C++ library; by default it will also compile files with extensions that indicate they are C source as C++, instead of as C.

From http://gcc.gnu.org/onlinedocs/gcc/Invoking-G_002b_002b.html#Invoking-G_002b_002b:

C++ source files conventionally use one of the suffixes .C, .cc, .cpp, .CPP, .c++, .cp, or .cxx; C++ header files often use .hh, .hpp, .H, or (for shared template code) .tcc; and preprocessed C++ files use the suffix .ii. GCC recognizes files with these names and compiles them as C++ programs even if you call the compiler the same way as for compiling C programs (usually with the name gcc).

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

For example, to compile a simple C++ program that writes to the std::cout stream, I can use either (MinGW on Windows):

  • g++ -o test.exe test.cpp
  • gcc -o test.exe test.cpp -lstdc++

But if I try:

  • gcc -o test.exe test.cpp

I get undefined references at link time.

And for the other difference, the following C program:

#include <stdlib.h>
#include <stdio.h>

int main()
{
int* new;
int* p = malloc(sizeof(int));

*p = 42;
new = p;

printf("The answer: %d\n", *new);

return 0;
}

compiles and runs fine using:

  • gcc -o test.exe test.c

But gives several errors when compiled using:

  • g++ -o test.exe test.c

Errors:

test.c: In function 'int main()':
test.c:6:10: error: expected unqualified-id before 'new'
test.c:6:10: error: expected initializer before 'new'
test.c:7:32: error: invalid conversion from 'void*' to 'int*'
test.c:10:9: error: expected type-specifier before '=' token
test.c:10:11: error: lvalue required as left operand of assignment
test.c:12:36: error: expected type-specifier before ')' token

Difference between CC, gcc and g++?

The answer to this is platform-specific; what happens on Linux is different from what happens on Solaris, for example.

The easy part (because it is not platform-specific) is the separation of 'gcc' and 'g++':

  • gcc is the GNU C Compiler from the GCC (GNU Compiler Collection).
  • g++ is the GNU C++ Compiler from the GCC.

The hard part, because it is platform-specific, is the meaning of 'CC' (and 'cc').

  • On Solaris, CC is normally the name of the Sun C++ compiler.
  • On Solaris, cc is normally the name of the Sun C compiler.
  • On Linux, if it exists, CC is probably a link to g++.
  • On Linux, cc is a link to gcc.

However, even on Solaris, it could be that cc is the old BSD-based C compiler from /usr/ucb. In practice, that usually isn't installed and there's just a stub that fails, wreaking havoc on those who try to compile and install self-configuring software.

On HP-UX, the default 'cc' is still a K&R-only C compiler installed to permit relinking of the kernel when necessary, and unusable for modern software work because it doesn't support standard C. You have to use alternative compiler names ('acc' IIRC). Similarly, on AIX, the system C compiler goes by names such as 'xlc' or 'xlc32'.

Classically, the default system compiler was called 'cc' and self-configuring software falls back on that name when it doesn't know what else to use.

POSIX attempted to legislate its way around this by requiring the programs c89 (originally) and later c99 to exist; these are the compilers compatible with the ISO/IEC 9899:1989 and 9899:1999 C standards. It is doubtful that POSIX succeeded.


The question asks about the differences in terms of features and libraries. As before, the answer is platform specific in part, and generic in part.

The big divide is between the C compilers and the C++ compilers. The C++ compilers will accept C++ programs and will not compile arbitrary C programs. (Although it is possible to write C in a subset that is also understood by C++, many C programs are not valid C++ programs). Similarly, the C compilers will accept C programs and will reject most C++ programs (because most C++ programs use constructs not available in C).

The set of libraries available for use depends on the language. C++ programs can usually use C libraries on a given platform; C programs cannot usually use C++ libraries. So, C++ has a larger set of libraries available.

Note that if you are on Solaris, the object code produced by CC is not compatible with the object code produced by g++ -- they are two separate compilers with separate conventions for things such as exception handling and name mangling (and the name mangling is deliberately different to ensure that incompatible object files are not linked together!). This means that if you want to use a library compiled with CC, you must compile your whole program with CC. It also means that if you want to use one library compiled with CC and another compiled with g++, you are out of luck. You have to recompile one of the libraries at least.

In terms of quality of assembler generated, the GCC (GNU Compiler Collection) does a very good job. But sometimes the native compilers work a bit better. The Intel compilers have more extensive optimizations that have not yet been replicated in GCC, I believe. But any such pontifications are hazardous while we do not know what platform you are concerned with.

In terms of language features, the compilers all generally hew fairly close to the current standards (C++98, C++2003, C99), but there are usually small differences between the standard language and the language supported by the compiler. The older C89 standard support is essentially the same (and complete) for all C compilers. There are differences in the darker corners of the language. You need to understand 'undefined behaviour', 'system defined behaviour' and 'unspecified behaviour'; if you invoke undefined behaviour, you will get different results at different times. There are also many options (especially with the GCC) to tweak the behaviour of the compiler. The GCC has a variety of extensions that make life simpler if you know you are only targetting that compiler family.

What's the different about gcc and gcc++?

From the "Programming Languages Supported by GCC" page:

The abbreviation GCC has multiple meanings in common use. The current official meaning is “GNU Compiler Collection”, which refers generically to the complete suite of tools. The name historically stood for “GNU C Compiler”, and this usage is still common when the emphasis is on compiling C programs. Finally, the name is also used when speaking of the language-independent component of GCC: code shared among the compilers for all supported languages.

And regarding the difference between the gcc and g++ commands, see the "GCC Command Options" page:

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. See Compiling C++ Programs, for information about the differences in behavior between gcc and g++ when compiling C++ programs.

and from "Compiling C++ Programs"

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

the difference between g++ and gcc [duplicate]

Both are provided by the GCC toolchain, and both are [wrappers around] compiler front-ends, but they are not the same thing:

  • gcc compiles C;
  • g++ compiles C++.

The C++ standard library symbols, and various other symbols required by the C++ runtime in order to support your C++ code, are only linked in with the latter (by default).


i just have a simple c main program and have a class, VoronoiDiagramGenerator.cpp and VoronoiDiagramGenerator.h is the class defination

I guess you mean a simple C++ program. C is not C++, and C++ is not C. They are two different languages.

why use gcc and g++ compiler drivers for c and c++

The only differences between gcc and g++ are that:

  • when the driver is used to invoke the linker, g++ causes libstdc++ to be linked as part of "stdlibs", while gcc will link only libc.
  • g++ will compile .c, .h and .i files as C++ unless the -x option is specified.

Both drivers will compile C or C++ depending on either the filename extension, or command-line switches. If you invoke the compiler-driver for compilation only and invoke the linker (ld) directly, using gcc or g++ -x, it makes no difference which you use.

Equally, if you invoke the gcc driver for C++ code and explicitly link stdlibc++ it also makes no difference - so long as your crt0.o is not C-only - a C++ runtime start-up must invoke global static constructors before main()) - this is likely to already be the case.

The definitive word from the documentation:

3.3 Compiling C++ Programs

C++ source files conventionally use one of the suffixes ‘.C’, ‘.cc’, ‘.cpp’, ‘.CPP’, ‘.c++’, ‘.cp’, or ‘.cxx’;
C++ header files often use ‘.hh’, ‘.hpp’, ‘.H’, or (for shared
template code) ‘.tcc’; and preprocessed C++ files use the suffix
‘.ii’. GCC recognizes files with these names and compiles them as C++
programs even if you call the compiler the same way as for compiling C
programs (usually with the name gcc).

However, the 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. On many systems, g++ is also installed with the
name c++.

When you compile C++ programs, you may specify many of the same
command-line options that you use for compiling programs in any
language; or command-line options meaningful for C and related
languages; or options that are meaningful only for C++ programs. See
Options Controlling C Dialect, for explanations of options for
languages related to C. See Options Controlling C++ Dialect, for
explanations of options that are meaningful only for C++ programs.

If you want to use just one, I suggest you use gcc and separately invoke the linker or explicitly link -libstdc++. That way the compilation mode will be dependent on the filename extension. Using g++ -x to compile C code is just going to cause confusion.

What’s the difference between 'gcc -g' and 'gcc -g3'?

According with the documentation:

-glevel

Request debugging information and also use level to specify how  much information. The default level is 2.

Where level 3 means:

Level 3 includes extra information, such as all the macro definitions present in the program. Some debuggers support macro expansion when you use -g3.

gcc: what's the difference between -g and -ggdb?

The compiled program should be stored at some where, for the debuggers (GDB here) to look into it and follow.

This is technically called as "Debugging data format".

Some universaslly accepted debugging data formats are (there are some more):

COFF - Common Object File Format - on UNIX like systems.

PE/COFF - Portable Executable - pretty famous on windows, usually dotnet uses it.

DWARF - popular on UNIX like systems.

etc..

The GDB options you asked in your question are related to generating these debugging data formats.

Based on documenation the funcdamental difference is:
option "g" provides the debugging data in particular system's native format.
option "-ggdb" is bit rich and expensive. provides the debugging data in most expressive format available.

g++ and c++ compiler

This is typical Ubuntu symlink mayhem.

If you ls -l /usr/bin/c++, you will see it is actually a symbolic link. to:

/etc/alternatives/c++

Which in turn, is also a symbolic link to:

/usr/bin/g++

So, on Ubuntu systems, c++ is g++. The reasoning behind the link indirection is that there are multiple packages that could provide a c++ compiler (such as different versions of g++). You'll see this a lot on Ubuntu. For example, qmake is a link to a file in /etc/alternatives, which is (on my system) a link back to /usr/bin/qmake-qt3.



Related Topics



Leave a reply



Submit