Linking inside of pattern rules

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Linking inside of pattern rules

Bogdan
Hi,

I'm looking for a suggestion on how to achieve something a bit unusual. Simplifying things a bit for the purpose of this question, I have a pattern rule that looks something like this:

$(BIN_DIR)/%: | $(BIN_DIR)
    $(CC) $(LDFLAGS) -o $@ $<

and in a separate files, I have entries such as the following:

$(BIN_DIR)/foo: $(FOO_OBJ_FILES:%=$(OBJ_DIR)/%)

I'd also like to be able to link each executable with the appropriate set of libraries. However, I'm not entirely sure how to select them when the pattern rule gets executed (other than a hacky solution, like associating each executable with a .link file and then doing something like $(CC) $(LDFLAGS) -o $@ $< `cat $@.link` but that's pretty horrid).

I should note that I am trying to limit myself to POSIX make, including features coming to Issue 8 of the specification (there are quite a few things that GNU Make already has, fortunately). But if that's not at all possible, I might use a GNU Make-specific feature.

Thanks,
Bogdan

Reply | Threaded
Open this post in threaded view
|

Re: Linking inside of pattern rules

Paul Smith-20
On Sun, 2019-12-22 at 17:02 +0000, Bogdan wrote:

> I'd also like to be able to link each executable with the appropriate set
> of libraries. However, I'm not entirely sure how to select them when the
> pattern rule gets executed (other than a hacky solution, like associating
> each executable with a .link file and then doing something like $(CC)
> $(LDFLAGS) -o $@ $< `cat $@.link` but that's pretty horrid).
>
> I should note that I am trying to limit myself to POSIX make, including
> features coming to Issue 8 of the specification (there are quite a few
> things that GNU Make already has, fortunately). But if that's not at all
> possible, I might use a GNU Make-specific feature.

I don't really understand what you're trying to do; an example might help.

However, from what I do understand why don't you use recursive variable
names?  This feature is widely available in all versions of make and I
think it's going to be added to the standard:

  exe1_LIBS = -lfoo -lbar
  exe2_LIBS = -lbiz -lbaz

  exe%: ...
          ... $($@_LIBS)