1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<h1 class="section">Including Other Makefiles</h1> <p>The <code>include</code> directive tells <code>make</code> to suspend reading the current makefile and read one or more other makefiles before continuing. The directive is a line in the makefile that looks like this: </p> <div class="example"> <pre class="example">include <var>filenames</var>…
</pre>
</div> <p><var>filenames</var> can contain shell file name patterns. If <var>filenames</var> is empty, nothing is included and no error is printed. </p> <p>Extra spaces are allowed and ignored at the beginning of the line, but the first character must not be a tab (or the value of <code>.RECIPEPREFIX</code>)—if the line begins with a tab, it will be considered a recipe line. Whitespace is required between <code>include</code> and the file names, and between file names; extra whitespace is ignored there and at the end of the directive. A comment starting with ‘<samp>#</samp>’ is allowed at the end of the line. If the file names contain any variable or function references, they are expanded. See <a href="using-variables">How to Use Variables</a>. </p> <p>For example, if you have three <samp>.mk</samp> files, <samp>a.mk</samp>, <samp>b.mk</samp>, and <samp>c.mk</samp>, and <code>$(bar)</code> expands to <code>bish bash</code>, then the following expression </p> <div class="example"> <pre class="example">include foo *.mk $(bar)
</pre>
</div> <p>is equivalent to </p> <div class="example"> <pre class="example">include foo a.mk b.mk c.mk bish bash
</pre>
</div> <p>When <code>make</code> processes an <code>include</code> directive, it suspends reading of the containing makefile and reads from each listed file in turn. When that is finished, <code>make</code> resumes reading the makefile in which the directive appears. </p> <p>One occasion for using <code>include</code> directives is when several programs, handled by individual makefiles in various directories, need to use a common set of variable definitions (see <a href="setting">Setting Variables</a>) or pattern rules (see <a href="pattern-rules">Defining and Redefining Pattern Rules</a>). </p> <p>Another such occasion is when you want to generate prerequisites from source files automatically; the prerequisites can be put in a file that is included by the main makefile. This practice is generally cleaner than that of somehow appending the prerequisites to the end of the main makefile as has been traditionally done with other versions of <code>make</code>. See <a href="automatic-prerequisites">Automatic Prerequisites</a>. </p> <p>If the specified name does not start with a slash, and the file is not found in the current directory, several other directories are searched. First, any directories you have specified with the ‘<samp>-I</samp>’ or ‘<samp>--include-dir</samp>’ options are searched (see <a href="options-summary">Summary of Options</a>). Then the following directories (if they exist) are searched, in this order: <samp><var>prefix</var>/include</samp> (normally <samp>/usr/local/include</samp> <a id="DOCF1" href="#FOOT1"><sup>1</sup></a>) <samp>/usr/gnu/include</samp>, <samp>/usr/local/include</samp>, <samp>/usr/include</samp>. </p> <p>The <code>.INCLUDE_DIRS</code> variable will contain the current list of directories that make will search for included files. See <a href="special-variables">Other Special Variables</a>. </p> <p>You can avoid searching in these default directories by adding the command line option <code>-I</code> with the special value <code>-</code> (e.g., <code>-I-</code>) to the command line. This will cause <code>make</code> to forget any already-set include directories, including the default directories. </p> <p>If an included makefile cannot be found in any of these directories it is not an immediately fatal error; processing of the makefile containing the <code>include</code> continues. Once it has finished reading makefiles, <code>make</code> will try to remake any that are out of date or don’t exist. See <a href="remaking-makefiles">How Makefiles Are Remade</a>. Only after it has failed to find a rule to remake the makefile, or it found a rule but the recipe failed, will <code>make</code> diagnose the missing makefile as a fatal error. </p> <p>If you want <code>make</code> to simply ignore a makefile which does not exist or cannot be remade, with no error message, use the <code><span class="nolinebreak">-include</span></code> directive instead of <code>include</code>, like this: </p> <div class="example"> <pre class="example">-include <var>filenames</var>…
</pre>
</div> <p>This acts like <code>include</code> in every way except that there is no error (not even a warning) if any of the <var>filenames</var> (or any prerequisites of any of the <var>filenames</var>) do not exist or cannot be remade. </p> <p>For compatibility with some other <code>make</code> implementations, <code>sinclude</code> is another name for <code><span class="nolinebreak">-include</span></code>. </p><div class="_attribution">
<p class="_attribution-p">
Copyright © 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022 Free Software Foundation, Inc. <br>Licensed under the GNU Free Documentation License.<br>
<a href="https://www.gnu.org/software/make/manual/html_node/Include.html" class="_attribution-link">https://www.gnu.org/software/make/manual/html_node/Include.html</a>
</p>
</div>
|