5

I'm new to GNU and GCC, sorry if my question sounds dumb.

We know that GCC stands for GNU Compiler Collection, so I think gcc is just a compiler (from a compiler collection).

But I also read that gcc is a compiler driver which contains Pre-processor (cpp), Compiler (cc1), Assembler (as) and Linker (ld).

So it looks that GCC is not a compiler, but why wiki says:

"GCC is a key component of the GNU toolchain and the standard compiler for most projects related to GNU and Linux"

and what does "1" means in cc1, why it is called cc1, not cc2, cc3 ...etc?

  • 2
    You might find this link useful [Unix & Linux Stack Exchange: Relationship between cc1 and gcc?](https://unix.stackexchange.com/questions/77779/relationship-between-cc1-and-gcc) – Thomas Jager Aug 24 '20 at 12:49
  • There's `cc1plus` for C++ compiler. – John Park Aug 24 '20 at 13:05
  • gcc is a **collection** of **compilers**, as indicated in http://gcc.gnu.org: "The GNU Compiler Collection includes front ends for C, C++, Objective-C, Fortran, Ada, Go, and D..." – rici Aug 24 '20 at 15:24

1 Answers1

2

In most cases you (a little inaccurately) call gcc the compiler. A reason is that you can run the whole tool chain, at least for simple projects, with a single gcc command. Let's say you have this main.c

// main.c
#include <stdio.h>
int main(void)
{
    printf("Hello, world!\n");
}

and compile it with

gcc main.c

Then everything you mentioned, cpp, cc1, as and ld will be involved in creating the executable a.out. Well, almost. cpp is old and newer versions of the compiler has the preprocessor integrated.

If you want to see the output of the preprocessor, use gcc -E main.c

As I mentioned, the preprocessor and the compiler is integrated nowadays, so you cannot really run cc1 without the preprocessor. But you can generate an assembly file with gcc -S main.c and this will produce main.s. You can assemble that to an object file with gcc -c main.s which will produce main.o and then you can link it with gcc main.o to produce your final a.out

https://renenyffenegger.ch/notes/development/languages/C-C-plus-plus/GCC/cc1/index (Emphasis mine)

cc1 is also referred to as the compiler proper.

cc1 preprocesses a c translation unit and compiles it into assembly code. The assembly code is converted into an object file with the assembler.

Earlier versions of cc1 used /usr/bin/cpp for the preprocessing stage.

https://renenyffenegger.ch/notes/Linux/fhs/usr/bin/cpp (Emphasis mine)

The preprocessor.

cpp is not bo be confused with c++.

The preprocessor is concerned with things like

  • macro expansion
  • removing comments
  • trigraph conversion
  • escaped newline splicing
  • processing of directives

Newer version of gcc don't invoke /usr/bin/cpp directly for preprocessing a translation unit. Rather, the preprocessing is performed by the compiler proper cc1.

I would almost consider this as a dup of this, but it's impossible to create cross site dupes. Relationship between cc1 and gcc?

Related: 'Compiler proper' command for C program

and what does "1" means in cc1, why it is called cc1, not cc2, cc3 ...etc?

Don't know. My first guess was that they just added a 1 to cc which was and is the standard compiler on Unix (excluding Linux) systems. On most Linux system, cc is just a link to gcc. But another good guess is that it stands for the first phase in compilation. Have not found a good source though.

klutt
  • 30,332
  • 17
  • 55
  • 95
  • what does "1" means in cc1? –  Aug 24 '20 at 13:11
  • @amjad Don't know. Possibly just something they added to make it different from the old `cc` compiler – klutt Aug 24 '20 at 13:12
  • @amjad the first phase of the C compiler – David Ranieri Aug 24 '20 at 13:16
  • @DavidRanieri Do you know or are you guessing? – klutt Aug 24 '20 at 13:17
  • @DavidRanieri Yes, I've seen that page, but it just states that `cc1` is the first phase, not that that is the meaning of the `1`. – klutt Aug 24 '20 at 13:18
  • @DavidRanieri I'm about to edit the answer, but I want to know if I should write that I believe it is that way or if it is that way. :) – klutt Aug 24 '20 at 13:19
  • As far as I know, cc1, cc2 and so on are scripts, cc1 is the first one to be called. – David Ranieri Aug 24 '20 at 13:21
  • @DavidRanieri Ok, I'll do some research later. Thanks. – klutt Aug 24 '20 at 13:23
  • @ David Ranieri whats the second phase of the C compiler? –  Aug 24 '20 at 13:25
  • @amjad https://medium.com/@danielavasquez_77768/the-four-stages-of-compilation-c-programming-and-gcc-compiler-9faadf2e273a – David Ranieri Aug 24 '20 at 13:47
  • You can also use `gcc main.c -o foo.out` to have it invoke the linker directly, without worrying about object files. – Lundin Aug 24 '20 at 13:50
  • @DavidRanieri so stage one is what cpp get involved, isn't that more suitable to call it cc2 since the second stage is the actualy stage that compile code into assembly? –  Aug 24 '20 at 13:52
  • @amjad As I wrote, cpp does not get involved anymore. It's integrated in cc1. – klutt Aug 24 '20 at 14:46
  • You'd have to ask RMS why he chose that name (if he still remembers) but my guess is that he just didn't want it to be confused with the standard executable `cc` (which was the name of the installed C compiler on many Unix installations). The driver gcc looks for the programs it needs to invoke (originally `cpp`, `cc1`, `as` and `ld`, but now `cpp` and `as` are no longer required) by searching in various places; if it can't find them anywhere else, it searches `$PATH`. So calling the C front-end `cc1` instead of `cc` avoids erroneously calling the installed compiler. – rici Aug 24 '20 at 17:35
  • There is no cc2, @DavidRanieri. – rici Aug 24 '20 at 17:37
  • @klutt could u have a look at this https://stackoverflow.com/questions/63642665/in-some-cases-the-linker-allows-multiple-modules-to-define-global-symbols-with-t –  Aug 29 '20 at 08:50