1

The usual way to create a 1:1 dependency in Makefiles is by using %, e.g.

%o:%cc 

Imagine I have 50 .cc files and I am interested in only 3 of them. I have constructed a list of their basenames:

CC_BASENAMES := Source15 Source32 Source41 

How can I create a prerequisite for an .o file by iterating over the items in $(CC_BASENAMES). The effect should be equivalent to:

Source15.o: Source15.cc Source32.o: Source32.cc Source41.o: Source41.cc 
2
  • How is this a unix or linux question? Shouldn't it be on stackoverflow or some programming site? Commented Mar 18, 2018 at 15:18
  • 1
    This is a Unix question because make is a Unix tool. Commented Mar 19, 2018 at 4:06

2 Answers 2

3

I'm not sure what you are asking, but there are several simple ways for manipulating strings.

For example, you can add the suffix .o to each of your basenames with OBJS = $(addsuffix .o,$(CC_BASENAMES)).

You can convert one suffix to another, for example $(OBJS:.o=.cc). So perhaps what you want is a Makefile such as

CC_BASENAMES := Source15 Source32 Source41 OBJS = $(addsuffix .o,$(CC_BASENAMES)) SRCS = $(OBJS:.o=.cc) fred.o: $(SRCS) cc $(SRCS) -o fred.o 

Or more usually you would define SRCS first and derive OBJS from it.


If you want a dependency rule to apply to just some targets, you can use targets: target-pattern: prereq-patterns ie

$(OBJS): %.o: %.cc 

See gnu make.

6
  • That is not an answer to the question. I clarified the question. Commented Mar 18, 2018 at 10:48
  • Do you mean something like $(OBJS): $(@:.o=.cc) Commented Mar 18, 2018 at 12:38
  • No. What you wrote means that all object files depend on all source files. I want 1:1 dependencies. Commented Mar 18, 2018 at 12:50
  • Do you mean then $(OBJS): %.o: %.cc Commented Mar 18, 2018 at 13:20
  • 1
    Did you check the addition to my answer? OBJS does indeed come from your list. Commented Mar 18, 2018 at 13:34
2

Having a general build rule (like %o:%cc) doesn't mean that make will have to go though all 50 source files every time. What you need to do is specify a the output you want, and let make apply the rule to build it for you. E.g. you could run

$ make Source15.o Source32.o Source41.o 

on the command line, or create a specific target in your makefile

my_build: Source15.o Source32.o Source41.o 

and then ask make to build that target:

$ make my_build 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.