help-make
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Implicit Rules with Different Source and Object Directory Question


From: redcomet
Subject: Re: Implicit Rules with Different Source and Object Directory Question
Date: Fri, 27 Nov 2009 09:52:31 -0800 (PST)

I have a working solution now that I stumbled onto while reading a ton of
make documentation.

My motivation for this redesign is to mimic the functionality of visual
studio's build process.  My folder structure is designed like so

/root/mysolution/
-->bin
-->common/
-->-->src
-->-->include
-->myproject/
-->-->src
-->-->include
-->-->build

and I wanted all my release code objects to be put in build/Release, and
debug objects to be put in build/Debug

I wanted to reference any source in any subdirectory of src and have its
object appear in the build directory WITHOUT its directory.  That way all
objects sit in build/Release or build/Debug

This is how visual studio builds projects and I finally got a working
makefile that operates the same way.
I wanted to post the key parts for anyone else who likes this design and
wanted to mimic it in linux.

You start off with your sources list
SOURCES = \
main.c \
linux/printline.c \
linux/dosomething.c \
../common/src/getopt.c
 
translate the list into a list of objects with
OBJECTS := $(patsubst %.c, $(BUILD_DIR)/Release/%.o, $(notdir $(SOURCES)))

this is your building rule which will compile all the c files (and make the
directory if it does not exist)
$(BUILD_DIR)/Release/%.o: %.c
        address@hidden -d $(dir $@) ] || mkdir -p $(dir $@)
        $(CC) $(INCLUDE) $(CFLAGS) $(DEFINES) -o $@ -c $<

and this is the rule to link it all together
release: $(OBJECTS)
        address@hidden -d $(dir $@) ] || mkdir -p $(dir $@)
        $(CC) $(LDFLAGS) -s $^ $(DEPENDS) -o$(BIN_DIR)/Release/$(BINARY)

These rules depend on a correct VPATH to find all the sources.  I found out
that you can auto generate the VPATH from the sources list like so
VPATH = $(addprefix $(SOURCE_DIR)/, $(dir $(SOURCES)))

and thats all it takes.

If you are wondering why I do not want want the directories to be made in
the build directory look back at my sources list.
I wanted to include a source file in the common directory but I do not want
to include ALL the source files in the common directory.  Since creating a
library is out this is the next best option.  
If I let the directories be created in build/Release based on the source
list directory I would end up with
build/common/getopt.c
instead of build/Release/getopt.c
or even build/Release/common/getopt.c

which is undesirable.

I know generating a VPATH like this is a bit of a hack, but considering the
hacks seen in most other makefiles I think this one is pretty mild. :)

I am in the process of implementing some auto dependency stuff so changes to
header files recompile the c file.  That promises to be another bag of worms
but at least one problem is solved.

-- 
View this message in context: 
http://old.nabble.com/Implicit-Rules-with-Different-Source-and-Object-Directory-Question-tp26520309p26545353.html
Sent from the Gnu - Make - Help mailing list archive at Nabble.com.





reply via email to

[Prev in Thread] Current Thread [Next in Thread]