Jeff Saremi
Jeff Saremi

Reputation: 3024

Make: Avoiding issues from having same targets in multiple Makefiles

I need to know the best way of dealing with this. Also you could answer this -- after reading the sample below --: Look at the makelib target in package.make and tell me if there is a way to force this be treated as not updated if the recipe (make -C ../lib/ -f lib.make) reports as nothing to be made (not using ordered prequisites)?

I need to explain this point using an example. I have inherited this and I need the best way to make this right.

A target which other targets will be depending on:

File lib.make
--------------
.DEFAULT_GOAL = thelib.dll

%.dll: file1.obj file2.obj
      makelib file1.obj file2.obj -o thelib.dll

This by itself is pretty solid. You run it once (make -f lib.make) and it creates the lib. If you run it subsequently, having no modified files, then it will tell you it has nothing to do.

Now we're going to use this in a special way somewhere else:

File: package.make
------------------
.DEFAULT_GOAL: all

all: package

makelib: 
        @make -C ../lib/ -f lib.make

package: makelib file3 file4
        @package_files file3 file4 ../lib/out/*.dll -o package

This is how lib.make is referenced inside package.make.

The issue is even though the package gets created when you call make -f package.make all make assumes that package target needs to be rebuilt every time since one of its dependencies -- makelib -- had to be remade

Make considers makelib out of date despite what happens after entering lib.make.

To correct this I thought of a few choices:

Are there any suggestions other than and directly listing the dll as the dependency?

Upvotes: 0

Views: 261

Answers (1)

MadScientist
MadScientist

Reputation: 101041

There are two main ways this works:

First, if you use recursive make (please remember to always invoke a sub-make using $(MAKE), never make directly) then you should make the target in the parent makefile be the actual file generated by the sub-make:

package: lib/thelib.dll ...
         ...

lib/thelib.dll: FORCE
        $(MAKE) -f lib
FORCE:

Second, you can use non-recursive make which means you include the sub-makefile into the parent make and write it so that it expects that. You can play tricks with variables etc. to make this more generic, so it can be invoked from either the parent or subdirectory, if you want.

Upvotes: 2

Related Questions