You really need to add a rule which knows how to make libA and libB, then add the dependency from test onto that rule. The rule can either call make in that directory (recursive make), or explicitly encode the rules for building the libs in your makefile. The first one is more traditional and is pretty simple to understand. It works with almost all situations you will encounter in the field, but there are some potential issues that can arise if you have more complex build setup (I would probably go with it anyway because it is simpler).
+4
A:
1800 INFORMATION
2009-07-21 20:39:05
+1 for 'RMCH'. I like make, but I'm a bit backward like that, and I'm happy with an inclusive make model that uses a lot of include and a lot of common recipes do maintain common compiler settings and reduce duplication. It was a bit of a pain to set up, though, and I think that 90% of people would probably get on better with a more modern tool (scons, cmake, etc.)
Charles Bailey
2009-07-21 20:45:20
A:
make -C dir
is your friend here. Do something like:
PROJECTS = A B C D
build:
for dir in $(PROJECTS); do \
$(MAKE) -C $$dir build; \
done
And list your sub-projects in the order you want them to be built.
Nikolai N Fetissov
2009-07-21 20:39:14
[Don't do that](http://miller.emu.id.au/pmiller/books/rmch/). It is error prone and scales poorly.
Novelocrat
2010-08-06 18:33:54
A:
I do something like this:
A/
Makefile
A.hpp
A.cpp
B/
Makefile
B.hpp
B.cpp
Makefile
test.cpp
file A/Makefile:
LIB = libA.so
OBJS = A.o
all: compile link
compile: ${OBJS}
link:
g++ -shared -o ${LIB} ${OBJS}
clean:
rm -f ${OBJS}
A.o:
g++ -c A.cpp
file B/Makefile:
LIB = libB.so
OBJS = B.o
all: compile link
compile: ${OBJS}
link:
g++ -shared -o ${LIB} ${OBJS}
clean:
rm -f ${OBJS}
B.o:
g++ -c B.cpp
file Makefile:
DIRS = A B
OUTPUT = test
LD = -L. -LA -LB -lA -lB
OBJS = test.o
all: compile link
compile: ${OBJS}
for DIR in ${DIRS}; do \
make -C $${DIR} compile; \
done
link:
for DIR in ${DIRS}; do \
make -C $${DIR} link; \
done
g++ -o ${OUTPUT} ${OBJS} ${LD}
clean:
for DIR in ${DIRS}; do \
make -C $${DIR} clean; \
done
rm -f ${OBJS}
Then you can:
make compile
make link
Or just:
make
Everything, also in the subdirectories, will be compiled and linked together
VDVLeon
2009-11-26 17:23:12
[Don't do that](http://miller.emu.id.au/pmiller/books/rmch/). It is error prone and scales poorly.
Novelocrat
2010-08-06 18:34:20