<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<meta name="generator" content="pdf2htmlEX">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<link rel="stylesheet" href="https://static.pudn.com/base/css/base.min.css">
<link rel="stylesheet" href="https://static.pudn.com/base/css/fancy.min.css">
<link rel="stylesheet" href="https://static.pudn.com/prod/directory_preview_static/6291a249bf399b7f3530a637/raw.css">
<script src="https://static.pudn.com/base/js/compatibility.min.js"></script>
<script src="https://static.pudn.com/base/js/pdf2htmlEX.min.js"></script>
<script>
try{
pdf2htmlEX.defaultViewer = new pdf2htmlEX.Viewer({});
}catch(e){}
</script>
<title></title>
</head>
<body>
<div id="sidebar" style="display: none">
<div id="outline">
</div>
</div>
<div id="pf1" class="pf w0 h0" data-page-no="1"><div class="pc pc1 w0 h0"><img class="bi x0 y0 w1 h1" alt="" src="https://static.pudn.com/prod/directory_preview_static/6291a249bf399b7f3530a637/bg1.jpg"><div class="c x0 y1 w0 h2"><div class="t m0 x1 h3 y2 ff1 fs0 fc0 sc0 ls0 ws0">BIT<span class="_ _0"></span>-0-<span class="ff2">程序环境和预处理<span class="_ _1"> </span></span><span class="fc1"> </span></div><div class="t m0 x1 h4 y3 ff2 fs1 fc0 sc0 ls0 ws0">版权<span class="ff1">© <span class="_ _2"></span><span class="ff2">比特科技<span class="_ _3"> </span><span class="ff1 fc1"> </span></span></span></div><div class="t m0 x1 h5 y4 ff3 fs1 fc0 sc0 ls0 ws0">本章重点:</div><div class="t m0 x2 h5 y5 ff3 fs1 fc0 sc0 ls0 ws0">程序的翻译环境</div><div class="t m0 x2 h5 y6 ff3 fs1 fc0 sc0 ls0 ws0">程序的执行环境</div><div class="t m0 x2 h5 y7 ff3 fs1 fc0 sc0 ls0 ws0">详解:<span class="ff4">C</span>语言程序的编译<span class="ff4">+</span>链接</div><div class="t m0 x2 h5 y8 ff3 fs1 fc0 sc0 ls0 ws0">预定义符号介绍</div><div class="t m0 x2 h5 y9 ff3 fs1 fc0 sc0 ls0 ws0">预处理指令<span class="_ _4"> </span><span class="ff5 fs2">#define</span></div><div class="t m0 x2 h5 ya ff3 fs1 fc0 sc0 ls0 ws0">宏和函数的对比</div><div class="t m0 x2 h5 yb ff3 fs1 fc0 sc0 ls0 ws0">预处理操作符<span class="ff4">#</span>和<span class="ff4">##</span>的介绍</div><div class="t m0 x2 h5 yc ff3 fs1 fc0 sc0 ls0 ws0">命令定义</div><div class="t m0 x2 h5 yd ff3 fs1 fc0 sc0 ls0 ws0">预处理指令<span class="_ _4"> </span><span class="ff5 fs2">#include</span></div><div class="t m0 x2 h5 ye ff3 fs1 fc0 sc0 ls0 ws0">预处理指令<span class="_ _4"> </span><span class="ff5 fs2">#undef</span></div><div class="t m0 x2 h5 yf ff3 fs1 fc0 sc0 ls0 ws0">条件编译</div><div class="t m0 x1 h6 y10 ff2 fs3 fc0 sc0 ls0 ws0">正文开始<span class="_ _5"> </span><span class="ff1 fc1"> </span></div><div class="t m0 x1 h6 y11 ff2 fs3 fc0 sc0 ls0 ws0">程序的翻译环境和执行环境<span class="_ _6"> </span><span class="ff1 fc1"> </span></div><div class="t m0 x1 h5 y12 ff3 fs1 fc0 sc0 ls0 ws0">在<span class="ff4">ANSI C</span>的任何一种实现中,存在两个不同的环境。</div><div class="t m0 x3 h5 y13 ff3 fs1 fc2 sc0 ls0 ws0">第<span class="ff4">1</span>种是翻译环境,在这个环境中源代码被转换为可执行的机器指令。<span class="ff4"> </span>第<span class="ff4">2</span>种是执行环境,它用于实际执行代</div><div class="t m0 x3 h5 y14 ff3 fs1 fc2 sc0 ls0 ws0">码。</div><div class="t m0 x1 h6 y15 ff2 fs3 fc0 sc0 ls0 ws0">详解编译<span class="ff1">+</span>链接<span class="_ _7"> </span><span class="ff1 fc1"> </span></div><div class="t m0 x1 h4 y16 ff2 fs1 fc0 sc0 ls0 ws0">翻译环境<span class="_ _8"> </span><span class="ff1 fc1"> </span></div></div><div class="c x0 y17 w2 h7"><div class="t m1 x4 h8 y18 ff6 fs4 fc3 sc0 ls0 ws0"><span class="fc4 sc0">比特科技</span></div></div><a class="l" rel='nofollow' onclick='return false;'><div class="d m2"></div></a><a class="l" rel='nofollow' onclick='return false;'><div class="d m2"></div></a><a class="l" rel='nofollow' onclick='return false;'><div class="d m2"></div></a><a class="l" rel='nofollow' onclick='return false;'><div class="d m2"></div></a><a class="l" rel='nofollow' onclick='return false;'><div class="d m2"></div></a><a class="l" rel='nofollow' onclick='return false;'><div class="d m2"></div></a></div><div class="pi" data-data='{"ctm":[1.613445,0.000000,0.000000,1.613445,0.000000,0.000000]}'></div></div>
</body>
</html>
<div id="pf2" class="pf w0 h0" data-page-no="2"><div class="pc pc2 w0 h0"><img class="bi x0 y0 w1 h1" alt="" src="https://static.pudn.com/prod/directory_preview_static/6291a249bf399b7f3530a637/bg2.jpg"><div class="c x0 y1 w0 h2"><div class="t m0 x5 h5 y19 ff3 fs1 fc2 sc0 ls0 ws0">组成一个程序的每个源文件通过编译过程分别转换成目标代码(<span class="ff4">object code</span>)。</div><div class="t m0 x5 h5 y1a ff3 fs1 fc2 sc0 ls0 ws0">每个目标文件由链接器(<span class="ff4">linker</span>)捆绑在一起,形成一个单一而完整的可执行程序。</div><div class="t m0 x5 h5 y1b ff3 fs1 fc2 sc0 ls0 ws0">链接器同时也会引入标准<span class="ff4">C</span>函数库中任何被该程序所用到的函数,而且它可以搜索程序员个人的程序库,</div><div class="t m0 x5 h5 y1c ff3 fs1 fc2 sc0 ls0 ws0">将其需要的函数也链接到程序中。</div><div class="t m0 x1 h4 y1d ff2 fs1 fc0 sc0 ls0 ws0">编译本身也分为几个阶段:<span class="_ _9"> </span><span class="ff1 fc1"> </span></div><div class="t m0 x1 h5 y1e ff3 fs1 fc0 sc0 ls0 ws0">看代码:<span class="ff4"> sum.c</span></div><div class="t m0 x1 h9 y1f ff4 fs5 fc0 sc0 ls0 ws0">test.c</div></div><div class="c x6 y20 w3 ha"><div class="t m0 x7 hb y21 ff5 fs2 fc5 sc0 ls0 ws0">int<span class="fc0"> <span class="fc6">g_val</span> <span class="fc7">=</span> <span class="fc8">2016</span>;</span></div><div class="t m0 x7 hb y22 ff5 fs2 fc5 sc0 ls0 ws0">void<span class="fc0"> <span class="fc9">print</span>(<span class="fca">const</span> </span>char<span class="fc0"> </span>*<span class="fc6">str<span class="fc0">)</span></span></div><div class="t m0 x7 hb y23 ff5 fs2 fc0 sc0 ls0 ws0">{</div><div class="t m0 x7 hb y24 ff5 fs2 fc0 sc0 ls0 ws0"> <span class="fc6">printf</span>(<span class="fcb">"%s\n"</span>, <span class="fc6">str</span>);</div><div class="t m0 x7 hb y25 ff5 fs2 fc0 sc0 ls0 ws0">}</div></div><div class="c x6 y26 w3 hc"><div class="t m0 x7 hb y27 ff5 fs2 fcc sc0 ls0 ws0">#include <stdio.h></div><div class="t m0 x7 hb y28 ff5 fs2 fc5 sc0 ls0 ws0">int<span class="fc0"> <span class="fc9">main</span>()</span></div><div class="t m0 x7 hb y29 ff5 fs2 fc0 sc0 ls0 ws0">{</div><div class="t m0 x7 hb y2a ff5 fs2 fc0 sc0 ls0 ws0"> <span class="fca">extern</span> <span class="fc5">void</span> <span class="fc6">print</span>(<span class="fc5">char</span> <span class="fc5">*<span class="fc6">str</span></span>);</div><div class="t m0 x7 hb y2b ff5 fs2 fc0 sc0 ls0 ws0"> <span class="fca">extern</span> <span class="fc5">int</span> <span class="fc6">g_val</span>;</div><div class="t m0 x7 hb y2c ff5 fs2 fc0 sc0 ls0 ws0"> <span class="fc6">printf</span>(<span class="fcb">"%d\n"</span>, <span class="fc6">g_val</span>);</div><div class="t m0 x7 hb y2d ff5 fs2 fc0 sc0 ls0 ws0"> <span class="fc6">print</span>(<span class="fcb">"hello bit.\n"</span>);</div><div class="t m0 x7 hb y2e ff5 fs2 fc0 sc0 ls0 ws0"> <span class="fca">return</span> <span class="fc8">0</span>;</div><div class="t m0 x7 hb y2f ff5 fs2 fc0 sc0 ls0 ws0">}</div></div><div class="c x0 y17 w2 h7"><div class="t m1 x4 h8 y18 ff6 fs4 fc3 sc0 ls0 ws0"><span class="fc4 sc0">比特科技</span></div></div><a class="l" rel='nofollow' onclick='return false;'><div class="d m2"></div></a></div><div class="pi" data-data='{"ctm":[1.613445,0.000000,0.000000,1.613445,0.000000,0.000000]}'></div></div>
<div id="pf3" class="pf w0 h0" data-page-no="3"><div class="pc pc3 w0 h0"><img class="bi x0 y0 w1 h1" alt="" src="https://static.pudn.com/prod/directory_preview_static/6291a249bf399b7f3530a637/bg3.jpg"><div class="c x0 y1 w0 h2"><div class="t m0 x1 h5 y30 ff3 fs1 fc0 sc0 ls0 ws0">如何查看编译期间的每一步发生了什么呢?</div><div class="t m0 x1 hd y31 ff4 fs1 fc0 sc0 ls0 ws0">test.c</div><div class="t m0 x8 he y32 ff4 fs5 fc0 sc0 ls0 ws0">1<span class="_ _a"></span>. <span class="ff3">预处理</span> <span class="ff3">选项<span class="_ _4"> </span><span class="ff5 fs6">gcc <span class="ff4">-</span>E test.c <span class="ff4">-</span>o test.i<span class="_ _4"> </span></span></span> <span class="ff3">预处理完成之后就停下来,预处理之后产生的结果都放在</span>test.i<span class="ff3">文件</span></div><div class="t m0 x2 he y33 ff3 fs5 fc0 sc0 ls0 ws0">中。</div><div class="t m0 x8 he y34 ff4 fs5 fc0 sc0 ls0 ws0">2<span class="_ _a"></span>. <span class="ff3">编译</span> <span class="ff3">选项</span> </div><div class="t m0 x9 he y35 ff5 fs6 fc0 sc0 ls0 ws0">gcc <span class="ff4">-</span>S test.c<span class="_ _4"> </span><span class="ff4 fs5"> <span class="ff3">编译完成之后就停下来,结果保存在</span>test.s<span class="ff3">中。</span></span></div><div class="t m0 x8 he y36 ff4 fs5 fc0 sc0 ls0 ws0">3<span class="_ _a"></span>. <span class="ff3">汇编</span> </div><div class="t m0 xa he y37 ff5 fs6 fc0 sc0 ls0 ws0">gcc <span class="ff4">-</span>c test.c<span class="_ _4"> </span><span class="ff4 fs5"> <span class="ff3">汇编完成之后就停下来,结果保存在</span>test.o<span class="ff3">中。</span></span></div><div class="t m0 x3 he y38 ff4 fs5 fc2 sc0 ls0 ws0">VIM<span class="ff3">学习资料</span></div><div class="t m0 x3 he y39 ff3 fs5 fc2 sc0 ls0 ws0">简明<span class="ff4">VIM</span>练级攻略:</div><div class="t m0 x3 h9 y3a ff4 fs5 fc1 sc0 ls0 ws0">https://coolshell.cn/articles/5426.html</div><div class="t m0 x3 he y3b ff3 fs5 fc2 sc0 ls0 ws0">给程序员的<span class="ff4">VIM</span>速查卡</div><div class="t m0 x3 h9 y3c ff4 fs5 fc1 sc0 ls0 ws0">https://coolshell.cn/articles/5479.html</div><div class="t m0 x1 hf y3d ff2 fs5 fc0 sc0 ls0 ws0">运行环境<span class="_ _8"> </span><span class="ff1 fc1"> </span></div><div class="t m0 x1 he y3e ff3 fs5 fc0 sc0 ls0 ws0">程序执行的过程:</div></div><div class="c x6 y3f w3 h10"><div class="t m0 x7 hb y40 ff5 fs2 fcc sc0 ls0 ws0">#include <stdio.h></div><div class="t m0 x7 hb y41 ff5 fs2 fc5 sc0 ls0 ws0">int<span class="fc0"> <span class="fc9">main</span>()</span></div><div class="t m0 x7 hb y42 ff5 fs2 fc0 sc0 ls0 ws0">{</div><div class="t m0 x7 hb y43 ff5 fs2 fc0 sc0 ls0 ws0"> <span class="fc5">int</span> <span class="fc6">i</span> <span class="fc7">=</span> <span class="fc8">0</span>;</div><div class="t m0 x7 hb y44 ff5 fs2 fc0 sc0 ls0 ws0"> <span class="fca">for</span>(<span class="fc6">i<span class="fc7">=<span class="fc8">0</span></span></span>; <span class="fc6">i<span class="fc7"><<span class="fc8">10</span></span></span>; <span class="fc6">i<span class="fc7">++</span></span>)</div><div class="t m0 x7 hb y45 ff5 fs2 fc0 sc0 ls0 ws0"> {</div><div class="t m0 x7 hb y46 ff5 fs2 fc0 sc0 ls0 ws0"> <span class="fc6">printf</span>(<span class="fcb">"%d "</span>, <span class="fc6">i</span>);</div><div class="t m0 x7 hb y47 ff5 fs2 fc0 sc0 ls0 ws0"> }</div><div class="t m0 x7 hb y48 ff5 fs2 fc0 sc0 ls0 ws0"> <span class="fca">return</span> <span class="fc8">0</span>;</div><div class="t m0 x7 hb y49 ff5 fs2 fc0 sc0 ls0 ws0">}</div></div><div class="c x0 y17 w2 h7"><div class="t m1 x4 h8 y18 ff6 fs4 fc3 sc0 ls0 ws0"><span class="fc4 sc0">比特科技</span></div></div><a class="l" rel='nofollow' onclick='return false;'><div class="d m2"></div></a><a class="l" rel='nofollow' onclick='return false;'><div class="d m2"></div></a><a class="l" rel='nofollow' onclick='return false;'><div class="d m2"></div></a></div><div class="pi" data-data='{"ctm":[1.613445,0.000000,0.000000,1.613445,0.000000,0.000000]}'></div></div>