Reputation: 291
Right now I'm working on some practice questions for a computer science course. One of the ones I've encountered involves makefiles and goes as follows:
You are working in a directory which contains the following files and no others:
makefile myscript one.c three.c two.h
------------------------------------
Here are the contents of makefile:
CC=gcc
CFLAGS=-ansi
myprogram: two.o three.o
gcc -o myprogram two.o three.o
two.c: one.c myscript
sed -rf myscript one.c >| two.c
three.o: two.h
------------------------------------
1.) If you type `make myprogram` in this directory, what commands will be run?
2.) After the commands are run, what new files will have been created?
3.) After this, you edit `two.h` (and no other files) and then type make `myprogram` again. What
commands will be run?
So from my limited understanding of Makefile
I know that for 1.)
the commands to be run would be gcc -o myprogram two.o three.o
because myprogram
does not exist in the directory. However, two.o
and three.o
do not exist either... This is where I get a bit lost, would I then run the command beneath two.c: one.c myscript
because it does forced overwrite? Any help is greatly appreciated, thanks!
Upvotes: 1
Views: 52
Reputation: 28828
Some make utilities have an option to display the commands that would be run, but without running them. For example for Linux and Unix make it's -n, --just-print, --dry-run, --recon. For Microsoft nmake, the option is /n.
If a script or batch file is involved, it could be more difficult.
Upvotes: 0
Reputation:
three.o
There's a specific rule for three.o
, so that explains where that three.o
comes from.
two.o
make
has lot of implicit rules: it knows (guesses) that if it finds a *.c
file, it should compile it to an *.o
file if there's no specific rule for it, or even go one step further and create an a.out
/executable file.
These implicit rules will work in the case of two.o
, which has no specific rule, but make
can deduce the rule from two.c
.
three.o
(reprise)
make
can also make a good guess what the source file should be if it's not given: in the case of three.o
, it will try and match that with three.c
, which is the logical source file for that object file. (two.h
is just an additional dependency).
If you really want to know, create the exercise. This may be a tad difficult because of myscript
and the sed
command, but keep in mind that you can essentially create nearly empty source files (include guards in the header file would still be a good idea, so it can't accidentally get included twice).
Then, you could run make
with the -d
(debug) flag, or with the -n
(no-op flag; don't do anything). The just go through steps 1, 2 and 3.
Of course, doing all this may show you what happens, but that doesn't have to mean you understand it. Combining this with what you know about Make, the above in this answer and the results you're getting from running the actual experiment should get you a long way though.
Upvotes: 3