tags:

views:

229

answers:

3

Hello,

gcc 4.4.1 C99

I am creating a client server application.

I have a Makefile that compiles all the files. However, I would like 2 create 2 targets (binaries) one called clt and svr. And would like the Makefile to create 2 separate directories for them, called ./client and ./server?

So far I have done this to my Makefile. That only creates the svr. I am not sure how I can add the second target for the client to be created.

Hope you understand my point?

Many thanks for any suggestions,

OBJECT_FILES = server.o client.o cltsvr_ults.o

CFLAGS = -ggdb -Wall -pthread

LIBS = -lpthread

CC = gcc

svr: $(OBJECT_FILES)
    $(CC) $(CFLAGS) $(OBJECT_FILES) $(LIBS) -o svr

client.o: client.c cltsvr_ults.h
    $(CC) -c client.c

server.o: server.c cltsvr_ults.h
    $(CC) -c server.c

cltsvr_ults.o: cltsvr_ults.c
    $(CC) -c cltsvr_ults.c

clean:
    rm svr *.o *~
+3  A: 

Make your first (default) target depend on the cltandsvr targets.

all:  svr clt

svr: ...
clt: ...

You also will want to have the cltandsvr target create the subdirectories.

Another, probably better, approach is to move all the server code into its own directory, and likewise all the client code into its own directory. (This assumes that you can split the code nicely into two portions.) Then have a small Makefile in the top directory whose targets execute the Makefiles in each of the subdirectories.

Loadmaster
That sounds like a better idea. However, how can my top directory call the 2 makefiles in the sub-directories? Thanks.
robUK
By doing a `cd clt; make $(MAKEFLAGS)`, which spawns a subprocess to perform the sub-make. Ditto for the `svr` target.
Loadmaster
+3  A: 

You can put your dependency graph for clt in there as well and add a first rule like this:

all: svr clt
Joey
Yep. One point - make looks for the first target by default, so place "all" above the "svr" and "clt" targets.
BenB
Hence *first* rule. But yes, I could have made that point clearer, I think.
Joey
+3  A: 
Beta
+1 for clean and complete sample code
grigy
"makefile should do"... "makefile... do"... *sigh*
Pavel Shved
Oh come on Pavel, a Makefile is code, like a script. Is it really so bad to say "this Makefile does X", instead of "this makefile casuses make to do X"?
Beta