Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to create two executables with one makefile?

3 views
Skip to first unread message

Daniel

unread,
Nov 18, 2009, 6:13:47 PM11/18/09
to
Below is my makefile, but it only makes the 1st target:

CC = g++
CFLAGS = -Wall -g -o0
INC =
SRCS1 = client.cpp player.cpp login.cpp
OBJS1 = $(SRCS1:.c=.o)
LIBS =
TARGET1 = tbclient.exe
WXCONFIG= `wx-config --cxxflags` `wx-config --libs`


$(TARGET1): $(OBJS1)
$(CC) $(CFLAGS) $(OBJS1) -o $(TARGET1) $(LIBS) $(WXCONFIG)

SRCS2 = server.cpp player.cpp
OBJS2 = $(SRCS2:.c=.o)
TARGET2 = tbserver.exe


$(TARGET2): $(OBJS2)
$(CC) $(CFLAGS) $(OBJS2) -o $(TARGET2) $(LIBS) $(WXCONFIG)

Ben Bacarisse

unread,
Nov 18, 2009, 6:22:23 PM11/18/09
to
Daniel <danw...@gmail.com> writes:

> Below is my makefile, but it only makes the 1st target:

Add a target the depends on the other two:

> CC = g++
> CFLAGS = -Wall -g -o0
> INC =
> SRCS1 = client.cpp player.cpp login.cpp
> OBJS1 = $(SRCS1:.c=.o)
> LIBS =
> TARGET1 = tbclient.exe
> WXCONFIG= `wx-config --cxxflags` `wx-config --libs`

all: $(TARGET1) $(TARGET2)

> $(TARGET1): $(OBJS1)
> $(CC) $(CFLAGS) $(OBJS1) -o $(TARGET1) $(LIBS) $(WXCONFIG)
>
> SRCS2 = server.cpp player.cpp
> OBJS2 = $(SRCS2:.c=.o)
> TARGET2 = tbserver.exe
>
>
> $(TARGET2): $(OBJS2)
> $(CC) $(CFLAGS) $(OBJS2) -o $(TARGET2) $(LIBS) $(WXCONFIG)

Doing $(EVERYTHING) through $(VARIABLES) like this is a little $(ODD).

--
Ben.

Daniel

unread,
Nov 18, 2009, 6:41:44 PM11/18/09
to
> all: $(TARGET1) $(TARGET2)
>

I tried this but it didnot work even with make all. It still only
makes the 1st target.

> Doing $(EVERYTHING) through $(VARIABLES) like this is a little $(ODD).
>

I thought so, but then if aint broke don't fix it (unless you really
know what your doing that is.)

Måns Rullgård

unread,
Nov 18, 2009, 6:50:58 PM11/18/09
to
Ben Bacarisse <ben.u...@bsb.me.uk> writes:

> Daniel <danw...@gmail.com> writes:
>
>> Below is my makefile, but it only makes the 1st target:
>
> Add a target the depends on the other two:
>
>> CC = g++
>> CFLAGS = -Wall -g -o0
>> INC =
>> SRCS1 = client.cpp player.cpp login.cpp
>> OBJS1 = $(SRCS1:.c=.o)

That's obviously wrong: .c vs .cpp.

>> LIBS =
>> TARGET1 = tbclient.exe
>> WXCONFIG= `wx-config --cxxflags` `wx-config --libs`
>
> all: $(TARGET1) $(TARGET2)
>
>> $(TARGET1): $(OBJS1)
>> $(CC) $(CFLAGS) $(OBJS1) -o $(TARGET1) $(LIBS) $(WXCONFIG)
>>
>> SRCS2 = server.cpp player.cpp
>> OBJS2 = $(SRCS2:.c=.o)
>> TARGET2 = tbserver.exe
>>
>>
>> $(TARGET2): $(OBJS2)
>> $(CC) $(CFLAGS) $(OBJS2) -o $(TARGET2) $(LIBS) $(WXCONFIG)
>
> Doing $(EVERYTHING) through $(VARIABLES) like this is a little $(ODD).

Nothing wrong with using variables. I'd do it like this, more or less:

$(TARGET1) $(TARGET2):
$(CC) $(CFLAGS) $^ -o $@ $(LIBS) $(WXCONFIG)

$(TARGET1): $(OBJS1)
$(TARGET2): $(OBJS2)

--
M�ns Rullg�rd
ma...@mansr.com

Daniel

unread,
Nov 18, 2009, 7:19:48 PM11/18/09
to
Now I get error
make: *** No rule to make target `g++', needed by `tbclient.exe'.
Stop.

Here is my current makefile:

CC = g++
CFLAGS = -Wall -g -o0

SRCS1 = client.cpp player.cpp login.cpp
OBJS1 = $(SRCS1:.c=.o)

LIBS =
TARGET1 = tbclient.exe
WXCONFIG= `wx-config --cxxflags` `wx-config --libs`

SRCS2 = server.cpp player.cpp


OBJS2 = $(SRCS2:.c=.o)
TARGET2 = tbserver.exe

$(TARGET1) $(TARGET2): $(CC) $(CFLAGS) $^ -o $@ $(LIBS) $(WXCONFIG)

$(TARGET1): $(OBJS1)

$(TARGET2): $(OBJS2)

Måns Rullgård

unread,
Nov 18, 2009, 7:30:47 PM11/18/09
to
Daniel <danw...@gmail.com> writes:

You dropped a newline and tab there:

$(TARGET1) $(TARGET2):
$(CC) $(CFLAGS) $^ -o $@ $(LIBS) $(WXCONFIG)

> $(TARGET1): $(OBJS1)
>
> $(TARGET2): $(OBJS2)

--

John Gordon

unread,
Nov 19, 2009, 1:34:09 AM11/19/09
to
In <yw1x7htn...@unicorn.mansr.com> =?iso-8859-1?Q?M=E5ns_Rullg=E5rd?= <ma...@mansr.com> writes:

> You dropped a newline and tab there:

> $(TARGET1) $(TARGET2):
> $(CC) $(CFLAGS) $^ -o $@ $(LIBS) $(WXCONFIG)

You're listing both TARGET1 and TARGET2 before the colon. Can you specify
two targets like this? And if you can, what does $@ expand to?

--
John Gordon A is for Amy, who fell down the stairs
gor...@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

John Gordon

unread,
Nov 19, 2009, 1:35:50 AM11/19/09
to

> > all: $(TARGET1) $(TARGET2)
> >

> I tried this but it didnot work even with make all. It still only
> makes the 1st target.

Did you add it at a point in the code before TARGET2 was defined?

Måns Rullgård

unread,
Nov 19, 2009, 5:10:24 AM11/19/09
to
John Gordon <gor...@panix.com> writes:

> In <yw1x7htn...@unicorn.mansr.com> =?iso-8859-1?Q?M=E5ns_Rullg=E5rd?= <ma...@mansr.com> writes:
>
>> You dropped a newline and tab there:
>
>> $(TARGET1) $(TARGET2):
>> $(CC) $(CFLAGS) $^ -o $@ $(LIBS) $(WXCONFIG)
>
> You're listing both TARGET1 and TARGET2 before the colon. Can you specify
> two targets like this? And if you can, what does $@ expand to?

Yes you can. $@ expands to whichever one is being built. People
really should read the manuals of tools they use.

Rainer Weikusat

unread,
Nov 19, 2009, 7:33:43 AM11/19/09
to
Daniel <danw...@gmail.com> writes:
> Now I get error
> make: *** No rule to make target `g++', needed by `tbclient.exe'.
> Stop.

[...]

> $(TARGET1) $(TARGET2): $(CC) $(CFLAGS) $^ -o $@ $(LIBS) $(WXCONFIG)

This claims that everything following the colon is a prequisite for
the targets. You should either have a semicolon before $(CC) or
put the 'rule' part on the next line with a htab in front of it.

Rainer Weikusat

unread,
Nov 19, 2009, 7:35:35 AM11/19/09
to
John Gordon <gor...@panix.com> writes:
>> You dropped a newline and tab there:
>
>> $(TARGET1) $(TARGET2):
>> $(CC) $(CFLAGS) $^ -o $@ $(LIBS) $(WXCONFIG)
>
> You're listing both TARGET1 and TARGET2 before the colon. Can you specify
> two targets like this? And if you can, what does $@ expand to?

The answers are 'yes' and 'the current target', cf

A rule with multiple targets is equivalent to writing many rules, each
with one target, and all identical aside from that.
(section 4.10 of the GNU make documentation)

Kenneth Brody

unread,
Nov 19, 2009, 3:57:11 PM11/19/09
to
Daniel wrote:
> Below is my makefile, but it only makes the 1st target:

That's right. Unless you pass a target on the command line, make only makes
the first target. Make the first target (I use the target name "all", so
that "make all" also works) dependent on the others.

> CC = g++
> CFLAGS = -Wall -g -o0
> INC =
> SRCS1 = client.cpp player.cpp login.cpp
> OBJS1 = $(SRCS1:.c=.o)
> LIBS =
> TARGET1 = tbclient.exe
> WXCONFIG= `wx-config --cxxflags` `wx-config --libs`

Add:

all: $(TARGET1) $(TARGET2)

I don't recall if variables need to be declared beforehand, or if forward
references are allowed.

> $(TARGET1): $(OBJS1)
> $(CC) $(CFLAGS) $(OBJS1) -o $(TARGET1) $(LIBS) $(WXCONFIG)
>
> SRCS2 = server.cpp player.cpp
> OBJS2 = $(SRCS2:.c=.o)
> TARGET2 = tbserver.exe
>
>
> $(TARGET2): $(OBJS2)
> $(CC) $(CFLAGS) $(OBJS2) -o $(TARGET2) $(LIBS) $(WXCONFIG)


--
Kenneth Brody

0 new messages