8

I compiled a library which used g++ instead of gcc. First I thought the source code was written in C++, but I found out later that there was not any C++ code in the *.cc files.

To confirm this, I replaced the g++ in the original makefile with gcc. And I still got the correct program.

What is the explanation? It was not the first time I met such a situation.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
deepsky
  • 845
  • 1
  • 10
  • 24
  • Possible duplicate of [What is the difference between g++ and gcc?](https://stackoverflow.com/questions/172587/what-is-the-difference-between-g-and-gcc) – underscore_d Dec 05 '17 at 10:54

4 Answers4

23

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:

File test.c

#include <stdio.h>

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

Copy

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

Output:

/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++!
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Matthew Slattery
  • 45,290
  • 8
  • 103
  • 119
  • Very good answer and I have tried your test code on my machine. Still one question, as you said gcc won't succeed at the linking stage for cc files, which is proved by your test code. In my makefile the compiling uses the gcc but the linking stage uses the gcc as well: CXXLD = $(CXX) CXXLINK = $(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) \ --mode=link $(CXXLD) $(AM_CXXFLAGS) $(CXXFLAGS) $(AM_LDFLAGS) \ $(LDFLAGS) -o $@ where CXX is gcc (I replaced g++ with gcc) but it still got the correct result. – deepsky Jul 09 '11 at 02:26
  • @deepsky: If the code is compiled with the right options, it may not introduce any dependencies on the libraries that don't get linked. In my example above, the complaint is about something in the `.eh_frame` section, which is related to exception handling support: on my system I find that `gcc -fno-exceptions -o test test.cc` *does* work OK. (Running `./test` still reveals that it was compiled as C++, not C.) – Matthew Slattery Jul 09 '11 at 14:40
3

It could be that the .cc code happens to be C, but was intended to be linked into a C++ library. The internals are different.

spraff
  • 32,570
  • 22
  • 121
  • 229
1

g++ automatically links the C++ runtime library—gcc doesn't. Obviously, when it doesn't matter—then it doesn't matter, but, as already pointed out by spraff, it could be intended for future use.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
vines
  • 5,160
  • 1
  • 27
  • 49
  • 2
    Also the "linkage" is different... The C++ compiler will mangle function names in the generated code (to support overloading); the C compiler will not. – Nemo Jul 08 '11 at 16:34
  • 1
    @Nemo: even if you call `gcc` rather than `g++`, the C++ compiler is used anyway if the file extension says it's a C++ source. –  Jul 09 '11 at 10:41
-1

I don't know why they chose to use g++ instead of gcc, but I believe it shouldn't matter, as any valid C program is also valid C++.

Matt Dunn
  • 5,106
  • 6
  • 31
  • 55