Simple header dependency generation

Programming, for all ages and all languages.
Post Reply
TylerH
Member
Member
Posts: 285
Joined: Tue Apr 13, 2010 8:00 pm
Contact:

Simple header dependency generation

Post by TylerH »

Here's an example I thought may be useful to some. It took me the last two years of trying and giving up and trying again to finally come up with this.

Code: Select all

BINDIR	=
OBJDIR	=obj
HEADERS	=$(wildcard *.hpp)
SOURCES	=$(wildcard *.cpp)
OBJECTS	=$(foreach SOURCE, $(SOURCES), $(patsubst %.cpp, %.o, $(SOURCE)))
TARGETS	=number-theory

CXX	=clang++
CXXFLAGS	=-ggdb -std=c++0x -Wall
LD	=clang++
LDFLAGS	=-ggdb -lgmpxx

.PHONY : all
all : $(TARGETS)

.PHONY : clean
clean :
	$ rm -f -r $(OBJECTS)

number-theory : $(OBJECTS)
	$(LD) $(LDFLAGS) -o nt $(OBJECTS)

define cxx-rule
$(subst \,, $(OBJDIR)/$(strip $(shell $(CXX) -MM $(1))))
	$(CXX) $(CXXFLAGS) -c -o $(OBJDIR)/$(strip $(patsubst %.cpp, %.o, $(1))) $(1)
endef

$(foreach SOURCE, $(filter %.cpp, $(SOURCES)), $(info $(call cxx-rule, $(SOURCE))))
As you may notice, I don't support C or asm files. This isn't a kernel makefile. I haven't felt like messing with kernel programming for a while now. But, the automatic header dependency generation is still useful.

If anyone wants help extending it to support C and asm files, I can help with that. I intend to do it anyway, whenever I get back in the mood to do some kernel programming.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Simple header dependency generation

Post by Solar »

TylerH wrote:It took me the last two years of trying and giving up and trying again to finally come up with this.
If you just had asked... there's automatic header dependency generation covered in the Makefile tutorial, which works for C, C++, and ASM using C-style includes passed through GCC... :(

I can't right now make out if your approach gives much different results (for the better or the worse).
Every good solution is obvious once you've found it.
Nable
Member
Member
Posts: 453
Joined: Tue Nov 08, 2011 11:35 am

Re: Simple header dependency generation

Post by Nable »

*scratch*
Just a part of Makefile that i use:

Code: Select all

...
.depend: $(SRCS)
        @echo "Creating .depend"
        $(CC) -MM -MG $(SRCS) > .depend

ifneq ($(MAKECMDGOALS), clean)
include .depend
endif
...
Is that related to topic?
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Simple header dependency generation

Post by Solar »

Yes, it is.

You might also want to have a look at the tutorial. It creates the dependency files on the fly, i.e. alongside the actual compilation, and generates one .d file per translation unit. This makes for less compiler invocations, you don't need a separate build rule for dependencies, and it minimizes the work necessary when only some source files are touched. (Your way, the compiler has to parse all source files every time one of them is touched.)
Every good solution is obvious once you've found it.
TylerH
Member
Member
Posts: 285
Joined: Tue Apr 13, 2010 8:00 pm
Contact:

Re: Simple header dependency generation

Post by TylerH »

Yeah, I saw the example in the tutorial before I did this. I just didn't like the idea of having more files in my directory or even another directory in my root. Right now, my build and source directories are one and the same, so the fewer directories and files that get created during compilation, the better. I hate a dirty source directory.

But, that said, the tutorial example does have some significant merits over mine, but only in large projects. In my example, the dependencies are generated for all files, unconditionally; whereas the tutorial example only updates dep files if the src has been changed. However, taking the size of my project into consideration, for my use, the performance difference is negligible.

Hopefully someday I'll muster the effort to create a project big enough for compile time to matter, but I don't forsee that day coming anytime soon; especially now that I'm in college.
Post Reply