makefiles: Different rules for different objects

I wish to make a single target, but for different
subsets of source files, I want different
compiler flags (specifically, different -O
optimisation levels).

eg:
# want $(cpp) -O1 for these
scrs1 = p1.cc
objs1 = p1.o
# want $(cpp) -O2 for these
srcs2 = p2.cc
objs2 = p2.o

How do I now set up different build rules
for the two object sets, and a single rule
to build and link the common target?
barneypAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

djveCommented:
I'd do something like this:

cpp = cpp

final_target:   p1.o p2.0
        $(cpp) p1.o p2.0 -o p.out

p1.o:   p1.cc
        $(cpp) -c -O1 p1.cc

p2.o:   p2.cc
        $(cpp) -c -O2 p2.cc


The rule "final_target" has two dependecies, the object files p1.0 and p2.0.

The two object files depends on the compiler leaving the object files from the C++ source code when it
compiles. These object files are then combined into the finale program p.out.

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
barneypAuthor Commented:
The answer was somewhat flawed because
it used the actual source/object files rather
than sets of them (as requested). But I can't
penalise the answer too heavily for that, as
I didn't specify that clearly. The solution
adapted to use sets of files looks like:

SCS1 = s1.cc t1.cc
OBS1 = s1.o  t1.o

SCS2 = s2.cc t2.cc
OBS2 = s2.o  t2.o

final_target: OBS1 OBS2
      $(CC) $(OBS1) $(OBS2) -o target

OBS1:   s1.cc
      $(CC) -c -O1 $(SCS1)

OBS2:   s2.cc
      $(CC) -c -O2 $(SCS2)

and this seems to work fine.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Unix OS

From novice to tech pro — start learning today.