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

I compiled a library that uses g ++ instead of gcc. At first I thought the source code was written in C ++, but later I found out that there are no C ++ code in the * .cc files.

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

Can anyone explain this? This is not the first time I've met such a situation.

+6
source share
4 answers

It depends on what exactly you changed in the makefile. gcc / g++ is really just a frontend program that calls the actual compiler and / or linker based on the parameters you give it.

If you call the compiler like gcc :

  • it will compile as C or C ++ based on the file extension ( .c or .cc / .cpp );
  • it will bind like C, i.e. it will not pull out C ++ libraries unless you specifically add additional arguments for this.

If you call the compiler like g++ :

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

(see the corresponding bit of the GCC documentation ).


Here is a simple program that determines if it has been compiled as C or C ++.

(It uses the fact that the character constant is int in C or char in C ++. sizeof(char) by definition 1; sizeof(int) will usually be larger - if only you use an obscure platform s> = 16-bit bytes, which you probably aren't.)

I named it test.c and copied it as test.cc :

 $ 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 binding test.c using gcc and test.cc using 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 does not work: it compiles the code as C ++, because the file ends in .cc , but does not work 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 contacting g++ (to pull into the necessary libraries):

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

... gcc compiled the code as C ++, not C, because it had the extension .cc .

While g++ does not compile .c files like regular C:

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

It is possible that the .cc code is C, but it should be linked to the C ++ library. The insides are different.

+2
source

g++ automatically binds the C ++ runtime library - gcc not. Obvoiusly, when it does not matter - then it does not matter, but, as spraff has already indicated, it can be intended for future use.

+1
source

I don’t know why they decided to use g ++ instead of gcc, but I believe that it doesn’t matter, since any valid C program is also valid C ++.

-1
source

Source: https://habr.com/ru/post/892338/


All Articles