Makefile include dependency file




















Why automate inclusion of header file dependencies : While working on a new project, I had to write a makefile from scratch. I found it little challenging to find out how to automatically include header dependencies in makefile, because everytime you include some header file in your source file, you can not modify your makefile.

So, I thought, there must be some mechanism that automatically takes care of it. If you search on google, you will find various ways. Manual solution is to use sed command that I guess searches for includes in your source code and puts all those dependencies in your makefile. There are various default macros. You can see them by typing "make -p" to print out the defaults. Most are pretty obvious from the rules in which they are used.

These predefined variables, i. Here is a table of variables whose values are additional arguments for the programs above. The default values for all of these is the empty string, unless otherwise noted. Extra flags to give to the C preprocessor and programs, which use it such as C and Fortran compilers. It is very common that a final binary will be dependent on various source code and source header files. Dependencies are important because they let the make Known about the source for any target.

Here, we tell the make that hello is dependent on main. Hence, whenever there is a change in any of these object files, make will take action. At the same time, we need to tell the make how to prepare. In the above code, the arguments in brackets are optional and ellipsis means one or more. Here, note that the tab to preface each command is required.

A simple example is given below where you define a rule to make your target hello from three other files. The semantics is very simple. When you say "make target", the make finds the target rule that applies; and, if any of the dependents are newer than the target, make executes the commands one at a time after macro substitution. If any dependents have to be made, that happens first so you have a recursion. Make terminates if any command returns a failure status.

Make ignores the returned status on command lines that begin with a dash. For example, who cares if there is no core file?

Make echoes the commands, after macro substitution to show you what is happening. Sometimes you might want to turn that off. People have come to expect certain targets in Makefiles. You should always browse first. However, it is reasonable to expect that the targets all or just make , install, and clean is found. The command is one that ought to work in all cases where we build an executable x out of the source code x. This implicit rule says how to make x out of x.

The rule is implicit because no particular target is mentioned. With not too much modification, we can ask gcc to output a dependency file next to each object file that is produced. We ask make to include these dependency files where available:. The rationale for doing this is described in Autodependencies with GNU make , although this article dates from a time when apparently only -MM was available.

Viewed k times. Improve this question. Mike Mike Having written my answer below I looked in the related list and found: stackoverflow.

Chris Dodd's answer is equivalent to mine, though it uses a different naming convention. Add a comment. Active Oldest Votes. Makefile fragment: depend:. Improve this answer. I like this trick, but how can I get depend to run only when the source files have changed? It seems to run every time regardless That's what I get for typing from memory. Try it now. Is it way to add before every file some prefix to show that it is in another directory e. That seemed to prevent depend from running every time and also caught changes to the header files only.

This seems counter to the previous comments.. Show 1 more comment. Community Bot 1 1 1 silver badge. Sophie Sophie 1, 7 7 silver badges 12 12 bronze badges. Thanks ctrucza, should be fixed now. Out of the box, this failed to locate the headers for me even though hpp and cpp are both on the same dir.



0コメント

  • 1000 / 1000