0

I have this folder/files structure:

./libs/
    helpers.c
    helpers.h
./a_app.c
./b_app.c
./c_app.c

Each app depends on helpers lib and GStreamer, so I need to compile helpers.o (inside libs/ folder) and then link each app.

Currently I have this Makefile:

CC      =   gcc
CFLAGS  =   -g -Wall -w
LFLAGS  =   -g -Wall -w -c

CFLAGS  +=  `pkg-config --cflags gstreamer-app-0.10`
LFLAGS  +=  `pkg-config --cflags gstreamer-app-0.10`

LDFLAGS =
LDFLAGS +=  `pkg-config --libs gstreamer-app-0.10`

all: examples

examples: helpers.o a_app
    $(info *** examples ***)

helpers.o:
    $(info *** helpers.o ***)
    $(CC) $(LFLAGS) libs/helpers.c -o libs/helpers.o $(LDFLAGS)

a_app: a_app.o
    $(CC) $(CFLAGS) libs/helpers.o a_app.o -o a_app $(LDFLAGS)

a_app.o: a_app.c
    $(info *** a_app.o ***)
    $(CC) $(LFLAGS) a_app.c $(LDFLAGS)

While I could add b_appand c_app I'm looking for another (more elegant) way of doing it. Can't I just say that I have a, b and c _app and let Makefile compile them all and link them against GStreamer and helpers?

Also, is there any way to make Makefile compile files without needing to tell it -o name_of_file (and perhaps make it compile them in the folder that they are, because of the helpers library).

7
  • 1
    Actually, I'm pretty sure make doesn't need to be told how to make .o files. Mar 19, 2013 at 11:56
  • So I can remove the helpers.o and a_app.o rules? Mar 19, 2013 at 12:00
  • 1
    Yeah, I think so. You should also be sure to specify that the a_app rule is dependent on libs/helpers.o Mar 19, 2013 at 12:01
  • If I remove the -o from the a_app, I get a binary called "a.out" instead of "a_app". Apart from that, it works fine. Mar 19, 2013 at 12:06
  • Right. Make only instinctively knows how to make .o flies, it needs the -o for any given output. Mar 19, 2013 at 12:19

1 Answer 1

2

Okay, so - as we discussed in the comments, make can figure out how to make the .o files, so those rules are unnecessary. To make a generalized rule for all your *_app files (assuming they all have the same dependency on helpers.h, you can do this:

%_app: %_app.o libs/helpers.o

Make uses the % as a wildcard, and in the rule/dependency line the wildcard will expand to the same thing in the dependencies as it did in the rule. In the actual execution, you can use $* to get the same string. So a single rule for all your *_app executables winds up looking a bit like this:

%_app: %_app.o libs/helpers.o
    $(CC) $(CFLAGS) libs/helpers.o $*_app.o -o $*_app $(LDFLAGS)

I was testing this on my machine (hence comments instead of answers, and wound up writing this Makefile:

CC      =   gcc
CFLAGS  =   -g -Wall -w
LFLAGS  =   -g -Wall -w -c

CFLAGS  +=  `pkg-config --cflags gstreamer-app-0.10`
LFLAGS  +=  `pkg-config --cflags gstreamer-app-0.10`

LDFLAGS =
LDFLAGS +=  `pkg-config --libs gstreamer-app-0.10`

new: clean all

clean:
    rm -rf *.o */*.o *_app

all: examples

examples: a_app b_app

%_app: %_app.o libs/helpers.o
    $(CC) $(CFLAGS) libs/helpers.o $*_app.o -o $*_app $(LDFLAGS)

Does that all make sense?

EDIT: It occurs to me that GNU Make can run some commands on the command line and store the string for its own purposes.

$(shell ls *_app.c | sed 's/.c//') will expand into all the apps you have in the current directory. so you can say:

examples: $(shell ls *_app.c | sed 's/\.c//')

Or, as I think is a little better:

...
ALLAPPS = $(shell ls *_app.c | sed 's/\.c//')
...
all: $(ALLAPPS)

That way make can be used to make everything, and make ?_app can be used to compile one app at a time.

Super ultra mega double EDIT:

Using a bald % operator as a target will bust up Make's ability to auto generate .o files. Here's the solution we worked out in chat:

CC      =   gcc
CFLAGS  =   -g -Wall -w
LFLAGS  =   -g -Wall -w -c

CFLAGS  +=  $(shell pkg-config --cflags gstreamer-app-0.10)
LFLAGS  +=  $(shell pkg-config --cflags gstreamer-app-0.10)

LDFLAGS =
LDFLAGS +=  $(shell pkg-config --libs gstreamer-app-0.10)

TARGETS = $(shell ls *.c | sed 's/\.c//')

new: clean all

clean:
rm -rf *.o */*.o *_app

all: examples

examples: $(TARGETS)

.SECONDEXPANSION:
$(TARGETS): libs/helpers.o [email protected]
$(CC) $(CFLAGS) libs/helpers.o [email protected] -o $@ $(LDFLAGS)
7
  • Though, I think make automatically cleans up its .o files. Not sure. Mar 19, 2013 at 12:29
  • I'm having some problem with LDFLAGS at the end. It doesn't get expanded. make shows the command that it's running and I doo see CC CFLAGS libs/helpers.o a_app.o -o a_app but I don't see the LDFLAGS at the end. Mar 19, 2013 at 12:38
  • 1
    Try using a shell command instead of backticks: $(shell pkg-config --libs gstreamer-app-0.10 Mar 19, 2013 at 12:42
  • Ok, so now it gets expanded but there are two more problems. The first on is that helpers.o doesn't get created and the second one is that for some reason LDFLAGS gets moved before a_app.c -o a_app. Here's my Makefile: pastie.org/6625805 Mar 19, 2013 at 12:48
  • let us continue this discussion in chat Mar 19, 2013 at 12:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.