Thread: Changes in header files and make to the rescue

  1. #1
    Registered User
    Join Date
    Oct 2018
    Posts
    42

    Changes in header files and make to the rescue

    I know "Make" is a cool software and I've been using it for a while, but when some people reason about its benefits, and when they explain how it helps programmers with managing the unwieldy dependency tree of their projects, one thing they talk about is that changing something in a header file, can cause a kind of trouble, that by using "Make" we can overcome. But I don't get it exactly, when we make some changes in a header file, there are two possibilities:

    1_ We reflect those change to all related source files too and so things will be all fine.

    2_ We forget to fix at least one of the source files(very likely), and then, when trying to link the whole project together, the linker shows us an error because it's now missing a symbol(maybe we have removed it from the header file, maybe we have changed it...) and then by looking at the error, we can fix all those reported places. And then recompile each unit and link the whole project once again.

    Now, if we had used Make, how would this process have been different? the process doesn't sound much different to me, you type "make" and the linker throws the error in just the same way. Then you read the error, and go to fix them all! Where am I wrong?

    I personally think the main benefit of "make" is comfort in typing. it's very simple to only type a single "make", each time a change is made, rather than typing the whole specific compile command again and agian.
    I even think the difficulty with remembering the dependencies is not a real benefit of make spince we can use "gcc -MM *.c" command(although it's crazy, it's possible) for that.
    Also for the importance of order in some compilations, although it's true, it must happen rarely IMNO(in my noobish opinion). Let's assume obj1.o has to be always up to date BEFORE obj2.o gets compiled (from src2.c), now say I forget this order after a while, and I change something in obj1.o , and don't compile src2.c again. Now when I try to link, the linker gives error, because again it can not match some parts of our objects. So we go and fix them all!

    P.S.: I feel I'm terribly wrong about all these but still, that's why we ask questions anyway

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The manual entry on How make Processes a Makefile might help:
    make reads the makefile in the current directory and begins by processing the first rule. In the example, this rule is for relinking edit; but before make can fully process this rule, it must process the rules for the files that edit depends on, which in this case are the object files. Each of these files is processed according to its own rule. These rules say to update each ‘.o’ file by compiling its source file. The recompilation must be done if the source file, or any of the header files named as prerequisites, is more recent than the object file, or if the object file does not exist.
    So, with this approach, assuming that you have kept the makefile updated with the header prerequisites, you wouldn't have to sort out which source files need recompiling and which do not, or to always rebuild the entire project, as make would work that out for you, recompiling only when necessary.

    EDIT:
    I guess you can look at it this way: if there is a problem with your code (e.g., you change a header and forgot to change corresponding source files), no build tool is going to save you. But if there is problem with your build process (e.g., you changed a header and corresponding source files, but forgot to recompile one of the affected source files, or don't want to always rebuild the entire project), that's where build tools come in handy.
    Last edited by laserlight; 01-16-2019 at 06:48 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2018
    Posts
    42
    Quote Originally Posted by laserlight View Post
    (e.g., you change a header and forgot to change corresponding source files), no build tool is going to save you.
    Thanks, this is logical.

    Quote Originally Posted by laserlight View Post
    you changed a header and corresponding source files, but forgot to recompile one of the affected source files, or don't want to always rebuild the entire project
    So what's wrong with just recompiling the affected source file whenever we remember? E.g: When we see our change did not have any effect. (I guess you want to say: on large projects, where several objects are edited for a single change, it's hard to keep track of the list of files we worked on recently).

    I think for whatever issue that "make" solves, there will be an error thrown in case we don't use "make"(and in a contrast, I mean those errors are like compile-time errors, that are just thrown, and we can fix them. They are not like run-time errors that crash the program and are confusingly hard to locate.), so make is essentially better for ease of typing many commands. Do you agree?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by narniat
    So what's wrong with just recompiling the affected source file whenever we remember? E.g: When we see our change did not have any effect.
    You would have wasted time having to check to see that the "no effect" is because you forgot to compile rather than because of a bug in your code. Put it another way: what's wrong with getting it right on the second or third try when you could have easily gotten it right on the first try? If you really would rather not get it right on the first try when you can easily do so... I guess you can keep programming a little slower than the rest of us, but I don't recommend skydiving as a hobby.

    Quote Originally Posted by narniat
    so make is essentially better for ease of typing many commands. Do you agree?
    No, as that is an oversimplification. If you wanted ease of typing many commands, just dump the commands in a shell script. Rather, one of the main motivations of using a build tool is to have an easily reproducible build process instead of having to manually keep track of different steps and reproduce them correctly time and again.
    Last edited by laserlight; 01-16-2019 at 08:39 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2018
    Posts
    42
    Oh haha, you're right. Of course skydiving is better.

    the main motivations of using a build tool is to have an easily reproducible build process
    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Make utility and Header Files.
    By Mr.Lnx in forum C Programming
    Replies: 6
    Last Post: 04-16-2013, 08:49 AM
  2. Trying to make the best use of header files
    By alanb in forum C++ Programming
    Replies: 4
    Last Post: 08-31-2009, 04:45 PM
  3. the proper way to make .h/header files
    By revelation437 in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2004, 01:22 AM
  4. Replies: 2
    Last Post: 08-08-2003, 04:36 PM
  5. Help with Header/Make files
    By Unregistered in forum C Programming
    Replies: 0
    Last Post: 04-10-2002, 10:57 PM

Tags for this Thread