makefiles: Different rules for different objects

Posted on 1999-12-07
Medium Priority
Last Modified: 2010-04-21
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 = 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?
Question by:barneyp

Accepted Solution

djve earned 750 total points
ID: 2263302
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.


Author Comment

ID: 2264847
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.

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
Suggested Courses
Course of the Month5 days, 10 hours left to enroll

588 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question