1752782 Members
6124 Online
108789 Solutions
New Discussion юеВ

Make file Related

 
Anil Kumar Malepati
Occasional Advisor

Make file Related

How to make .o files to get created in a different directory and how to make use of them (from different directories) in linking?
5 REPLIES 5
Dennis Handly
Acclaimed Contributor

Re: Makefile Related

You can create your own compile rules:
$(objdir)/foo.o: foo.c
         $(CC) -o $@ -c foo.c $(CFLAGS)

And when you link, you use a macro with a list of all of the objects created as above.

Peter Nikitka
Honored Contributor

Re: Make file Related

Hi,

you can else use the VPATH macro to reference source files not residing in the current dir (see manual).

For linking it's best to build libraries out of your object files (perhaps one lib per directory?) and reference these.

In dir1/Makefile:
...
OBJS=$(SRCS:.c=.o)
$(LIBDIR)/libdir1.a: $(OBJS)
ar c $@ $(OBJS)

In exe/Makefile:
...
USERLIBS=-L$(LIBDIR) -ldir1 -ldir2 ...
...
myexe:
$(CC) -o $@ ... $(USERLIBS) ...


mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Anil Kumar Malepati
Occasional Advisor

Re: Make file Related

I tried using VPATH. But it doesn't find the source files in a different directory. Following is the make file contents.

LIBDIR=${PWD}/lib
BINDIR=${PWD}/bin
SRCDIR=${PWD}/src

VPATH=${SRCDIR}

all: clean ${LIBDIR}/session.o
#${LIBDIR}/session.o: ${SRCDIR}/session.c ${SRCDIR}/session.h
# ${CC} ${CFLAGS} -o $@ -c ${SRCDIR}/session.c

${LIBDIR}/session.o: session.c session.h
${CC} ${CFLAGS} -o $@ -c session.c

clean:
rm -f ${LIBDIR}/*.o
rm -f ${BINDIR}/*

Make fails with the following error
Anil Kumar Malepati
Occasional Advisor

Re: Make file Related

Forgot pasting the error in my reply (above).
(Bundled) cc: error 1913: `session.c' does not exist or cannot be read
*** Error exit code 1
Dennis Handly
Acclaimed Contributor

Re: Makefile Related

>I tried using VPATH. But it doesn't find the source files in a different directory.

Why are you using new-fangled VPATH?
Why not the tried and true relative path that points exactly to it?

If you use VPATH, as with any new and hard to use option, you need to read the documentation:
http://www.gnu.org/software/automake/manual/make/General-Search.html#General-Search
http://www.gnu.org/software/automake/manual/make/Commands_002fSearch.html#Commands_002fSearch

The second link indicates you need to use "$^" instead of session.c.
Unfortunately, make(1) while it documents VPATH, it is almost useless since it doesn't mention "$^" nor say that "$<" works for anything except inference rules.

#${LIBDIR}/session.o: ${SRCDIR}/session.c ${SRCDIR}/session.h
# ${CC} ${CFLAGS} -o $@ -c ${SRCDIR}/session.c

I assume this works?

>(Bundled) cc: error 1913: `session.c' does not exist or cannot be read

This indicates that VPATH isn't working.
Also, why are you using cc_bundled(1)?