Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
757 views
in Technique[技术] by (71.8m points)

macos - C compiler for mac?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...