www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/09/23/10:27:54

From: frazer AT rtp DOT ericsson DOT se (Scott Frazer)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Make files
Date: Mon, 23 Sep 1996 15:58:04 GMT
Organization: Ericsson Data Services Americas
Lines: 78
Message-ID: <5261bk$are@cnn.exu.ericsson.se>
References: <32461180 DOT 29C6 AT stud DOT tue DOT nl>
NNTP-Posting-Host: pc340.rtp.ericsson.se
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

"Richard L.F. van Paasen" <R DOT L DOT F DOT v DOT Paasen AT stud DOT tue DOT nl> wrote:

>Hi, (subject: write a generic makefile)

>I've downloaded the DJGPP software recently. I am a former Borland C++
>user, who's used to projects instead of makefiles. The beauty of 
>Borlands project files is that the only thing you have to tell
>the compiler is the sources and external objs and libs you're using.
>It figures out all depedencies (rules etc.) by itself, even from nested 
>header files. (of course it has drawbacks too).

Well, step one would be to read the well written post previous to
this one by John Aldrich.  The only thing he didn't seem to cover
was automatic dependency generation.  You can use the "-M" and/or
"-MM" flags with gcc to have it put out a list of dependencies for
a file.  Take this example makefile for instance:

--- start Makefile ---

# c compiler

CC = gcc

# c compiler flags

CFLAGS = -m486 -O2

# implicit rules

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

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

%.d: %.c
	$(CC) -MM $(CFLAGS) $< | sed 's/$*.o/& $@/g' > $@

%.d: %.S
	$(CC) -MM $< | sed 's/$*.o/& $@/g' > $@

# files

C_SOURCES = file1.c file2.c file3.c
S_SOURCES = file4.S file5.S

OBJS = $(C_SOURCES:.c=.o) $(S_SOURCES:.S=.o)

# targets

whatever.exe: $(OBJS)
	$(CC) -o $@ $(OBJS)

include $(C_SOURCES:.c=.d)
include $(S_SOURCES:.S=.d)

--- end Makefile ---

Dependencies in a makefile are made like this (for example):

file.c:  file.c inc1.h inc2.h inc3.h

Using gcc with -MM will produce exactly this output.  The 
above makefile will generate files with a ".d" (for depend-
ency) extension which are then included into the makefile
at the end.  Note that the output of "gcc -MM blah blah" is
put through "sed" (stream editor) which is a utility you
can get at one of the DJGPP distribution sites.  This is
necessary so that the ".d" file is automatically regenerated
if you change the ".c" or ".S" source file.  I know this
probably seems a bit convoluted, but play around with it and
it will soon make sense.  I would also suggest browsing 
through the info files on make ... they aren't that long.

Hope this helps,
Scott


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019