tags:

views:

45

answers:

3

After almost a decade of C# and VC++ coding, I am getting back to a linux – g++ - make – emacs environment.

Trying to refresh my memory about writing a make file I did not have many problems, but I stumbled in the following issue, that I admit I do not remember how I solved it in the past:

Let’s say that a particular .cpp file have some dependencies to several other header files (setting aside its corresponding header which is easy to handle)… What is the best way to detect that some of the .h were changed?

I certainly do not like the idea of placing them in my target – depend list since this is a manual and error prone process!

The easy answer is of course to build clean whenever there is a .h change but I cannot really recall what was the standard way….

In VC++ I did not have to deal with this since the IDE was very good at handling dependencies…

+1  A: 

Look at automated dependencies. gcc with the -M -MD -MT etc. flags will parse your file and compute dependencies. Pass them through sed. There are many examples that google will find.

deinst
A: 

As @deinst mentioned, gcc has an ability to output all header files your cpp uses directly and indirectly. What you need is roughly following:

obj-file: src-file

# dep file should be recreated each time header file or header file it depends on is changed
dep-file: src-file 
    gcc -M ...... > dep-file
    modify dep-file to make dep-file depend and all header files too

include dep-file

It was implemented once in project I was engaged in, but the full implementation of the idea was long and tricky one.

If you have a possibility consider higher tools that support autodepency build it such as cmake or scons. I personaly worked with cmake - each programmer can write a cmake file and autodepency is for free.

dimba
+2  A: 

deinst and idimba have you on the right track. To read about the potential pitfalls you might encounter and how to avoid them, take a look at this article.

http://mad-scientist.net/make/autodep.html

Ken Smith
+1 for a nice link
dimba