Question

I'm working through a text on linking, and wanted to work along with some examples in said text.

To better understand whats going on when I invoke the gcc driver, I was looking into doing all the compilation old-school by hand;

  • preprocessing using cpp
  • compiling with cc1
  • assembling with as
  • linking using ld

Unfortunately, on my Mac I don't seem to be able to reference cc1directly (no listing of cc1 in man). What are my options?

Was it helpful?

Solution

Read some material about GCC internals. First the wikipage on GCC. Then, you could play with the MELT probe (you may want a Linux system to use it). Then read the GCC manual. And the GCC resource center

Use gcc -v -Wall to compile, it will show what cc1 is running (and -Wall is always useful). So try compiling with gcc -v -Wall hello.c -o helloworld.bin

the cc1 program don't sit in your PATH or in /usr/bin/ but -on my system- in /usr/lib/gcc/x86_64-linux-gnu/4.8/cc1

The command gcc -print-prog-name=cc1 will tell you which cc1 is used by your gcc. See GCC debugging options for more.

The preprocessing is now inside cc1.

The gcc program is just a driver, starting cc1 (which does most of the compiling work), then as, ld etc...

The MELT site contains some documentation, and some slides explaining GCC, which you could find interesting. MELT is a domain specific language to extend GCC.

See also the picture on http://starynkevitch.net/Basile/gcc-melt/cc1-internals.svg and the below picture

http://starynkevitch.net/Basile/gcc-melt/gcc-MELT-inside.png

picture from http://starynkevitch.net/Basile/gcc-melt/gcc-MELT-inside.png, done by me, CC BY SA

The cc1 is producing a .s assembly file; the as (started by gcc) is transforming it into .o. The ld linker (started by gcc) will produce a library or an executable.

PS. I have a Linux system, but things are very similar on MacOSX.

reference on linking

A good book about linking is Levine's Linkers & loaders book.

PS. MELT is obsolete in 2021, but I am working on the Bismon static source code analyzer and on RefPerSys (which generates C++ code).

OTHER TIPS

For reference, I installed gcc-10 with brew on my macOS (Catalina).

While cc1 is not directly accessible (via PATH), it can be found in

/usr/local/Cellar/gcc/10.2.0/libexec/gcc/x86_64-apple-darwin19/10.2.0

If you are lost, try gcc -v and from the verbose information you may find where your cc1 is.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top