Question

I am having problems with parallel builds in make, namely having this definition

 rmlink:
         $(RM) $(ALIB)
         $(RM) $(BLIB)
         $(RM) $(CLIB)
         $(RM) $(DLIB)
         $(RM) $(ELIB)

 mklink:
         ln -sf $(ALIB_WITHPATH) $(ALIB)
         ln -sf $(BLIB_WITH_PATH) $(BLIB)
         ln -sf $(CLIB_WITH_PATH) $(CLIB)
         ln -sf $(DLIB_WITH_PATH) $(DLIB)
         ln -sf $(E_LIB) $(ELIB)

and launching build with make I will have those targets build

$ make
/bin/rm -f libA.so
/bin/rm -f libB.so
/bin/rm -f libC.so
/bin/rm -f libD.so
/bin/rm -f libE.so
ln -sf ../libA.so libA.so
ln -sf ../libB.so libB.so
ln -sf ../libC.so libC.so
ln -sf ../libD.so libD.so
ln -sf ../libE.so libE.so

But launching it with parallel build with make -j 2 (or more) I would get :

$ make -j 2
/bin/rm -f libA.so
ln -sf ../libB.so libB.so
/bin/rm -f libB.so
/bin/rm -f libC.so
/bin/rm -f libD.so
ln -sf ../libD.so libD.so
/bin/rm -f libE.so
ln -sf ../libA.so libA.so
ln -sf ../libC.so libC.so
ln -sf ../libE.so libE.so
g++: libB.so: No such file or directory

Which in this particular case would remove my link to libB.so and won't create it again - breaking the build in effect.

How can I fix this to be able to launch parallel builds ?

EDIT

Adding something like this to mklink target seems to fix the issue:

mklink: rmlink

Please comment if this is the correct way.

Was it helpful?

Solution

When considering parallel builds, make decides the order in which to build things solely by the prerequisite lists. If target FOO is not defined as a prerequisite of target BAR, then make will feel free to build both FOO and BAR at the same time.

Remember that having two targets listed as prerequisites to the same target, as in TARGET : FOO BAR, just means that both FOO and BAR must be built before TARGET. It does NOT mean that FOO and BAR have any relationship to each other, at all. So if you have:

foo: rmlink mklink

then make is free to run both rmlink and mklink at the same time. If you want to have rmlink always completed before mklink is started, you have to declare a prequisite relationship between them, like:

mklink: rmlink

And of course, remember you must ensure that the mklink target is a prerequisite of any linker target that needs those links to exist.

There may well be more wrong here but given the parts of the makefile you've shown and the question you asked, that should fix it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top