Live data from Hacker News

One line you should add to every makefile

blog.jgc.org

21–30 of 99 posts

Re: One line you should add to every makefile

#21

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…

It may be my inexperience speaking, but every time I come across code that was built not by hand I visibly cringe and search for something else. It's not that it's bad, it's just that if I don't understand it then there is nothing for me to fix or play around with.

Re: One line you should add to every makefile

#23

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.

Once you start to need loops, conditions, functions to factor common code, switches for production/staging/development builds etc. I don't find Make elegant or easy to maintain even when knowing the syntax personally.

Re: One line you should add to every makefile

#25
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?

I don't use autoconf myself. If I did, I'd definitely copy it from an existing project.

Re: One line you should add to every makefile

#27

Better article from years ago, that this blogger is probably rehashing anyway: http://blog.melski.net/2010/11/30/makefile-hacks-print-the-v...

that this blogger is probably rehashing anyway

Nope.

Eric (who wrote that blog you are referrring to is a friend), but I wrote this little trick up years before him: http://www.cmcrossroads.com/article/printing-value-makefile-... and now I'm rehashing my own writing 10 years later.

Re: One line you should add to every makefile

#28
You can avoid editing the Makefile to add the rule with GNU make 3.81 or older, which does not support --eval. Create a new file in your home named ~/Makefile.debug with the contents:

  print-%: ; @echo $*=$($*)
  include Makefile
Now you can use it the following way from any source directory:

  $ make -f ~/Makefile.debug print-SOURCE_FILES

Re: One line you should add to every makefile

#29
post #21

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…

It may be my inexperience speaking, but every time I come across code that was built not by hand I visibly cringe and search for something else. It's not that it's bad, it's just that if I don't understand it then there is nothing for me to fix or play around with.

It's best to treat it the same way you treat assembler when compiling a C++ program. Normally, you wouldn't inspect the generated assembler. If you do, you will find it very hard to follow, but that's the disadvantage that comes with the benefit that you don't have to write it all by hand. I consider generated makefiles the same. I don't commit them to my repository, most of the times I don't look inside them, it's just an intermediate product of my build system.

Re: One line you should add to every makefile

#30

Earlier quoted context omitted.

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, s…

How do they track the dependencies automatically?
Post reply on HN