tags:

views:

82

answers:

5

I have a project with 50+ .h files and 50+ .cpp files. I'm using make to build a project, which looks something like this (it's just a piece of an entire file):

HEADERS := $(shell find $(INCLUDE) -name "*.h")
%.obj: %.cpp $(HEADERS)
    $(CPP) $(CPPFLAGS) -fPIC -o $@ -g -c $<

When I'm making changes to one .h file, the whole project has to be re-compiled. It's annoying and time-consuming. But I don't want to hard-code file dependencies inside Makefile, since it's even more time-consuming. I would like to have some make-like tool, which will find dependencies right inside my .cpp/.h files, automatically. Any suggestions? Thanks in advance!

+3  A: 

Scons is good in finding the right sources that need to be recompiled. It scans the files and the headers and builds up an internal dependency tree. www.scons.org

Haplo
Moving from make to scons was a very nice experience. Having a configuration file written in python gives a lot of flexibility (it was always difficult for me to modifiy my obscure Makefile), and I'm much more confident in the dependencies management (furthermore, scons knows what to do when the configuration file itself is modified).
rafak
+5  A: 

There are many options, but nothing that works exactly like make. Many build systems will generate the Makefiles with dependencies for you though. CMake and Automake are two commonly used systems that work like that. You define your project file in a meta-language and it will generate the Makefiles.

Lukáš Lalinský
+1 for suggesting Automake, oh my darling Automake.
Staffan
+3  A: 

If you are using gcc then you can use the -M option. It is designed to do exactly what you want i.e. generate a Makefile rule describing the dependencies.

Troubadour
A: 

Jam.

Boost.Build.

The latter is based on a (massively) enhanced version of the former.

atzz
+1  A: 

Simplest way:

depend:
    g++ -M *.cpp >.depends

-include .depends

Better way:

SRC=foo.cpp bar.cpp ...
OBJ=$(patsubst %.cpp,%.o,$(SRC))
DEPS=$(patsubst %.o,.deps/%.o.dep,$(OBJ))

all: .deps

.deps: 
     mkdir -p .deps

.cpp.o:
    $(CXX) $(CXXFLAGS) -MD -MF .deps/[email protected] -c -o $@ $<

-include $(DEPS)

So compiler will generate all dependencies for each file during build automatically.

Or even better: use Autotools, CMake or other build system that does this job for you.

Artyom