Difference Between Cc, Gcc and G++

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

Invoking GCC as cc versus gcc

For grins, I just traced down how argv[0] is used from within gcc (main.c -> top_lev.c -> opts.c -> langhooks.c) and it appears that argv[0] is currently used for nothing more than giving malloc something to report when it fails. There doesn't appear to be any behavior change if argv[0] is anything other than gcc.

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

why use g++ instead of gcc to compile *.cc files?

It depends on what exactly you changed in the makefile. gcc / g++ is really just a front-end driver program which invokes the actual compiler and / or linker based on the options you give it.

If you invoke the compiler as gcc:

  • it will compile as C or C++ based on the file extension (.c, or .cc / .cpp);
  • it will link as C, i.e. it will not pull in C++ libraries unless you specifically add additional arguments to do so.

If you invoke the compiler as g++:

  • it will compile as C++ regardless of whether or not the file extension is .c or .cc / .cpp;
  • it will link as C++, i.e. automatically pull in the standard C++ libraries.

(see the relevant bit of the GCC documentation).


Here's a simple program which detects whether or not it has been compiled as C or C++.

(It makes use of the fact that a character constant has the size of an int in C, or a char in C++. sizeof(char) is 1 by definition; sizeof(int) will generally be larger - unless you're using an obscure platform with >= 16-bit bytes, which you're probably not.)

I've called it test.c and copied it as test.cc as well:

$ cat test.c
#include <stdio.h>

int main(void)
{
printf("I was compiled as %s!\n", sizeof('a') == 1 ? "C++" : "C");
return 0;
}
$ cp test.c test.cc
$

Compiling and linking test.c with gcc, and test.cc with g++, works as expected:

$ gcc -o test test.c
$ ./test
I was compiled as C!
$ g++ -o test test.cc
$ ./test
I was compiled as C++!
$

Compiling and linking test.cc with gcc doesn't work: it compiles the code as C++ because the file ends in .cc, but fails at the link stage:

$ gcc -o test test.cc
/tmp/ccyb1he5.o:(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
$

which we can prove by separately compiling with gcc, and linking with g++ (to pull in the right libraries):

$ gcc -c test.cc
$ g++ -o test test.o
$ ./test
I was compiled as C++!
$

...gcc has compiled the code as C++ rather than C, because it had a .cc file extension.

Whereas g++ does not compile .c files as plain C:

$ g++ -o test test.c 
$ ./test
I was compiled as C++!
$

Difference gcc and g++ as linker?

Any of the GCC language frontends (gcc, g++, gfortran, etc.), when
invoked for linkage will delegate to the system linker, ld, and will
silently pass to the linker the boilerplate options for the language
associated with that frontend (C,C++,Fortran, etc).

What those boilerplate options are is determined by the builder
of the toolchain (typically, your distro). The salient difference is
between the standard libraries that are linked for the different
languages. You can inspect the differences in full by directing the
frontend to request verbose mode from the linker: pass -Wl,-v.

Why does Code::Blocks by default use g++ as the linker even for C language
projects? It's the simplest default: the resulting linkage options will work
for exclusively C++ projects of course; they'll also work for exclusively
C language projects (albeit with some redundancy), and they'll also work for mixed C/C++ language projects.

This decision does have a flaw if you program in C and not all all in C++
and therefore have - economically - installed gcc but not g++ on your
system. Then you'll find that out-of-the-box your Code::Blocks C projects can't link
because you haven't got the default linker. But you can fix this in
a jiffy by changing the Linker for Dynamic libs from g++ to gcc
in the global Compiler settings of the toolchain.

ming32-make is using g++ instead of cc or gcc to compile a .c source file

mingw32-make (correctly IMO) treats the file system as case insensitive, so foo.c and FOO.C refer to the same file.

Your makefile lacks any explicit rule for your hello_world goal, so built-in implicit rules must be used; it seems that the COMPILE.C rule is being matched for hello_world.c, before the COMPILE.c rule; COMPILE.C is a default rule for compiling C++, so the appropriate compiler is used.

As you note, setting CXX = gcc is kludgy, and fundamentally wrong; it will break if your project expands to requiring mixed C and C++ components. A more appropriate solution is to provide your own rule ... either an explicit rule, or a pattern rule, to direct mingw32-make to use $(CC) for compiling *.c (and *.C) files, so that the built-in default rules will not apply. You can then keep CXX = g++ for compiling any *.cpp files, (recommended for compatibility with MSVC convention), which your project may include.



Related Topics



Leave a reply



Submit