⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 using_make.html

📁 ADI 公司blackfin系列的用户使用文挡。
💻 HTML
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><head>  <title></title>  <link rel="stylesheet" media="screen" type="text/css" href="./style.css" />  <link rel="stylesheet" media="screen" type="text/css" href="./design.css" />  <link rel="stylesheet" media="print" type="text/css" href="./print.css" />  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body><a href=start.html>start</a></br><div class="toc"><div class="tocheader toctoggle" id="toc__header">Table of Contents</div><div id="toc__inside"><ul class="toc"><li class="clear"><ul class="toc"><li class="level2"><div class="li"><span class="li"><a href="#using_make" class="toc">Using Make</a></span></div><ul class="toc"><li class="level3"><div class="li"><span class="li"><a href="#introduction" class="toc">Introduction</a></span></div></li><li class="level3"><div class="li"><span class="li"><a href="#creating_a_new_makefile" class="toc">Creating a New Makefile</a></span></div></li><li class="level3"><div class="li"><span class="li"><a href="#a_more_terse_makefile" class="toc">A More Terse Makefile</a></span></div></li><li class="level3"><div class="li"><span class="li"><a href="#activities_exercises" class="toc">Activities / Exercises</a></span></div></li></ul></li></ul></li></ul></div></div><h2><a name="using_make" id="using_make">Using Make</a></h2><div class="level2"></div><!-- SECTION [1-22] --><h3><a name="introduction" id="introduction">Introduction</a></h3><div class="level3"><p>The make command is actually a powerful utility. It can be used to generate a series of commands to be executed by the shell. It is intended for use in maintaining files and, most commonly, for generating the final executable for a complex collection of programs. The GNU Make documentation is cited in the References section, but many Unix programming books will also have a chapter or section on make. There is an excellent O&rsquo;Reilly book on the System V Release 4 version of make, but here we stay with GNU make. Since make works with a potentially large set of interdependent files, we need to specify those interdependencies along with what we want done. This information is provided in a makefile. If make is invoked without specifying a makefile it works through the default names in order until it finds one. These names are </p><ul><li class="level1"><div class="li"><code>GNUmakefile</code></div></li><li class="level1"><div class="li"><code>makefile</code></div></li><li class="level1"><div class="li"><code>Makefile</code></div></li></ul><p>However, you can specify your own choice e.g.</p><pre class="code">make -f my_makefile</pre><p> We cannot do any more here than hint at the power of make. Nevertheless, we&rsquo;ll find it useful to work through a simple example. We assume that the files reside in the same directory and that is the directory from which we will invoke </p><ul><li class="level1"><div class="li">main.c</div></li><li class="level1"><div class="li">parser.c</div></li><li class="level1"><div class="li">lexer.c</div></li><li class="level1"><div class="li">makefile</div></li></ul></div><!-- SECTION [23-1295] --><h3><a name="creating_a_new_makefile" id="creating_a_new_makefile">Creating a New Makefile</a></h3><div class="level3"><p>Consider a project with the three C files, main.c, parser.c, and lexer.c. These are to be combined into a single executable. So that you can work through this in the exercises, we&rsquo;ll pick these to be essentially stubs of some final large mythical programs.</p><p> <code>main.c</code> </p><pre class="code c"><span class="co2">#include proj.h</span><span class="kw2">extern</span> <span class="kw4">int</span> parser<span class="br0">&#40;</span><span class="br0">&#41;</span>;<span class="kw4">int</span> main<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>    <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span class="kw3">printf</span></a><span class="br0">&#40;</span><span class="st0">"main<span class="es0">\"</span>);    parser():} </span></pre><p><code>parser.c</code> </p><pre class="code c"><span class="co2">#include &quot;proj.h&quot;</span><span class="co2">#include &quot;parser.h&quot;</span><span class="kw2">extern</span> <span class="kw4">int</span> lexer<span class="br0">&#40;</span><span class="br0">&#41;</span>;<span class="kw4">int</span> parser<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>    <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span class="kw3">printf</span></a><span class="br0">&#40;</span><span class="st0">"parser<span class="es0">\"</span>);    lexer():} </span></pre><p> <code>lexer.c</code> </p><pre class="code c"><span class="co2">#include &quot;proj.h&quot;</span><span class="kw2">extern</span> <span class="kw4">int</span> lexer<span class="br0">&#40;</span><span class="br0">&#41;</span>;<span class="kw4">int</span> lexer<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>    <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span class="kw3">printf</span></a><span class="br0">&#40;</span><span class="st0">"lexer<span class="es0">\"</span>);} </span></pre><p> To combine these into a single executable, app, we would do the following sequence of steps:</p><p><strong>gcc -c lexer.c</strong><strong>gcc -c parser.c</strong><strong>gcc -c main.c</strong><strong>gcc -o app main.o parser.o lexer.o</strong></p><p>As the project moved along we would work on one part or another and periodically recompile the appropriate parts, keeping careful track. The make utility automates this and the up front work of writing the makefile is not a great burden. In fact, it helps organize your approach to the problem. For our example, we&rsquo;ll start with the makefile below.</p><p><code>Makefile:</code>  </p><pre class="code c"><span class="co2">#define CC</span>CC = gcc CFLAGS = -O2&nbsp;<span class="co2">#declare a target (app) and list its dependants</span><span class="co2"># specify the rule to build the target </span><span class="co2"># NOTE start each command line with a &lt;TAB&gt; </span>&nbsp;app: main.<span class="me1">o</span> lexer.<span class="me1">o</span> parser.<span class="me1">o</span>      $<span class="br0">&#40;</span>CC<span class="br0">&#41;</span> $<span class="br0">&#40;</span>CFLAGS<span class="br0">&#41;</span> -o app main.<span class="me1">o</span> parser.<span class="me1">o</span> lexer.<span class="me1">o</span>&nbsp;main.<span class="me1">o</span>: main.<span class="me1">c</span> proj.<span class="me1">h</span>      $<span class="br0">&#40;</span>CC<span class="br0">&#41;</span> $<span class="br0">&#40;</span>CFLAGS<span class="br0">&#41;</span> -c main.<span class="me1">c</span>&nbsp;parser.<span class="me1">o</span>: parser.<span class="me1">c</span> proj.<span class="me1">h</span> parser.<span class="me1">h</span>      $<span class="br0">&#40;</span>CC<span class="br0">&#41;</span> $<span class="br0">&#40;</span>CFLAGS<span class="br0">&#41;</span> -c parser.<span class="me1">c</span>&nbsp;lexer.<span class="me1">o</span>: lexer.<span class="me1">c</span> proj.<span class="me1">h</span> lexer.<span class="me1">c</span>      $<span class="br0">&#40;</span>CC<span class="br0">&#41;</span> $<span class="br0">&#40;</span>CFLAGS<span class="br0">&#41;</span> -c lexer.<span class="me1">c</span></pre><p><strong>must</strong></p><p>Comparing this to the manual steps indicates the flavor of what is happening, but let&rsquo;s go through it and then try some variations. first line <br/></p><pre class="code">CC = gccCFLAGS = -O2</pre><p>Here <code>CC</code> and <code>CFLAGS</code> are a user defined variable, which has been given a value of &lsquo;gcc&rsquo;, clearly intended to identify the compiler and &lsquo;-O2&rsquo; to set any flags for the compiler. When it is later used, it will be enclosed to look like this <br/></p><pre class="code">$(CC)</pre><p>If we wanted the capability to compile for subsequent debugging, but wanted to be able to turn that feature off, we could write</p><pre class="code">CC = gcc# CC = gcc</pre><p> The <code>&lsquo;#</code>&rsquo; designates a comment, so to turn on the debugging support, we would just remove the <code>&lsquo;#</code>&rsquo;  remainder of the makefile The rest of the file consists of rules. Make will parse each rule and find out:</p><ul><li class="level1"><div class="li"> what it should build - the target</div></li><li class="level1"><div class="li"> what the target depends on - the dependencies</div></li><li class="level1"><div class="li"> how to build the target - the command(s)</div></li></ul><p> The format is : <code>target: dependency list</code> <br/> <code>list of commands </code></p><p>Important note: For historical reasons, too deeply entrenched to change, a command must begin with a tab character. Some editors can be configured to substitute the appropriate number of spaces for the tab character. This will not work for make.  As an example, consider this rule taken from our makefile:</p><pre class="code">app: main.o lexer.o parser.o$(CC) -o app main.o parser.o lexer.o</pre><p> Here, app is the target. It depends on the files main.o, parser.o, and lexer.o (the dependencies). The second line tells make how to build app. Now make is smart enough to parse the whole file and figure out that the dependencies form a hierarchy and then knows which commands to do first. Further, make will not do unnecessary work. It will not bother to rebuild files whose constituent components have not changed.</p></div><!-- SECTION [1296-5160] --><h3><a name="a_more_terse_makefile" id="a_more_terse_makefile">A More Terse Makefile</a></h3><div class="level3"><p>Here is a makefile that works just like the earlier one. App:</p><pre class="code">Makefile:main.o lexer.o parser. main.o: main.c proj.h parser.o: \parser.c proj.h parser.h lexer.o: lexer.c proj.h lexer.c </pre><p> We&rsquo;ve taken out all the commands. This will work because make can deduce the needed commands. </p><p>In this section we&rsquo;ve introduced only the tip of the iceberg. We urge you to not only start using make (if you don&rsquo;t already) and get the GNU make documentation to learn its many other capabilities. As is typical with other UNIX derived tools, if you wish it had some particular feature, chances are very good that it already does.</p></div><!-- SECTION [5161-5828] --><h3><a name="activities_exercises" id="activities_exercises">Activities / Exercises</a></h3><div class="level3"><p>With an editor create the files </p><ul><li class="level1"><div class="li"><code>main.c</code></div></li><li class="level1"><div class="li"><code>parser.c</code></div></li><li class="level1"><div class="li"><code>lexer.c</code></div></li><li class="level1"><div class="li"><code>makefile</code></div></li></ul><p>where all reside in the same directory. You&rsquo;ll also need the corresponding header files, which can be empty. Create this with <code>touch</code> i.e.</p><pre class="code">touch proj.htouch parser.htouch lexer.h</pre><p> Carry out these activities: </p><ul><li class="level1"><div class="li">Now invoke <code>make</code> and note what is reported to the screen and what new files are created.</div></li><li class="level1"><div class="li">Try removing some *.o files, one at a time, and do another make. </div></li></ul><p>Does what make reports to the screen make sense? </p><ul><li class="level1"><div class="li">Try removing one of the header files.</div></li></ul><p>What happens? </p></div><!-- SECTION [5829-] --></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -