JohnP
JohnP

Reputation: 687

How to detect header changes in make dependency list

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…

Upvotes: 6

Views: 4039

Answers (3)

dimba
dimba

Reputation: 27581

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 autodependency build it such as cmake or scons. I personaly worked with cmake - each programmer can write a cmake file and autodependency is for free.

Upvotes: 1

deinst
deinst

Reputation: 18782

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.

Upvotes: 7

Ken Smith
Ken Smith

Reputation: 805

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

Upvotes: 2

Related Questions