tags:

views:

47

answers:

1

Hi everyone,

After some time using make to build C++ programs I still don't have a very good knowledge about Makefiles. I am thinking about asking for a "good" example and use it from now on. I have been searching, but the ones I found is too complicated for me to understand. Please give me a template, with comments explaining how it works.

Thanks.

+1  A: 

Makefiles have the tendency to get really hairy really fast, particularly when working with multiple directories. Many of the Makefile I came across in my professional life where little more then glorified shell scripts with the dependency part mostly non existent. This kind of problems were noted by the seminal paper recursive make considered harmful.

There, and in a following article Implementing non-recursive make -- you can find a reasonable template.

Also here and here on SO you can find people searching for the illusive Makefile(s) template.

Typically, the good Makefile I have seen where the result of an expert that worked for several months and created an infrastructure that transformed the Makefile syntax into something almost completely different. The developers just needed to make assignment to special variables, include the magic code, and build.

The next step in this evolution, is a more modern tool such as CMake. CMake will generate well formed Makefiles for you. If you have control over it, please consider such a tool.

IMHO you will find it makes much more sense, and make you much more productive, give you the added value of cross platform and support the entire build process (including configuration, packaging, Continuous Integration etc.)

Chen Levy
I'd recommend `automake` over `CMake`. `CMake`'s concept of lists is completely busted (sometimes whitespace-separated, sometimes semicolon-separated and the semicolons often find their way into compiler flags). The `pkg-config` macro is a particularly bad case. `automake` may not be pretty (although the new `AM_SILENT_RULES` is nice), but it works.
Jack Kelly
@Jack Kelly, you are probably much smarter then I. As hard as **I** tried, I was unable to make sense of the autotools suite. For me `CMake` was easy to grok. I suspect that if @phunehehe found Makefiles hard to understand, `automake` will not be easier for him.
Chen Levy
@Chen Levy: I would be interested to see what you read when trying to learn the autotools. The amount of legacy ways to do things that are still copied around on the internet is unfortunate and doesn't help the autotools reputation for being a bit of a black art. I found this one to be a good start: http://www.lrde.epita.fr/~adl/autotools.html
Jack Kelly