Page 1 of 1

More Makefile Automation

Posted: Fri Jun 22, 2007 1:37 am
by AJ
Hi,

I have followed Solar's makefile tutorial on the wiki which was extremely useful (up to now, I had been adding each object file separately).

One question with the automation side though. How can I make it so that all my objects are compiled in a single directory? I'll show you the problem:

Code: Select all

OBJFILES := $(patsubst %.c,%.o,$(SRCFILES))
This is fine, but as you can probably see, it means that objects are compiled in to the source directory. Of course, the following:

Code: Select all

OBJFILES := $(patsubst c/%.c,obj/%.o,$(SRCFILES))
resolves the problem, but *only* if all source files are in the same directory, "c/". How do I substitute the .c for .o, and substitute *any path* for obj/? I know that if I want my source directories less cluttered I could just use the mv command, but the make will always do a full build.

Any help from anyone more familiar with makefiles is appreciated!
Cheers,
Adam

Posted: Fri Jun 22, 2007 5:04 am
by os64dev
problably you don't want to build all in one directory because that means that any source file you have needs to have a unique name. If not you will overwrite an earlier compiled file with all the problems associated with that.

to build my object files i have the followin entry in the makefile:

Code: Select all

obj/%.co: src/%.cc
	    @$(utlCC) $(CC_FLAGS) -O3 -c -I./inc -MD -MT $@ -MF $(@:%co=%cd) -o $@ $<
	    @$(utlOD) -trds $@ > $(@:%co=%ct)
	    @echo build.. $@
this will build all the sources in any directory within the src directory to the same directory in the obj directory.

thus src/kernel/main.cc will be build as obj/kernel/main.co
thus src/app/main.cc will be build as obj/app/main.co

no name clashes and build is left out of the source tree.
only problem is that you need the sub directories of obj but that should not be a problem.