Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect header changes in make dependency list

Tags:

c++

makefile

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…

like image 693
JohnP Avatar asked Aug 12 '10 21:08

JohnP


People also ask

How do you check if a header file is included?

To check if an header file has been included or not in a C or C++ code, we need to check if the macro defined in the header file is being defined in the client code. Standard header files like math. h have their own unique macro (like _MATH_H ) which we need to check. Consider this example of checking if math.

Where does gcc look for headers?

GCC looks for headers requested with #include " file " first in the directory containing the current file, then in the directories as specified by -iquote options, then in the same places it would have looked for a header requested with angle brackets. For example, if /usr/include/sys/stat. h contains #include "types.

What are dependencies in makefiles?

A dependency is a file that is used as input to create the target. A target often depends on several files. A command is an action that make carries out. A rule may have more than one command, each on its own line.

Why is a makefile used?

Makefile sets a set of rules to determine which parts of a program need to be recompile, and issues command to recompile them. Makefile is a way of automating software building procedure and other complex tasks with dependencies. Makefile contains: dependency rules, macros and suffix(or implicit) rules.


1 Answers

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.

like image 108
deinst Avatar answered Sep 21 '22 23:09

deinst