
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Vjeux &#187; Makefile</title>
	<atom:link href="http://blog.vjeux.com/category/makefile/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.vjeux.com</link>
	<description>French Web Developer</description>
	<lastBuildDate>Wed, 21 Jul 2010 01:30:44 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Makefile &#8211; Automatic Dependencies with makedepend</title>
		<link>http://blog.vjeux.com/2009/makefile/makefile-automatic-dependencies-with-makedepend.html</link>
		<comments>http://blog.vjeux.com/2009/makefile/makefile-automatic-dependencies-with-makedepend.html#comments</comments>
		<pubDate>Fri, 20 Nov 2009 15:23:53 +0000</pubDate>
		<dc:creator>vjeux</dc:creator>
				<category><![CDATA[Makefile]]></category>

		<guid isPermaLink="false">http://blog.vjeux.com/?p=613</guid>
		<description><![CDATA[Having to deal with dependencies in Makefile is a real pain, there are a lot of examples of way to deal with it on the web but none of them is satisfying.
For example using gcc -MM does not work with subfolders, a depend rule requires the user to use it everytimes he adds new files [...]]]></description>
			<content:encoded><![CDATA[<p>Having to deal with dependencies in Makefile is a real pain, there are a lot of examples of way to deal with it on the web but none of them is satisfying.</p>
<p>For example using <code>gcc -MM</code> does not work with subfolders, a <code>depend</code> rule requires the user to use it everytimes he adds new files ...</p>
<p>Here is what is wanted:</p>
<ul>
<li>Completly automatic: No user interaction is required when adding new files</li>
<li>Works with an arbitrary amount of files</li>
<li>Works with an arbitrary amount of level of folders</li>
<li>Is not recalculated when nothing changed</li>
<li>Use only one file to store dependencies</li>
<li>Do not depend on complicated regular expressions</li>
</ul>
<p>Here is the result:</p>

<div class="wp_syntax"><div class="code"><pre class="make" style="font-family:monospace;">BINARY  <span style="color: #004400;">=</span> project<span style="color: #004400;">.</span>exe
CC      <span style="color: #004400;">=</span> gcc
CFLAGS  <span style="color: #004400;">=</span> 
FILES   <span style="color: #004400;">=</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #0000CC; font-weight: bold;">shell</span> find src<span style="color: #004400;">/</span> <span style="color: #004400;">-</span>name <span style="color: #CC2200;">&quot;*.c&quot;</span><span style="color: #004400;">&#41;</span>
HEADERS <span style="color: #004400;">=</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #0000CC; font-weight: bold;">shell</span> find src<span style="color: #004400;">/</span> <span style="color: #004400;">-</span>name <span style="color: #CC2200;">&quot;*.h&quot;</span><span style="color: #004400;">&#41;</span>
OBJS    <span style="color: #004400;">=</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span>FILES<span style="color: #004400;">:.</span>c<span style="color: #004400;">=.</span>o<span style="color: #004400;">&#41;</span>
&nbsp;
all<span style="color: #004400;">:</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">BINARY</span><span style="color: #004400;">&#41;</span>
&nbsp;
<span style="color: #004400;">-</span><span style="color: #666622; font-weight: bold;">include</span> Makefile<span style="color: #004400;">.</span>deps
&nbsp;
<span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">BINARY</span><span style="color: #004400;">&#41;</span><span style="color: #004400;">:</span> Makefile<span style="color: #004400;">.</span>deps <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">OBJS</span><span style="color: #004400;">&#41;</span>
        <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">CC</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">CFLAGS</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">OBJS</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">-</span>o <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">BINARY</span><span style="color: #004400;">&#41;</span>
&nbsp;
Makefile<span style="color: #004400;">.</span>deps<span style="color: #004400;">:</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">FILES</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">HEADERS</span><span style="color: #004400;">&#41;</span>
        makedepend <span style="color: #004400;">--</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">CFLAGS</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">--</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">FILES</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">-</span>f<span style="color: #004400;">-</span> <span style="color: #004400;">&gt;</span> Makefile<span style="color: #004400;">.</span>deps</pre></div></div>

<p>This is in fact really easy. In your <code>$(BINARY)</code> rule, you add <code>Makefile.deps</code> as a prerequisite.</p>
<p>In order to generate the <code>Makefile.deps</code> you mark all <code>$(FILES)</code> and <code>$(HEADERS)</code> as prerequisite, so every time you change a file or header it will recompile the list.<br />
We use makedepend to generate the dependencies list. It works like <code>gcc -MM</code> except that it outputs the correct file path when used with folders.</p>
<p>Then all is required is to include the <code>Makefile.deps</code>. We include it with <code>-include</code> so it does work the first time you compile.</p>
<p>Thanks to Lemoine Gauthier who helped me to discover this technique.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.vjeux.com/2009/makefile/makefile-automatic-dependencies-with-makedepend.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
