Live data from Hacker News

One line you should add to every makefile

blog.jgc.org

11–20 of 99 posts

Re: One line you should add to every makefile

#11
post #3

I firmly believe that there is only one production-quality makefile that has ever been written from scratch. Every other makefile has been copied from an earlier makefile, dating back to the one Ur-makefile, and modified to suit the author's purpose.

We wrote several Makefiles from scratch at work.

And a dependency-graph-of-thing-we-work-with to Makefile generator, like the output of gcc -M does.

In fact its a whole build system using gnu make, written from scratch.

Re: One line you should add to every makefile

#13
post #2

The mere fact that such a thing is necessary disqualifies make as a build tool for me. That being said, this trick could be handy for existing projects.

> The mere fact that such a thing is necessary disqualifies make as a build tool for me.

It's a very simple debugging addition. Hardly necessary, just convenient. The "built-in" echo works just fine as well.

Re: One line you should add to every makefile

#14
post #4

That's one huge advantage with Rake, just add `p VARIABLE_NAME` anywhere in your Rakefile, and you'll see the textual value of it at that point.

That misses the point; you can already echo a variable wherever you want in a makefile similarly. This gives you a way to do it without editing the makefile, without knowing before hand you want to see that variable.

Re: One line you should add to every makefile

#15

I'd say that most people should not write makefiles by hand. You should use some higher level build system, that knows how to deal with source files in your language, track their dependencies, etc. A "hello world" makefile with one source file looks simple, but once the project gets more complicated, you quickly end up building such a high level build system yourself, with the additional disadvantage of restricting y…

Makefiles, once you have learned the syntax, are very simple to write, as they can be naturally decomposed into individual steps, and rules are easily generalized for similar file types.

Their reputation has been tarnished by autotools, but in that case you're using autotools as your build system, not make.

Re: One line you should add to every makefile

#16
post #3

I firmly believe that there is only one production-quality makefile that has ever been written from scratch. Every other makefile has been copied from an earlier makefile, dating back to the one Ur-makefile, and modified to suit the author's purpose.

When I started with make, I copied working files to a safe place, copied from the safe place to a scratch place, tweaked the scratch, got it working, etc., etc. Managing the make file was itself part of managing the project.

Then I read and thoroughly grokked the O'Reilly book on make (which some douche stole from my desk; twenty plus years on, I am still bitter about that one... ...don't miss the Tannenbaum OS book or the Stevens networking books, but the make book, it burns, it burns....)

After that all I wrote all of my make files from scratch. Doing so made sure I understood my project, its dependencies, etc., and made sure I wasn't going to accidentally corrupt a build with some edge case from another one.

make is super straightforward once you get how it works. Sort of like git rebase, I suppose (still working on that one... :->).

Re: One line you should add to every makefile

#17
I think this article is a great reference for others wanting to write a small informational or how-to post. It's concise and easy to follow for those who are familiar with the subject while including enough details for beginners.

Back to the subject, I would also be interested to know a way to print out the value of a variable every time it changed in the course of a make system's execution.

Re: One line you should add to every makefile

#18

I'd say that most people should not write makefiles by hand. You should use some higher level build system, that knows how to deal with source files in your language, track their dependencies, etc. A "hello world" makefile with one source file looks simple, but once the project gets more complicated, you quickly end up building such a high level build system yourself, with the additional disadvantage of restricting y…

Makefiles, once you have learned the syntax, are very simple to write, as they can be naturally decomposed into individual steps, and rules are easily generalized for similar file types. Their reputation has been tarnished by autotools, but in that case you're using autotools as your build system, not make.

The problem is that C/C++ files have internal depenedencies which make needs to know about. Yes, for gcc you can use a simple trick to extract the dependencies to makefile rules, but in case you use a different compiler, you need to do it yourself. If you are compiling a different language, you need to do it yourself again. Using something like CMake or even automake, which can automatically track the dependencies, saves you a lot of work.

Re: One line you should add to every makefile

#19
post #8

Earlier quoted context omitted.

autoconf yes, but Makefiles, no. I'll write a new Makefile from scratch maybe once a year or so.

If you use autoconf, why not use also automake?

If you've already shot yourself in the foot, why not also shoot yourself in the face?

Re: One line you should add to every makefile

#20
post #4

That's one huge advantage with Rake, just add `p VARIABLE_NAME` anywhere in your Rakefile, and you'll see the textual value of it at that point.

You can add echo commands to Makefiles just as easily. Rake is cool for Ruby programs, but that's about it.

Personally I find Rake awful for Ruby work too because you often need to read through a Ruby program to figure out what they do, and so many tools include custom Rake integration that makes you hunt down documentation elsewhere.

I usually use Makefiles for my Ruby projects.

Post reply on HN