On Mar 23, 2:53 pm, Johann Klammer <
klamm...@NOSPAM.a1.net> wrote:
>
> > --quote: Managing Projects with GNU Make (3rd)--
>
> I suspect that in a later chapter they will suggest you put a makefile
> in each subdirectory and use some recursive make thingy...
>
> But then you may want to read this:
http://evbergen.home.xs4all.nl/nonrecursive-make.html
>
> BTW, most of the old geezers around here seem to killfile posts from
> google groups, so you may get more answers using some real newsreader
> (like thunderbird).
>
Thank you.
If I don't use wildcard in variable SRCS, this version even woks for
all the following layout:
Layout 1:
$ ls . include/ src/ mod_a/
.:
include Makefile mod_a src
include/:
bar.h foo.h
mod_a/:
mod_a.c mod_a.h
src/:
bar.c foo.c main.c
$
Layout 2:
$ ls . include/ src/ mod_a/
.:
include main.c Makefile mod_a src
include/:
bar.h foo.h
mod_a/:
mod_a.c mod_a.h
src/:
bar.c foo.c
$
The inconvenient is that I need to specify all the names and
directories of source files. I can stay peace with it though.
I haven't tried your new sed snippet. With you suggestion of the
wildcard in variable SRCS, there's no need to do that. But the ./src/
Makefile and all source files should be put in one same directory eg ./
src/ and header files in ./header. If I can fix it, I'll get my
general Makefile and it will be great.
$ cat Makefile
OUT = main.out
SRCS = main.c foo.c bar.c \
mod_a.c
OBJS = $(patsubst %.c,%.o,$(SRCS))
CC = gcc
CXX = g++
CFLAGS = -ansi -pedantic -Wall -W
CXXFLAGS = -ansi -pedantic -Wall -W
CPPFLAGS = -I include -I mod_a
LDFLAGS =
vpath %.c src mod_a
# use CXX instead of CC for C++ source file
$(OUT): $(OBJS)
$(CC) $(LDFLAGS) $^ -o $@
include $(SRCS:.c=.d)
# use CXX instead of CC for C++ source file
%.d: %.c
@set -e; rm -f $@; \
$(CC) -M $(CPPFLAGS) $< > $@.$$$$; \
sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
rm -f $@.$$$$
.PHONY : clean
clean:
rm *.d $(OBJS) $(OUT)
$
$ make
Makefile:19: main.d: No such file or directory
Makefile:19: foo.d: No such file or directory
Makefile:19: bar.d: No such file or directory
Makefile:19: mod_a.d: No such file or directory
gcc -ansi -pedantic -Wall -W -I include -I mod_a -c -o main.o main.c
gcc -ansi -pedantic -Wall -W -I include -I mod_a -c -o foo.o src/
foo.c
gcc -ansi -pedantic -Wall -W -I include -I mod_a -c -o bar.o src/
bar.c
gcc -ansi -pedantic -Wall -W -I include -I mod_a -c -o mod_a.o mod_a/
mod_a.c
gcc main.o foo.o bar.o mod_a.o -o main.out
$ make
make: `main.out' is up to date.
$ touch include/foo.h
$ make
gcc -ansi -pedantic -Wall -W -I include -I mod_a -c -o main.o main.c
gcc -ansi -pedantic -Wall -W -I include -I mod_a -c -o foo.o src/
foo.c
gcc main.o foo.o bar.o mod_a.o -o main.out
$ make
make: `main.out' is up to date.
$ touch mod_a/mod_a.h
$ make
gcc -ansi -pedantic -Wall -W -I include -I mod_a -c -o main.o main.c
gcc -ansi -pedantic -Wall -W -I include -I mod_a -c -o mod_a.o mod_a/
mod_a.c
gcc main.o foo.o bar.o mod_a.o -o main.out
$ make
make: `main.out' is up to date.
$ ./main.out
main.c:8:main
src/foo.c:6:foo
src/bar.c:6:bar
mod_a/mod_a.c:6:mod_a
$