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 with dependencies
Makefile with dependencies
IndexPreviousNext
Let's split up the previous C code in even more files (always create them in a new directory), to see how to make treats dependencies:

hello.h:
#define HELLO "Hello"
hello.c:
#include "hello.h"

char *
hello() 
{
  return HELLO;
}
world.h:
#define WORLD "world"
world.c:
#include "world.h"

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

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

void
printhelloworld()
{
    printf("%s, %s!\n", hello(), world());
    printf("I just said %s to the %s.\n", HELLO, WORLD);
    return;
}
main.c:
/* Prototypes. */
void printhelloworld();

int
main(int argc, char *argv[]) 
{
    printhelloworld();
    return 0;
}
Even in a small project with just a few files, as the example above, it starts to be tricky figuring out the dependencies between files.
In this case gcc can help us to create targets and prerequisites:
$ gcc -MM *.c
hello.o: hello.c hello.h
main.o: main.c
printhelloworld.o: printhelloworld.c hello.h world.h
world.o: world.c world.h
Paste each line from the output into an empty Makefile (line order doesn't matter)...
main.o: main.c
printhelloworld.o: printhelloworld.c hello.h world.h
hello.o: hello.c hello.h
world.o: world.c world.h
...and specify how to compile each target...
main.o: main.c
        cc -c main.c # TAB before command!

printhelloworld.o: printhelloworld.c hello.h world.h
        cc -c printhelloworld.c # TAB before command!

hello.o: hello.c hello.h
        cc -c hello.c # TAB before command!

world.o: world.c world.h
        cc -c world.c # TAB before command!
...and finally add the helloworld and clean targets, and we're done:
# The executable 'helloworld' depends on all object files
helloworld: main.o printhelloworld.o hello.o world.o
        cc -o helloworld main.o printhelloworld.o hello.o world.o # TAB before command!

main.o: main.c
        cc -c main.c # TAB before command!

printhelloworld.o: printhelloworld.c hello.h world.h
        cc -c printhelloworld.c # TAB before command!

hello.o: hello.c hello.h
        cc -c hello.c # TAB before command!

world.o: world.c world.h
        cc -c world.c # TAB before command!

# Remove object files, executables (UNIX/Windows), Emacs backup files, and core files
.PHONY : clean
clean:
        rm -rf  *.o helloworld helloworld.exe *~ *.core core # TAB before command!
As we see in the rule of the target helloworld, the list of object files grows.
To not repeat typing the list (besides beeing good for the lazy ones like me, it may avoid typos) we can substitute it (or any text) with a variable...
OBJECTS = main.o printhelloworld.o hello.o world.o
...which will look like this in the Makefile:
OBJECTS = main.o printhelloworld.o hello.o world.o

# The executable 'helloworld' depends on all object files listed in $(OBJECTS)
helloworld: $(OBJECTS)
        cc -o helloworld $(OBJECTS) # TAB before command!

main.o: main.c
        cc -c main.c # TAB before command!

printhelloworld.o: printhelloworld.c hello.h world.h
        cc -c printhelloworld.c # TAB before command!

hello.o: hello.c hello.h
        cc -c hello.c # TAB before command!

world.o: world.c world.h
        cc -c world.c # TAB before command!

# Remove object files, executables (UNIX/Windows), Emacs backup files, and core files
.PHONY : clean
clean:
        rm -rf $(OBJECTS) helloworld helloworld.exe *~ *.core core # TAB before command!
IndexPreviousNext
Last modified: Fri Dec 07 17:13:42 Romance Standard Time 2007