Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I was investigating on the
same question here
, but I was not very clear of what I was asking, even for myself. Sorry for those who spent time answering my unclear question.
So let's try again with a more realistic example. We consider this structure:
├── Makefile
└── src/
├── bar
├── foo
└── Makefile
Where the main Makefile is:
all: src/foobar
src/foobar:
make -C $(dir $@)
And the sub-makefile is:
foobar: foo bar
join $^ > $@
If I run make for the first time (from ./
) everything works as expected, foobar
is produced.
$ make
make -C src/
make[1]: Entering directory '/project/src'
join foo bar > foobar
make[1]: Leaving directory '/project/src'
$ make
make: Nothing to be done for 'all'.
However if I touch any of the foobar
dependencies. The parent Makefile
will not regenerate the target. Here, I perfectly understand the behavior of Make, but I want to tell it to be aware of foobar
' dependencies.
$ touch src/foo
$ make
make: Nothing to be done for 'all'.
My current solution which is not very neat is to ask for the dependencies. So the src/Makefile
become:
src=foo bar
foobar: $(src)
@echo "Joining"
join $^ > $@
files: $(src)
@echo $^
And the ./Makefile
:
all: src/foobar
src=$(addprefix src/,$(shell make --no-print-directory -C src files | tr '\n' ' '))
src/foobar: $(src)
make -C $(dir $@)
I must also say that this particular example could be simplified using a single Makefile
only. My real example is quite more complex. The src/Makefile
generate an executable while the parent Makefile
do lots of other things (packing in a specific format, generate the documentation, build other sub-makefiles and so on). Thus, I want to keep these tasks well separated and I need to different Makefiles
.
–
–
In the main Makefile
create a dependency for the child target or directory that is always in need of building, and let the child Make then do the real work.
There is a good example here: http://owen.sj.ca.us/~rk/howto/slides/make/slides/makerecurs.html.
To translate for your case, change your main Makefile
to be:
all: src/foobar
src/foobar: force
$(MAKE) $(MFLAGS) -C src
force:
I also added $(MFLAGS)
which will pass same flags from parent to child make
.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.