Discussion:
makefile question
(too old to reply)
Helmut Giese
2007-11-23 13:37:28 UTC
Permalink
Hello out there,
seems like I forgot some of the finer points on using a makefile.

I can create a list of .obj files from my source files
# Create list of .obj files from C files
COBJS = $(CSRCS:.c=.obj)

and I know about implicit rules like
# compile any C file
.c.obj:
$(CC) -c $(CCFLAGS) -o $@ $<
which make for compact and easy to maintain makefiles.

Everything works well as long as source and .obj files share the same
directory. However I would like to place my .obj files into a sub
directory (say OBJ) - and I fail miserably when trying to adapt the
above syntax to using different directories.

I am near to certain that (many) years ago I knew how to achieve this
and so I think that it is possible.

Any tips will be greatly appreciated.
Best regards
Helmut Giese
Ebbe Kristensen
2007-11-23 14:34:59 UTC
Permalink
Post by Helmut Giese
Everything works well as long as source and .obj files share the same
directory. However I would like to place my .obj files into a sub
directory (say OBJ) - and I fail miserably when trying to adapt the
above syntax to using different directories.
I did just that yesterday. My solutions looks like this:

.AUTODEPEND

.path.h = .\headers

.path.c = .\source

.path.obj = .\output

OBJFILES=file01.obj file02.obj

.c.obj:
$(CC) -c $(CCFLAGS) -o $@ $<

main: target.exe

target.exe: $(OBJFILES)
<link command - use $** to get expanded list of obj-files>

No need for a list of source files as these are inferred from the list of
object files.

Ebbe
Darko Miletic
2007-11-23 15:13:45 UTC
Permalink
Post by Helmut Giese
Hello out there,
seems like I forgot some of the finer points on using a makefile.
I can create a list of .obj files from my source files
# Create list of .obj files from C files
COBJS = $(CSRCS:.c=.obj)
and I know about implicit rules like
# compile any C file
which make for compact and easy to maintain makefiles.
n
Everything works well as long as source and .obj files share the same
directory. However I would like to place my .obj files into a sub
directory (say OBJ) - and I fail miserably when trying to adapt the
above syntax to using different directories.
Let us assume that we have structure of project like this:

root
|
|----src
|
|----objs

Makefile is in root folder, source files are in src and object files
should go to objs.

Here is a working example with only one source file called main.c:

CSRCS=main.c
OBJS=$(CSRCS:.c=.obj)
OBJDIR=objs
SRCDIR=src

.path.c=$(SRCDIR)
.path.obj=$(OBJDIR)

all: main.exe

clean:
del $(OBJDIR)\*.obj
del main.*

main.exe: $(OBJS)
bcc32 $**

.c.obj:
bcc32 -c -o $@ $<
Helmut Giese
2007-11-23 16:20:37 UTC
Permalink
Thanks Ebbe and Darko,
I had all forgotten about the 'path' directive.

Have a nice weekend and best regards
Helmut Giese

Continue reading on narkive:
Loading...