l i n u x - u s e r s - g r o u p - o f - d a v i s
Next Meeting:
July 7: Social gathering
Next Installfest:
Latest News:
Jun. 14: June LUGOD meeting cancelled
Page last updated:
2003 Mar 21 13:24

The following is an archive of a post made to our 'vox-tech mailing list' by one of its subscribers.

Report this post as spam:

(Enter your email address)
Re: [vox-tech] makefile question - auto dependencies
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [vox-tech] makefile question - auto dependencies

On Fri, Mar 21, 2003 at 10:18:21AM -0800, Peter Jay Salzman wrote:
> i was playing around with auto-dependencies with makefiles the other
> day.  here's a hello world example of the technique:

>    OBJS   = $(patsubst %.c, %.o, $(wildcard *.c))
>    SRCS   = $(wildcard *.c)

should use SRCS in OBJS list.

>    DEPDIR = .deps
>    vpath %.d ${DEPDIR}

I don't use vpath in the sample above... can add if you want.

> into a single line saying that all .o files depend on the corresponding
> .d file?

  I used to use a nasty sed expression to make the .d files, but for
some reason decided that they were not needed.

  The .o's do not depend on the .d.

  Both the .d and .o the depend on the .c.

  Here is a current sample of Makefile which I use to build programs
with, stripped down to only work on C code... below no user servicable
parts is the stuff that does auto building.
  Rules are written for C++, ProC, LaTeX, etc.

  If you only want a single target to make life simplier you can replace 
go,s2,s3 with:
	gcc ${CFLAGS} -o $@ $^ ${LFLAGS}

  If you care to use a special LD variable to id the linker
stuff ...

TARGET := go

CC := gcc
CFLAGS := -g -Wall -W -O9
CFLAGS := -g -Wall -W
LFLAGS := -lm

all: ${TARGET}

go: main.o
	gcc ${CFLAGS} -o $@ $^ ${LFLAGS}
s2: s2.o
	gcc ${CFLAGS} -o $@ $^ ${LFLAGS}
s3: s3.o
	gcc ${CFLAGS} -o $@ $^ ${LFLAGS}

# no user servicable parts below here... so don't change below here
%.o: %.c
	gcc ${CFLAGS} -c $^

.%.d: %.c
	@gcc -MM ${CFLAGS} -o $@ $^

	rm -rf *.o

nuke: clean
	rm -rf ${DFILES} $(TARGET)

CFILES := ${wildcard *.c}
OFILES := ${patsubst %.c,%.o,${CFILES}}
DFILES := ${patsubst %.c,.%.d,${CFILES}}

ifneq ($(MAKECMDGOALS),nuke)
-include ${DFILES}
vox-tech mailing list

LUGOD Group on LinkedIn
Sign up for LUGOD event announcements
Your email address:
LUGOD Group on Facebook
'Like' LUGOD on Facebook:

Hosting provided by:
Sunset Systems
Sunset Systems offers preconfigured Linux systems, remote system administration and custom software development.

LUGOD: Linux Users' Group of Davis
PO Box 2082, Davis, CA 95617
Contact Us

LUGOD is a 501(c)7 non-profit organization
based in Davis, California
and serving the Sacramento area.
"Linux" is a trademark of Linus Torvalds.

Sponsored in part by:
Sunset Systems
Who graciously hosts our website & mailing lists!