Red Antigua Logo
Yet another piece of web.
Ads by Goooooogle
Search this site (by Google)
Tools    (top)
Your IP
Check a site for broken links
(W3C)

Perl modules    (top)
Tree::Numbered::Tools
(CPAN)
Perl tutorials    (top)
Perl modules
HTML::Template
CGI::Application
Mail::POP3Client
Mail::Send
MIME::Tools
Cookies with CGI::Application
Upload files with CGI::Application
Download files with CGI::Application
Redirect with CGI::Application
CPAN shell
Install DBD::mysql from the CPAN shell
Perl trim function
Validate an IP with Perl
Run suid Perl scripts under Apache
Perl taint mode
Perl date functions with Date::Calc

In Spanish
Curso de Perl

C tutorials    (top)
C - Introduction
C - Absolute beginner's Emacs
C - Examples for beginners
C - Makefile examples
C - Autotools examples
Server configurations    (top)
DNS
Apache
Apache Authentication and Access Control
mod_perl on FreeBSD
MySQL
MySQL add account
phpMyAdmin
Squid
DHCP

UNIX on Windows    (top)
Apache setup on Windows
MySQL setup on Windows
PHP setup on Windows
Perl setup on Windows
Emacs setup on Windows
UnxUtils
PuTTY
WinSCP
GIMP on Windows
MinGW - gcc on Windows
MSYS - UNIX-styled shell on Windows
msysDTK - autotools on Windows
GDB for MinGW on Windows

Misc. FreeBSD/UNIX    (top)
CD and DVD creation on FreeBSD using 'k3b' on FreeBSD
'portupgrade' on FreeBSD
'ipf' on FreeBSD
'pf' on FreeBSD
'su' on FreeBSD
Mount an ISO image under FreeBSD
Load the correct sound driver under FreeBSD without knowing what sound card you are using
Simultaneous sound channels on FreeBSD
FreeBSD network stuff
DOS-to-UNIX file conversion
favicon.ico on UNIX
Emacs tips
Sendmail tips
GKrellm
Command Line Calculator
Save multimedia streams with 'mplayer'
xargs - solution to 'Argument list too long'
Process multiple images from the command line using 'ImageMagick'
Turn the system bell off under X Windows
Process each line in an input file from the command line (or in a shell script)
How to keep a program running in the background using 'nohup'
How to remove symbolic links in the current directory using 'find' and 'rm'
How to remove Emacs backup files in the current directory and all subdirectories using 'find' and 'rm'
How to execute .profile without logging in
Configure X to handle non-English characters
How to move /var to /usr/var

Redirect a web page    (top)
Redirect to another web page
Apache redirect
C redirect
Perl redirect
PHP redirect
HTML redirect
JavaScript redirect

Javascript    (top)
Trim function
Login form
Register form
Popup window

Virus list    (top)
Latest 10 viruses - Sophos

Off topic    (top)
Personal home links

Makefile for hello.c, world.c, and main.c
Makefile for hello.c, world.c, and main.c
IndexPreviousNext
Let's split up the previous C code in 3 files (create them in a new directory), just to see how to make can build them separately:

hello.c:
char *
hello() 
{
  return "Hello";
}
world.c:
char *
world() 
{
  return "world";
}
main.c:
#include <stdio.h>

/* Prototypes. */
char *hello();
char *world();

int
main(int argc, char *argv[]) 
{
    printf("%s, %s!\n", hello(), world());
    return 0;
}
In this case, we can compile the 3 source files main.c, hello.c, and world.c separately, but we need all the object files to exist before we can link them together.
(Otherwise, linking main.o will complain about missing references to the functions hello() and world(), while hello.o and world.o both will complain about a missing main() function.)
Anyhow, if we create our Makefile correctly, we don't have to bother about this:
# The executable 'helloworld' depends on all 3 object files
helloworld: main.o hello.o world.o
        cc -o helloworld main.o hello.o world.o # Line starts with TAB!

# Build main.o (only requires main.c to exist)
main.o: main.c
        cc -c main.c # Line starts with TAB!

# Build hello.o (only requires hello.c to exist)
hello.o: hello.c
        cc -c hello.c # Line starts with TAB!

# Build world.o (only requires world.c to exist)
world.o: world.c
        cc -c world.c # Line starts with TAB!

# Remove object files, executables (UNIX/Windows), Emacs backup files, and core files
clean:
        rm -rf  *.o helloworld helloworld.exe *~ *.core core # Line starts with TAB!
This Makefile has 5 targets, helloworld, main.o, hello.o, world.o, clean.
To build the application, type
make helloworld
or (as helloworld is the first target)
make
As we see, the target helloworld has 3 prerequisites, the object files main.o, hello.o, world.o.
If the prerequisites already exist, make just links them together.
But if for example the file hello.o does not exist, make will look for the target hello.o, build it (which in this case means compiling hello.c) before it links the object files together.

To build only the object files, type
make main.o; make hello.o; make world.o
or just
make main.o hello.o world.o
To remove object files and the application executable, Emacs backup files etc., type
make clean
As we can see, the target clean has no prerequisites.
No particular file needs to exist to cleanup undesired files, which makes sense.
You can name this target whatever you want, but the common praxis is to use the name clean.
The difference between the target clean and the other targets is that no file exists (nor will be created) with the name clean.
This kind of target is called a phony target.
(As you may have noticed, the target hello in our previous example is also a phony target - there will never exist a file named hello.)
To be sure that a phony target works as expected, it should be marked as .PHONY : target.
Normally a phony target will work anyway, but if a file named clean exists, we may get unexpected results:

Before:
# Remove object files, executables (UNIX/Windows), Emacs backup files, and core files
clean:
$ touch clean
$ make clean
make: `clean' is up to date. # This is not what we want!
After:
# Remove object files, executables (UNIX/Windows), Emacs backup files, and core files
.PHONY : clean
clean:
$ touch clean
$ make clean
rm -rf  *.o helloworld helloworld.exe *~ *.core core # Yes!

Note:
make keeps track on source code timestamps and object file timestamps.
If a source code file is newer than an object file (which means it has been modified since last time it was compiled), or the object file does not exist (which means the source code was never compiled), make recompiles the source code, otherwise not.

Exercise:
  1. Make a cleanup.
  2. Build the application. Check the make output.
  3. Build the application a second time. Compare with the previous make output.
  4. Modify one of the source code files.
  5. Build the application a third time. Compare with the previous two make outputs.
IndexPreviousNext
Last modified: Wed Sep 26 16:38:31 Romance Daylight Time 2007