[comp.unix.programmer] make with multiple executables from same source files

tif@doorstop.austin.ibm.com (Paul Chamberlain) (11/12/90)

I'm not sure what happened the first time, I'll try again...

I have two executables that depend on the same source files but are
compiled with different CFLAGS.  Anybody have a good way to make a
makefile understand this?

Let's say I have source1.c, source2.c, and source3.c.  I want to
be able to say "make exe1", "make exe2", or "make all".  But I
want it to do the right thing if I say "make all; make all".  i.e.

	cc -DDEF1 -c source1.c
	cc -DDEF1 -c source2.c
	cc -DDEF1 -c source3.c
	cc -o exe1 source1.o source2.o source3.o
	cc -DDEF2 -c source1.c
	cc -DDEF2 -c source2.c
	cc -DDEF2 -c source3.c
	cc -o exe2 source1.o source2.o source3.o

and then

	`all' is up to date.

I don't mind having extra "touch", "test", or "rm" commands.  I
don't even mind having more than one makefile.

Paul Chamberlain | I do NOT represent IBM.     tif@doorstop, sc30661 at ausvm6
512/838-7008     | ...!cs.utexas.edu!ibmchs!auschs!doorstop.austin.ibm.com!tif

gpz@ESD.3Com.COM (G. Paul Ziemba) (11/13/90)

tif@doorstop.austin.ibm.com (Paul Chamberlain) writes:

>Let's say I have source1.c, source2.c, and source3.c.  I want to
>be able to say "make exe1", "make exe2", or "make all".  But I
>want it to do the right thing if I say "make all; make all".  i.e.

>	cc -DDEF1 -c source1.c; cc -DDEF1 -c source2.c; cc -DDEF1 -c source3.c
>	cc -o exe1 source1.o source2.o source3.o
>	cc -DDEF2 -c source1.c; cc -DDEF2 -c source2.c; cc -DDEF2 -c source3.c
>	cc -o exe2 source1.o source2.o source3.o
>and then
>	`all' is up to date.

>[has 2 targets, same sources, different preprocessor definitions
>to produce different objects with the same names]

I would suggest that the object files (.o) be given different names
depending on the flags they were compiled with. Make gets confused
because it thinks that source1.o is the same as source1.o.

How about something like this:

all:	exe1 exe2

exe1:	source1.def1.o source2.def1.o source3.def1.o
	${CC} -o $@ source1.def1.o source2.def1.o source3.def1.o

exe2:	source1.def2.o source2.def2.o source3.def2.o
	${CC} -o $@ source1.def2.o source2.def2.o source3.def2.o

source1.def1.o:	source1.c
	${CC} -o source1.def1.o -DDEF1 -c $?

source2.def1.o:	source2.c
	${CC} -o source2.def1.o -DDEF1 -c $?

source3.def1.o:	source3.c
	${CC} -o source3.def1.o -DDEF1 -c $?

source1.def2.o:	source1.c
	${CC} -o source1.def2.o -DDEF2 -c $?

source2.def2.o:	source2.c
	${CC} -o source2.def2.o -DDEF2 -c $?

source3.def2.o:	source3.c
	${CC} -o source3.def2.o -DDEF2 -c $?


 ~!paul
--
Paul Ziemba  api!gpz  gpz@ESD.3com.com  408.764.5390   OS/2: just say no.

"How much char could a char star star if char star could star char?"
(quote stolen from mspercy@clemson.clemson.edu)

mpreen@hemel.bull.co.uk (Malcolm Preen) (11/15/90)

gpz@ESD.3Com.COM (G. Paul Ziemba) writes:

>source1.def1.o:	source1.c
>	${CC} -o source1.def1.o -DDEF1 -c $?

Giving the above as an example to create a .o with a different name from the
.c it comes from.

On our system, a Bull DPX/2-340, the man page says that -o is passed to ld(1)
and this is the case when trying it. All the people I have spoken to agree
that this is what is supposed to happen in Unix C.
-- 
|Malcolm Preen          |Malcolm.Preen@hemel.bull.co.uk|
|Bull HN Info. Sys. Ltd.|HM14 UK03                     |
|Maxted Road            |Tel: +44 442 232222 x4367     |
|Hemel Hempstead, UK    |Fax: +44 442 234084           |
+-----------------------+------------------------------+
-- 
|Malcolm Preen          |Malcolm.Preen@hemel.bull.co.uk|
|Bull HN Info. Sys. Ltd.|HM14 UK03                     |
|Maxted Road            |Tel: +44 442 232222 x4367     |
|Hemel Hempstead, UK    |Fax: +44 442 234084           |
+-----------------------+------------------------------+

gpz@ESD.3Com.COM (G. Paul Ziemba) (11/16/90)

mpreen@hemel.bull.co.uk (Malcolm Preen) writes:
>gpz@ESD.3Com.COM (G. Paul Ziemba) writes:

>>source1.def1.o:	source1.c
>>	${CC} -o source1.def1.o -DDEF1 -c $?

>On our system, a Bull DPX/2-340, the man page says that -o is passed to ld(1)
>and this is the case when trying it. All the people I have spoken to agree
>that this is what is supposed to happen in Unix C.

Surprise, all Unixes are not alike.

It just so happens that I have a Bull DPX/2 with manuals at my
disposal, and they confirm what Mr. Preen says. I can not
determine a way to cause cc to rename the .o file.

On other Unixes I have used (BSD, Sun, Xinu), -o does in fact
specify the name of the output file regardless of the particular
compilation step being performed.

OS-dependent compiler flags are one of life's little joys
for me as a programmer attempting to build software on an
array of different platforms. I will abstain from comment
on what the "correct" cc argument syntax/semantics should
be.

 ~!paul
--
Paul Ziemba  api!gpz  gpz@ESD.3com.com  408.764.5390   OS/2: just say no.

"How much char could a char star star if char star could star char?"
(quote stolen from mspercy@clemson.clemson.edu)

weimer@ssd.kodak.com (Gary Weimer) (11/17/90)

In article <4196@awdprime.UUCP> tif@doorstop (Paul Chamberlain) writes:
>I have two executables that depend on the same source files but are
>compiled with different CFLAGS.  Anybody have a good way to make a
>makefile understand this?

With SunOS's make, you can simply add this line to your Makefile:

.KEEP_STATE:

This causes make to remember the exact command used to compile each object.
Since the flags are changed, make sees this as a different compile which
has not yet been done.