[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Baffled by cc -o in Make manual
From: |
Sven C. Dack |
Subject: |
Re: Baffled by cc -o in Make manual |
Date: |
Tue, 1 Aug 2017 19:46:36 +0100 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.2.1 |
Hello,
this is completely normal.
The compiler calls the assembler and linker automatically when needed
and depending on the input files. One doesn't actually call the
assembler and linker manually in most cases. You can do:
cc -c prog.c -o prog.o
This will compile the file 'prog.c' into assembly code, pass it to the
assembler, which then produces the 'prog.o' file. You can also do:
cc -S prog.c -o prog.s
This tells the compiler to only produce assembly output and to write it
to 'prog.s'. When you look at it can you see the assembler instructions,
which would otherwise have been passed onto the assembler for it to
produce an object file.
The compiler simply acts as a front-end to the assembler and linker. You
can let the compiler do it all in one go where it calls the assembler
and linker to build the final program:
cc prog.c -o prog
Because programs can become quite large and managing the source requires
many files is it more convenient to split the task into multiple files:
cc -c prog1.c -o prog1.o
cc -c prog2.c -o prog2.o
cc -c prog3.c -o prog3.o
cc -o prog prog1.o prog2.o prog3.o
And let's not forget the libraries. These also are being handled through
the compiler even when it's the linker, which does the actual work:
cc prog1.o prog2.o prog3.o mylib.a -lm -lc -o prog
And so on. This concept lets you manage the development of a program in
convenient chunks and doesn't force you to keep an eye on the whole code
all the time.
This is then why it has 'make' and Makefiles. It allows you to define
how a program is being put together, how to call the compiler, which
files belong together, and so on. And because 'make' can detect when
files have changed does it make this particularly easy, because only
what has changed then needs to be recompiled and thereby saves you time.
A simple, first Makefile for the three files 'prog1.c', 'prog2.c' and
'prog3.c' could look like this:
prog1.o: prog1.c
cc prog1.c -o prog1.o
prog2.o: prog2.c
cc prog2.c -o prog2.o
prog3.o: prog3.c
cc prog3.c -o prog3.o
prog: prog1.o prog2.o prog3.o
cc prog1.o prog2.o prog3.o -o prog
When you now type 'make prog' will it check if a file by that name
exists and if it's newer than 'prog1.o', 'prog2.o' and 'prog3.o'. If so
then it knows 'prog' is up to date and doesn't need to be remade.
Otherwise will it invoke "cc prog1.o prog2.o prog3.o -o prog" to produce
a newer version.
But it doesn't stop there. Because it also has rules to make 'prog1.o'
and the other two will it first check if perhaps these are out of date
and if these need to be remade.
Thus, when one of the files 'prog1.c', 'prog2.c' or 'prog3.c' have
changed and become newer will 'make' only remake those pieces of your
program, which really require to be remade.
Once this is understood does everything else about 'make' fall into
place. It's all about breaking large work into manageable chunks and to
avoid extra work be defining clever rules, which will do all the
necessary steps for you in order to put it all together.
I hope this helps you to understand the use of 'make' and 'cc'.
Cheers,
Sven
On 01/08/17 17:32, Ray Foulkes wrote:
Hello, like many reports I suppose I have this totally wrong but the
very first example on page 4 of the make manual at
https://www.gnu.org/software/make/manual/make.pdf reads:
edit : main.o kbd.o command.o display.o \
insert.o search.o files.o utils.o
cc -o edit main.o kbd.o command.o display.o \
insert.o search.o files.o utils.o
I am totally baffled as to what cc -o is doing there. Surely it should
be ld?? and then the -o is for "output" i.e. create "edit" as an
executable.
On the other hand it continues on through the other examples of using
variables in subsequent pages so I am beginning to doubt myself.
I cannot understand how make knows to call ld if it is not explicit
somewhere in the file.
Please ignore if I am just being stupid... If so, boy, have you got me
baffled - and I have only got to page 4!
Regards, Ray Foulkes
_______________________________________________
Bug-make mailing list
address@hidden
https://lists.gnu.org/mailman/listinfo/bug-make