CSC161 2011S Imperative Problem Solving

Reading: Make

Skim: Managing Projects with GNU Make, Chapters 1 and 2 http://oreilly.com/catalog/make3/book/index.csp

Please submit a question by 9 p.m. on Monday, 4 April 2011


Some Basics

Of all the topics covered in Chapter 2, can you give a list of which ones we will be covering in more detail, and which ones (if any) are less important (for the purposes of this class)?

You should know how to build a readable Makefile for a group of .c and .h files that can be combined in multiple ways. In order to do that, you should know about the form of rules, user-defined variables, standard variables for the C rules, automatic variables, and the basic targets of default, clean, package, and test.

Chapter 2 seemed to get more and more confusing as it went on. Will we be putting each of the make methods into practice in labs and future homework assignments?

We will put a selected subset into use.

What are some rules that you use more often? Some suggestions for useful ones that we should memorize?

My most frequent C rule has the following form:


target: file1.o file2.o file3.o
        $(CC) $(CFLAGS) ($LDFLAGS) -o $@ $^

Of course, I should probabily use $(LINK.o) or something similar.

I have been using the format "target: prereq1 prereq2 prereqn" in my makefile without using any commands underneath. From the reading it seems that giving a command is an important part of using make. How am I using make to compile things when I don't actually include a command to compile?

Because Make was first designed to work with C, it's pretty smart about figuring out the commands for target/prereq sets that involve C files. We've seen this already in that it knows how to make a .o file from a .c file, even if you don't give a rule.

What are CPPFLAGS?

In chapter 2 on page 20 on the top of the page, what exactly does CPPFLAGS = -I include do?

CPPFLAGS are the flags used by the C preprocessor. As you may recall, one thing the preprocessor does is substitute in the text of #include directives. The -I include says to look in the directory called include when processing those directives. I can do an example if you'd like

What the difference between clean and distclean?

In the model they gave us, clean will remove the .o files (and maybe the Emacs backups) while distclean will remove everything other than the basic files.

Will make always work in the current directory? (Can you tell make to look at a makefile in a separate directory?)

You can specify a makefile with make -f makefile. You can even have Make switch directories during the process.

Why are pattern rules used for?

When you have lots of rules that look identical or when you may not be able to predict all of the files that the Makefile will be used with, pattern rules provide a way to provide the general-purpose solution. For example, we know that whenever someone has a .c file and wants a .o file, we should compile with $(CC), so we write


%.o: $.c
       $(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $<

How does make function without makefiles?

As the reading suggests, there is a huge collection of default rules that make uses.

When we started using make, we just used calls like make name without a makefile, but in the reading, make by itself was used. Can you explain the difference?

I understand how Makefiles work but I still do not feel confident enough to create a Makefile. Could you also explain what exactly does $ make do? Does it simply return what's in the makefile? Does it run the makefile?

If you have a Makefile, typing make will build the first thing in the Makefile. (I usually specify that thing as default.) The Makefile provides information on how to build that thing.

Do all languages use the sample Makefile process, with the Makefile being based on the operating system? Or do different languages/compilers/operating systems lead to different Makefiles?

Make is primarily a Linux/Unix application. (That means it's also available on Mac OS X.) It can be used with any language and compiler.

The book offers a command for checking the default rules used by make, but how would you go about modifying these rules? Would these changes be reflected in the display of default rules, or are those static?

Any rule you put in your own Makefile overrides the default.


Variables

What do they mean by expanding variables?

To be honest, I am not entirely sure as to what the purpose of variables are or what they do. Do they grab something from a source with the variable name?

A variable in a Makefile can store any piece of text you'd like: a file name, a list of file names, a command, a parameter to a command, whatever. As we do in most programs, we use variable names in Make to make things easier to read and to eliminate duplication.


Phony Targets

Could you clarify the use of Phony Targets?

Phony confused me. Can you clarify its meaning?

I do not really understand the reason for having Phony Targets.

Sometimes we use Make to help us remember long sequences of commands that we use for their side effect, rather than to build a particular file. To make it clear to Make that each time we enter a target, we still want the commands executed, we use the .PHONY notation.

Can you go over a few examples of the proper applications of phony and empty targets?

Sure.

How are "phony" targets created? Are they just leftovers that have not yet been deleted after condensing or renaming files, or do they somehow create themselves, like temporary files?

Phony targets are specified by the designer of the Makefile. They are never made, and so are not leftovers. In effect, phony targets are a way of specifying commands.


Suffixes

What are .l, .a, and .so files?

.a - a library (archive). .l - a Lex or Flex source file. .so - a shared object (easily reloadable object code, like a dll in Windows).

When do I know when to use which . to use? like when to use .h? .o? etc?

For now, you mostly care about .c (C source code), .h (header files, intended to give information about libraries and other C files), and .o (partially compiled C code). When we need others, I'll tell you about them.

Will we be using the other different suffixes while in this class? If so, would there be like a library to say what each does?

You're in the Internet generation. You should be able to Google it. For example, Wikipedia has a list of suffixes at http://en.wikipedia.org/wiki/List_of_file_formats_%28alphabetical%29.


Libraries

Can you go over using libraries in make?

It's more an issue of using libraries in C. For now, we've been treating .o files as libraries. However, formally libraries are collections of .o files that have expreienced some additional processing and are put in a place that they can be conveniently found. Libraries are named libsomething.a. They are linked with your code with -lsomething.

Are the order of libraries on the command line important?

It depends a bit on the compiler/linker. While the order should not be important, sometimes the linker will only grab a function from a library if it knows it needs it. Hence, if one library depends on another, the dependent library should come first.

What is the difference between a library and a normal directory?

A library is a collection of mostly-compiled code. A directory is a collection of files.


VPATH

What are VPATH and vpath?

Locations for Make to look when it looks for files.

If a file of the same name exists in multiple places in the VPATH list, what happens?

The first instance of the file is used.

Are VPATH and vpath the same?

No. One is used to give a general list of directories. The other is used for associated directories with particular suffixes.


Random Linux stuff

Is there a library to say what the letter after the dashes mean? like -a, -c, -I..etc

The man page will tell you. Most of those are flags for the C compiler.

On page 12, what are Bourne shells?

The shell is the thing you use to interact with the Unix/Linux system. (You may recall that we use the bash shell.) Originally, there were two shells, the basic shell (sh) and the C shell (csh), which had a more C-like syntax for commands. The Bourne shell was a shell designed by a person named Bourne. We use the Bourne-again shell, an extension of that shell.

How do the Flex and Bison programs mentioned in Chapter 1 work, and what is their significance in the make program?

Flex is a program that does lexical analysis. It provides a simple mechanism for looking for patterns in files. It's used, for example, to find the keywords in a C program. Bison is a program used to parse and respond to more complex structures in text files. Many compilers are written with Bison (which is based on YACC, yet another compiler compiler).

What is the df seen on page 14?

df gives you information on disk usage.

What are RCS, SCCS, and CVS?

Revision Control System, Source Code Control System, and Concurrent Versioning System. Systems for keeping track of changes to your source code.

What is the difference between cc and gcc when compiling a program?


Miscellaneous

What happened to CFLAGS=-Wall?

That's one of the make variables I recommend you use.

Why are these useful .INTERMEDIATE, .SECONDARY, .PRECIOUS, and .DELETE_ON_ERROR?

In larger systems, it helps to be able to specify exactly what Make should do in certain situations.

Isn't dependency bad? It plays a big part in make, and it seems that this has its drawbacks.

It is unavoidable that things depend on other things. .c files depend on the files they include, particular .h files. (That is, if you change the included file, the .c file has implicitly changed.) Programs depend on their source code. (That is, if you change the source code, you need to recompile the program.) So, no, I would not say that it's bad.

However, cyclic dependencies are bad and should be avoided.

Why are there so many rules? Shouldn't there be a more simple way to do it all?

Make is designed to be used for almost any kind of project one might envision. Hence, it needs to support a large variety of activities. My guess is that as people found new things to do, someone added a feature to make that easier. Most of us learn a subset of make appropriate for the task at hand.


Questions That I Can't or Won't Answer

When compiling to a windows .exe is there something equivalent to the GNU make?

I don't use Windows, and so don't know. However, I do know that you can use Cygwin to have GNU make on Windows.

Why does the makefile compile in 4 programs? 4 executables?

I'm not sure what you are asking.

Okay, I don't understand rebuilds that well. Can you explain further?

I guess I don't understand either. In what context did you see this term?

Can you go over these rules: Explicit rules, pattern rules, implicit rules, static pattern rules, suffix rules.

I will go over the ones I consider important.

Will we be doing any of the more complex looking programming stuff in makefiles from chapter 2 in this course?

I don't know what you think the more complex looking programming stuff is. We'll certainly take advantage of wildcards and the ugly automatic variables.

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Tue Apr 5 10:44:12 2011.
The source to the document was last modified on Tue Apr 5 10:44:10 2011.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CSC161/2011S/Readings/make.html.

Samuel A. Rebelsky, rebelsky@grinnell.edu