MirkoBanchi
MirkoBanchi

Reputation: 2253

How to depend on targets of a previous make invocation

I have a lib (say mylib) and two executables and one of these (say exe1) depends on lib. In file system i have:

src
   Makefile
   ...
   lib
     mylib
       Makefile
       ...
   exe1
     Makefile
     ...
   exe2
     Makefile
     ...

and by launching make in src dir all is builded.

Makefile in src:

EXE=exe1 exe2
LIB=mylib

all: $(LIB) $(EXE)

.PHONY: $(LIB) $(EXE)

$(LIB):
    $(MAKE) -C lib/$@

$(EXE): $(LIB)
    $(MAKE) -C $@

and, for example, Makefile for exe1 is:

...
all: exe1 copy

exe1: exe1.o
    $(CC) $(CFLAGS) $(OBJ) $(LDFLAGS) -o $@

%.o: %.c
    $(CC) $(CFLAGS) -c $< -o $@
...

My problem is that if i change a file in mylib dir the library is correctly rebuilded but exe1 obsiously no...is there a way to teel make that exe1 target depends on a target (mylib) that is specified in a previous invocation of make without specifing dependencies on mylib's files in exe1 Makefile?

Thank you all.

Upvotes: 0

Views: 131

Answers (1)

Beta
Beta

Reputation: 99084

@tripleee and @JackKelly (curse his name) are right, this is not a healthy makefile system.

You can get something like what you want by changing src/Makefile:

EXE=exe1 exe2
LIB=lib/mylib/mylib

all: $(LIB) $(EXE)

.PHONY: $(EXE)

$(LIB):
    $(MAKE) -C lib/mylib

exe1: $(LIB)

$(EXE):
    $(MAKE) -C $@

and changing exe1/makefile so that it will always rebuild exe1:

.PHONY: exe1

This still has many problems, but at least it will correctly rebuild lib/mylib/mylib and src/exe1/exe1 when you run Make in src. (It will not work if you run Make in src/exe1/.)

Upvotes: 1

Related Questions