问题
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 cc1
directly (no listing of cc1
in man
). What are my options?
回答1:
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
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.
来源:https://stackoverflow.com/questions/16694600/c-compiler-for-mac