GNU make - Files in various Directories

Dear Newsgroup,

I tried to compile my software project with the tool GNU make. The
situation is as follows: The source and header files are in different
directories. The binary should reside in the directory and the the
object files should be stored in the directory.

project_dir
|
± source
| |
| ± main.cpp
| ± foo.cpp
|
± include
| |
| ± foo.h
|
± bin
|
± temp

The makefile I wrote looks like:

------------------ MAKEFILE -----------------------

PROJ_DIR= project_dir
BIN_DIR= $(PROJ_DIR)/bin
TEMP_DIR= $(PROJ_DIR)/temp
INCLUDE_DIR= $(PROJ_DIR)/include
SOURCE_DIR= $(PROJ_DIR)/source

CC=g++

CXXFLAGS= -g -Wall
LFLAGS= -g -Wall -o $(BIN_DIR)/a.out

OBJS= foo.o main.o

vpath x.cpp $(SOURCE_DIR)
vpath x.h $(INCLUDE_DIR)
vpath x.o $(TEMP_DIR)
vpath x $(BIN_DIR)

%.c : %o
$(CC) $(CXXFLAGS) -o $(TEMP_DIR)/$@ $<

a.out : $(OBJS)
$(CC) $(LFLAGS) -o $(BIN_DIR)/$@ $^

main.o : main.cpp foo.h

foo.o : foo.cpp foo.h

---------------- MAKEFILE END ------------------------

If I run ‘make’ the object files are produced and stored in the
directory. But the linking process failed because ‘make’ doesn’t find
the object files. I have to run ‘make’ again to link the object files
correctly.
Is there any possibility that I have to run ‘make’ only once?

Thank you very much in advance,
Thomas


Thomas Reisinger
t.reisinger@tu-bs.de

Thomas Reisinger <t.reisinger@tu-bs.de> wrote:

I tried to compile my software project with the tool GNU make. The
situation is as follows: The source and header files are in different
directories. The binary should reside in the directory and the the
object files should be stored in the directory.

If I run ‘make’ the object files are produced and stored in the <temp
directory. But the linking process failed because ‘make’ doesn’t find
the object files. I have to run ‘make’ again to link the object files
correctly.
Is there any possibility that I have to run ‘make’ only once?

=======================================================================
PROJ_DIR= project_dir
BIN_DIR= $(PROJ_DIR)/bin
TEMP_DIR= $(PROJ_DIR)/temp
INCLUDE_DIR= $(PROJ_DIR)/include
SOURCE_DIR= $(PROJ_DIR)/source

CC=g++

CXXFLAGS= -g -Wall
LFLAGS= -g -Wall

OBJS= $(TEMP_DIR)/foo.o $(TEMP_DIR)/main.o

vpath %.cpp $(SOURCE_DIR)
vpath %.h $(INCLUDE_DIR)

$(TEMP_DIR)/%.o : %.cpp
$(CC) -c $(CXXFLAGS) -o $@ $<

$(BIN_DIR)/a.out : $(OBJS)
$(CC) $(LFLAGS) -o $@ $^

$(TEMP_DIR)/main.o : main.cpp foo.h

$(TEMP_DIR)/foo.o : foo.cpp foo.h


Brian Stecher (bstecher@qnx.com) QNX Software Systems, Ltd.
phone: +1 (613) 591-0931 (voice) 175 Terence Matthews Cr.
+1 (613) 591-3579 (fax) Kanata, Ontario, Canada K2M 1W8

Thomas Reisinger <t.reisinger@tu-bs.de> wrote:

Dear Newsgroup,

I tried to compile my software project with the tool GNU make. The
situation is as follows: The source and header files are in different
directories. The binary should reside in the directory and the the
object files should be stored in the directory.

%.c : %o

Shouldn’t this be:
%.c : %.o
^ this is missing

If I run ‘make’ the object files are produced and stored in the <temp
directory. But the linking process failed because ‘make’ doesn’t find
the object files. I have to run ‘make’ again to link the object files
correctly.
Is there any possibility that I have to run ‘make’ only once?

But I do like Brian’s recommendation better than just making this fix.


Bill Caroselli – Q-TPS Consulting
1-(626) 824-7983
qtps@earthlink.net