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).

# want $(cpp) -O1 for these
scrs1 =
objs1 = p1.o
# want $(cpp) -O2 for these
srcs2 =
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?
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.

I'd do something like this:

cpp = cpp

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

        $(cpp) -c -O1

        $(cpp) -c -O2

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.


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 =
OBS1 = s1.o  t1.o

SCS2 =
OBS2 = s2.o  t2.o

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

      $(CC) -c -O1 $(SCS1)

      $(CC) -c -O2 $(SCS2)

and this seems to work fine.
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.