Compiling, make & lint

Note: You need to know all of this material for exams.


A simple C program: First look at the standard "Hello World" program.


Compiling a C program: On Linux and on the Sun machines there are two compilers: cc and gcc.

Usually executablename and sourcename are the same. Thus if the above program were in a file called myhello.c,

would create a file called myhello. You could run the program just by invoking its name:

In case your C program uses a mathematical function, then on many systems you will need the additional -lm option to force the math library to be included in the compilation.


Using lint: The C compiler is not very picky about checking for correctness. The lint program is used to pick out the fuzzy parts of your code. To run it, execute:

On the Sun systems, you should use the lint option: -Xarch=v9. The following shows the command and the output:

I've been trying the following options: -err=warn -Ncheck=%all -Nlevel=4 -Xarch=v9

Comments about the lint output:

We can fix this program as follows:

We can run lint on this:


Using make: make is a utility that can help you avoid doing a lot of typing when you want to compile your programs. More importantly, it will help you avoid serious errors. It may not seem to useful at this point, but when your programs become more complicated, you will want to use it. In this course, you must use it! Only the very simplest aspects of the make utility will be discussed here.

When you execute make, it looks for a file called Makefile in the current directory. This Makefile describes the dependency relationships that exist between various program modules.

The dependencies have the following form:

The first line is called a dependency and the second line is called a rule. Rule lines must start with a tab character. There may be more than one rule line for each dependency.

The rules are invoked if any of the components are newer than the target, or if the target does not exist. Here is a simple Makefile for myhello (the second line starts with a TAB):

If myhello does not exist, then running make will compile myhello.c:

If we do it again:

By default, make will look only at the first target in the makefile. If you give it an argument, it will look at the corresponding target.

Consider the following makefile:

Now typing make will do the same thing as before, but typing make lint will cause the rule for lint to be used. Since the file lint does not exist, the rule will always be executed:

Sometimes you want to remove the executable and start again. Here is another useful Makefile. (Remember that each of the "rule" lines must start with a TAB.)

Now we can run make some more:


Discussion of make: The make utility is complex, with numerous online expositions, such as GNU Make reference.

make does more for you than might be obvious from the simple presentation above. It keeps track of the time each component has been altered. Then if it is properly set up, make will decide whether or not a C source file needs to be recompiled, based on information in a database about whether the source file has been modified since the last compile. Functionality such as this is also provided by modern IDEs (Interactive Development Environments) for languages (like JBuilder for Java or Visual Studio), but the much older make has been used very successfully for 30 years and is still in wide use by the Unix community.

The make utility also keeps a programmer from making mistakes. For example, beginning programmers (and others) who type the compile command directly might make the mistake:

In this case the compiled file will overwrite the original source, so that the source will be lost (unless you have another copy). A properly and carefully written Makefile will keep you from making this and other errors.


Copyright © 2011, Neal R. Wagner. Permission is granted to access, download, share, and distribute, as long as this notice remains.