GNU make: How can I use automatic variables for multiple targets in my prerequisites?

I am using GNU make for compiling my fortran projects. In my special case, I have two static libraries each with a list of related objects. My Makefile contains:

LIB_BTMOD = btmod
LIB_btmod_OBJS = \
  m_bt_check \
  m_bt_execute

LIB_MDLMOD = mdlmod
LIB_mdlmod_OBJS = \
  m_model_set \
  m_model_run

After that I want to create each library with

$(LIB_BTMOD) : $(LIB_btmod_OBJS:%=%.o)
  ar rc lib$(@).a $^

$(LIB_MDLMOD) : $(LIB_mdlmod_OBJS:%=%.o)
  ar rc lib$(@).a $^

That solution seems a bit redundant for me. How can I use it with only one multiple target?

My proposition looks like:

$(LIB_BTMOD) $(LIB_MDLMOD) : $(LIB_$(@)_OBJS:%=%.o)
  ar rc lib$(@).a $^

but that does not extend the prerequisites as intended. What did I wrong at this point?

Thank you very much for your help.

Best regards

Upvotes: 0

Views: 358

Answers (1)

Beta
Beta

Reputation: 99172

The way to do what you intend is to first rearrange elements:

$(LIB_BTMOD) : $(LIB_btmod_OBJS:%=%.o)
$(LIB_MDLMOD) : $(LIB_mdlmod_OBJS:%=%.o)

$(LIB_BTMOD) $(LIB_MDLMOD) :
    ar rc lib$(@).a $^

Then rewrite the prerequisite lines:

$(LIB_BTMOD) : $(addsuffix .o,$(LIB_btmod_OBJS))
$(LIB_MDLMOD) : $(addsuffix .o,$(LIB_mdlmod_OBJS))

Then attempt to combine them (and fail):

$(LIB_BTMOD) $(LIB_MDLMOD) : $(addsuffix .o,$(LIB_$(@)_OBJS))

Then remember secondary expansion and get the syntax right:

.SECONDEXPANSION:

$(LIB_BTMOD) $(LIB_MDLMOD) : $$(addsuffix .o,$$(LIB_$$(@)_OBJS))
    ar rc lib$(@).a $^

That said, I think you'd be better off appending the .o when you construct the object lists, rather than on the fly.

Upvotes: 0

Related Questions