1

Reading the official documentation

A prerequisite is a file that is used as input to create the target. A target often depends on several files.

If my source file already includes the header, should I list the header in the rule?

src.c

#include <stdio.h>
#include "myheader.h"

int main()
{
    printMessage();
    return 0;
}

myheader.h

void printMessage()
{
    printf("a message to screen\n");
}

makefile

src : src.o
    cc -o src src.o

src.o : src.c
    cc -c src.c

If I add myheader.h in the prerequisite it changes nothing, the same message is printed to screen. If a header is explicitly included, should it appear in the prerequisite?

Tony Tannous
  • 14,154
  • 10
  • 50
  • 86

2 Answers2

3

The header file should be included in the dependency list.

The first time you use make to build your program, it will compile just the same whether you include myheader.h as a dependency or not. The difference is what happens when one of the files changes.

If you run make again without changing anything, it will say that "src" is up to date and won't compile anything. However, if you were to modify myheader.h and you didn't include it as a dependency, then make will say that the target is up to date. It doesn't look at the source file to see what files it includes. The make utility doesn't know anything about C or C++ source code (or any other source code). It looks only at whether the given files have changes without looking at their content.

When you include myheader.h as a dependency, if you later modify that file then running make will rebuild the program.

If you want to know all the non-system header files that a given source file depends on, you can run gcc with the -MM option. This will output a make rule listing the header dependencies of a source file.

For example, if you run gcc -MM src.c, you'll get the following output:

src.o: src.c myheader.h

You can then include that in your makefile.

dbush
  • 205,898
  • 23
  • 218
  • 273
2

Yes, you should.

The make program uses the list of files to figure out if a dependency changed and the the target should be rebuilt as a result. It needs you to specify that dependency explicitly.

It does not see the inclusion, it only sees the rules you specified. So there is a theoretical possibility that you change the header in a way that may require a re-compilation of src.o, but make will not know you did that unless you tell it to watch out.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
  • If header file A is including different header file B, and the `.c` is including the `A.h`, should I specify `B.h` too incase `B.h` changes? or would specifying `A.h` alone be enough? – Tony Tannous Oct 03 '18 at 12:27
  • @TonyTannous - Personally I'd prefer to have `A.h` as a dummy target that depend on `B.h` (by dummy I mean, it doesn't execute anything, only lists dependencies). This is where managing a makefile can become difficult, and a tool like CMake becomes useful. – StoryTeller - Unslander Monica Oct 03 '18 at 12:32
  • I couldn't fine any paper or a proper article when to write my own make files or to use cmake. I have 4 projects which include quite few source files and header files. If you have any source to read I'd appreciate it. – Tony Tannous Oct 03 '18 at 12:33
  • 1
    @TonyTannous - Writing those things is in part art form. It's hard to figure out what's best until after you went past the learning curve. What helped me upon starting is [Mastering Cmake (Amazon link)](https://www.amazon.co.uk/Mastering-CMake-Ken-Martin/dp/1930934319), and the official cmake tutorial/reference. The `verbose` option for `cmake` also helped me figure out where I was bungling it up. – StoryTeller - Unslander Monica Oct 03 '18 at 12:36
  • Hi there, can't ask this directly on SO but I respect your opinion so asking in a comment. How would you recommend for a C programmer to learn c++ and then moving to c++14 things? to start immediately in c++14 or learn basics of c++ first? – Tony Tannous Jan 31 '19 at 10:50
  • @TonyTannous - Starting out from C++14 is not a bad idea, might as well get accustomed to current code. The biggest challenge is getting into the mindset of C++. I started out as a C programmer myself. I think [books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) are actually the best way to get up to speed on that. Bjarne's *A Tour of C++* is a good place to start. Then the "Effective C++" series is a must, the advice there is always relevant. And then just code, but I don't need to tell you that :) – StoryTeller - Unslander Monica Jan 31 '19 at 11:01
  • 1
    Thanks! I will try to learn as much as possible in the next 2 weeks and then jump in deep water. – Tony Tannous Jan 31 '19 at 11:08