﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-淋雨的博客-随笔分类-Linux</title><link>http://www.cppblog.com/inkcpp/category/18625.html</link><description /><language>zh-cn</language><lastBuildDate>Thu, 10 May 2012 23:40:25 GMT</lastBuildDate><pubDate>Thu, 10 May 2012 23:40:25 GMT</pubDate><ttl>60</ttl><item><title>pptp搭建的vpn代理上网很慢 </title><link>http://www.cppblog.com/inkcpp/archive/2012/05/02/173463.html</link><dc:creator>淋雨的感觉</dc:creator><author>淋雨的感觉</author><pubDate>Wed, 02 May 2012 05:45:00 GMT</pubDate><guid>http://www.cppblog.com/inkcpp/archive/2012/05/02/173463.html</guid><wfw:comment>http://www.cppblog.com/inkcpp/comments/173463.html</wfw:comment><comments>http://www.cppblog.com/inkcpp/archive/2012/05/02/173463.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/inkcpp/comments/commentRss/173463.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/inkcpp/services/trackbacks/173463.html</trackback:ping><description><![CDATA[<span style="color: #836c74; font-family: Arial; font-size: 12px; letter-spacing: 1px; line-height: 20px; background-color: #ffffff; ">转贴自：</span><a href="http://blog.chinaunix.net/u/16464/showart.php?id=1923564" style="color: #836c74; font-size: 12px; font-family: Verdana, Arial, Helvetica, sans-serif; text-decoration: none; line-height: 20px; letter-spacing: 1px; background-color: #ffffff; ">http://blog.chinaunix.net/u/16464/showart.php?id=1923564<br /></a><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; ">题:用pptp搭建了linux平台的vpn服务器，拨入后访问内网ftp，下载文件极慢；用其作网关上网，除了baidu外，大部分网站访问速度极慢，几乎无法访问。</div><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; ">解决: 在pptp所在的linux服务的iptables的*filter表中加入</div><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; ">-I FORWARD -p tcp --syn -i ppp+ -j TCPMSS --set-mss 1356<br style="line-height: normal; " />或者在命令提示符运行<br style="line-height: normal; " />/sbin/iptables -I FORWARD -p tcp --syn -i ppp+ -j TCPMSS --set-mss 1356</div><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; ">原因分析&nbsp;<br style="line-height: normal; " />=====在断开vpn链接的情况下：<br style="line-height: normal; " />在windowsXP下用ping -f -l XXXXXX 192.168.0.1一步一步测试（XXXXXXX为MTU大小，可以从1500开始，逐渐减小，知道可以ping通）<br style="line-height: normal; " />我们可以得到可以ping通的MTU最大为1426；<br style="line-height: normal; " />=====在连接vpn的前提下<br style="line-height: normal; " />在windowsXP下用ping -f -l XXXXXX 192.168.0.1一步一步测试（XXXXXXX为MTU大小，可以从1500开始，逐渐减小，知道可以ping通）<br style="line-height: normal; " />我们可以得到可以ping通的MTU最大为1372；</div><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; ">超过这个数则不能通，<br style="line-height: normal; " />====拨通vpn，在服务器上用netstat &#8211;i查看接口，得到<br style="line-height: normal; " />Iface&nbsp;&nbsp; MTU Met&nbsp;&nbsp;&nbsp; RX-OK RX-ERR RX-DRP RX-OVR&nbsp;&nbsp;&nbsp; TX-OK TX-ERR TX-DRP TX-OVR Flg<br style="line-height: normal; " />eth0&nbsp;&nbsp; 1500&nbsp;&nbsp; 0 102528561&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0 194391413&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0 BRU<br style="line-height: normal; " />eth1&nbsp;&nbsp; 1500&nbsp;&nbsp; 0 519820535&nbsp;&nbsp;&nbsp; 954 11553&nbsp;&nbsp;&nbsp; 924 208798037&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0 BRU<br style="line-height: normal; " />lo&nbsp;&nbsp;&nbsp; 16436&nbsp;&nbsp; 0&nbsp;&nbsp; 151062&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp; 151062&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0 LRU<br style="line-height: normal; " />ppp0&nbsp;&nbsp; 1396&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 19&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 8&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0 OPRU</div><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; ">可知ppp的最大mtu为1396，当然，对应的mss应为（mtu-20字节的IP头部+20字节的TCP 头部=）1356</div><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; ">【小知识1】计算机网络中的MSS：<br style="line-height: normal; " />MSS: Maximum Segment Size 最大分段大小<br style="line-height: normal; " />MSS最大传输大小的缩写，是TCP协议里面的一个概念。<br style="line-height: normal; " />MSS就是TCP数据包每次能够传输的最大数据分段。为了达到最佳的传输效能，TCP协议在建立连接的时候通常要协商双方的MSS值，这个值TCP协议在实现的时候往往用MTU值代替（需要减去IP数据包包头的大小20Bytes和TCP数据段的包头20Bytes）所以往往MSS为1460。通讯双方会根据双方提供的MSS值得最小值确定为这次连接的最大MSS值。</div><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; "><br style="line-height: normal; " />【小知识2】mtu是网络传输最大报文包。&nbsp;<br style="line-height: normal; " />mss是网络传输数据最大值。&nbsp;<br style="line-height: normal; " />mss加包头数据就等于mtu.&nbsp;<br style="line-height: normal; " />简单说拿TCP包做例子。&nbsp;<br style="line-height: normal; " />报文传输1400字节的数据的话，那么mss就是1400，再加上20字节IP包头，20字节tcp包头，那么mtu就是1400+20+20.&nbsp;<br style="line-height: normal; " />当然传输的时候其他的协议还要加些包头在前面，总之mtu就是总的最后发出去的报文大小。mss就是你需要发出去的数据大小。</div><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; ">【小知识3】<a href="http://www.cnpaf.net/Class/TCPANDIP/200511/9898.html" style="color: #836c74; font-family: Verdana, Arial, Helvetica, sans-serif; text-decoration: none; ">http://www.cnpaf.net/Class/TCPANDIP/200511/9898.html</a></div><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; ">假设PC建立了到SERVER的HTTP连接，PC希望从SERVER下载一个大的网页。SERVER接收到PC的请求后开始发送大网页文件，其IP的DF位置1，不允许分片，IP报文长度为1500字节。到达VPN网关2的外网口（以太）后，VPN网关2发现其长度超过了1500个字节，于是将其丢弃，并给SERVER发回一个目的地址不可达的ICMP信息，同时指出&#8220;MTU of next hop: 1500&#8221;。PC接收到该消息后，又按照1500字节对外发送，又被丢弃，于是就形成了循环，无法通讯。<br style="line-height: normal; " />根据上述的分析，很容易得到如下解决方式，在VPN网关2的出接口设置MTU为1500－4－20＝1476，这样VPN网关2返回ICMP不可达消息时将给出&#8221;MTU of next hop: 1476&#8221;。SERVER将以1476作为自己的最大MTU对外发送，到达VPN网关1，封装GRE和外层IP头后就不会超过1500而顺利发到对端。</div><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; ">-I FORWARD -p tcp --syn -i ppp+ -j TCPMSS --set-mss 1356</div><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; ">因为mss是在TCP连接建立开始时，通过带有syn标志的IP数据包进行传输的，所以我们在iptables里面规定，在转发数据时，只要发现产生于ppt*的带有 syn标志数据包时，将其mss设定为1356字节，这样就与ppp0接口的路径MTU向匹配了，数据自然就可以畅通无阻啦。</div><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; ">（注，vpn拨入一个，则建立一个ppt*的虚拟设备，这个可以再linux上用ifcpnfig看到，第一个为ppt1，第二个为ppt2&#8230;&#8230;）</div><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; "><br style="line-height: normal; " />参考：</div><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; ">1、<a href="http://fanqiang.chinaunix.net/app/other/2005-09-13/3655.shtml" style="color: #836c74; font-family: Verdana, Arial, Helvetica, sans-serif; text-decoration: none; ">http://fanqiang.chinaunix.net/app/other/2005-09-13/3655.shtml</a></div><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; ">2、<a href="http://technet.microsoft.com/zh-cn/library/cc768084(en-us).aspx" style="color: #836c74; font-family: Verdana, Arial, Helvetica, sans-serif; text-decoration: none; ">http://technet.microsoft.com/zh-cn/library/cc768084(en-us).aspx</a></div><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; ">3、这是一个比较复杂的问题。首先，发现问题的过程是这样的：使用一台WinXP的电脑（简称主机A）连接公司的VPN成功后，访问内网的一个基于 B/S的CRM系统（简称主机B）时，发现首页可以显示（页面比较简单，包含的数据量较小），输入账号密码登陆后，发现只能显示页面顶部的一点点内容，而下面大部分内容无法显示。而换一台Win2000的电脑登陆，内容就可以完全显示出来。登陆到Linux VPN主机上，利用tcpdump对数据传输过程进行抓包分析，发现：每当B向A传输大于1396字节的数据时，VPN主机就会反馈B如下信息&nbsp;<br style="line-height: normal; " />注意：&nbsp;<br style="line-height: normal; " />10.87.0.200：VPN主机的内网网卡的IP地址&nbsp;<br style="line-height: normal; " />10.87.200.1：主机A的IP地址&nbsp;<br style="line-height: normal; " />10.87.200.6：主机B通过VPN获取的IP地址&nbsp;<br style="line-height: normal; " />21:54:21.953848 IP 10.87.0.200 &gt; 10.100.0.100: icmp 556: 10.100.0.203 unreachable -&nbsp;<br style="line-height: normal; " />need to frag (mtu 1396)&nbsp;<br style="line-height: normal; " />可以看到VPN主机向提供web服务的主机B返回了一个ICMP不可达的差错报文。其含义是VPN主机收到了一个需要分片才能通过的数据包，而这个数据包在其IP头部又设置了不能分片（DF）的标志。所以该数据包不能通过VPN主机。&nbsp;<br style="line-height: normal; " />根据TCP/IP协议，在建立TCP连接时，传输双方都要指明自己的mss（最大报文长度）大小，然后选取双方之中最小的那个mss，以避免在随后的数据传送过程中出现数据包分片传输的情况。通过抓包分析，主机B的mss为1460字节，主机A的mss为1357字节。两者取小所以双方协商的结果确定 mss为1357字节，也就是说以后进行TCP数据传输时，数据包的最大传输单元MTU不能超过1397（mss+20字节的IP头部+20字节的TCP 头部），同时在IP头部设置了不能分片（DF）的标志。&nbsp;<br style="line-height: normal; " />然后在VPN主机上执行netstat &#8211;i，观察各个网络接口的路径MTU值为多少。观察结果如下：&nbsp;<br style="line-height: normal; " />Iface MTU&nbsp;<br style="line-height: normal; " />eth0 1500 //外网网卡接口&nbsp;<br style="line-height: normal; " />eth1 1500 //内网网卡接口&nbsp;<br style="line-height: normal; " />lo 16436 //本机回环接口&nbsp;<br style="line-height: normal; " />ppp0 1396 //WinXP VPN接入通道接口&nbsp;<br style="line-height: normal; " />可以看到ppp0接口的路径MTU为1396字节，也就是说如果一个数据包想要通过这个接口的话，一定不能大于1396字节，如果大于这个值，会出现两种结果：<br style="line-height: normal; " />1、如果这个数据包的IP头部没有设置不能分片（DF）的标志，那么VPN主机就把这个数据包分片，使其数据包大小小于1396字节，然后允许其通过。&nbsp;<br style="line-height: normal; " />2、反之，如果这个数据包的IP头部设置不能分片（DF）的标志，那么VPN主机就会返回一个ICMP不可达的差错报文。同时丢弃这个数据包。&nbsp;<br style="line-height: normal; " />问题就出在这里，主机A和主机B协商的mss为1357字节，也就是说其TCP数据包的MTU为1397，而ppp0允许的路径MTU却为1396，主机 A的MTU居然大于ppp0的路径MTU！当主机B向主机A发送了一个1397字节的数据包时，自然不能通过ppp0接口了。回到发现问题的那个情况，首页之所以能够显示成功，是因为首页包含的数据较小，传输时只需要一个没有超过1396字节的IP数据包就可以了，所以能够显示出来，而登陆成功后的页面包含的数据较大，需要分为多个IP数据包进行传输。这里可以假设一下，开头的一个IP数据包因为没有超过1396字节因而通过，而随后的IP数据包因为其大小为1397字节，超过了路径MTU，所以不予通过。反映到页面，就是登陆页面下面的大部分内容无法显示了。&nbsp;<br style="line-height: normal; " />我们再来看看用安装了Win2000主机C连接VPN又是什么状况呢？通过抓包发现，主机C提出的mss为1360（可以推算出其MTU为1400），而执行netstat &#8211;i，发现此时的ppp0的MTU为1496，路径MTU大于主机C的MTU，这个结果是正常的。&nbsp;<br style="line-height: normal; " />大家一定会问，何主机B提出的MTU小于路径MTU，这个问题只能问问微软了，我查了一些英文资料，说这是WinXP本身系统的一个问题。&nbsp;<br style="line-height: normal; " />知道了问题的原理，那让我们来看看如何进行解决吧。解决方法很简单，就是借助iptalbes，设定主机B进行协商时提出的mss为1356。即在iptables里面加入一条规则：&nbsp;<br style="line-height: normal; " />代码&nbsp;<br style="line-height: normal; " />iptables -A FORWARD -p tcp --syn -s 10.87.200.0/31 -j TCPMSS --set-mss 1356&nbsp;&nbsp;</div><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; ">因为mss是在TCP连接建立开始时，通过带有syn标志的IP数据包进行传输的，所以我们在iptables里面规定，在转发数据时，只要发现带有 syn标志并且源地址为主机B的IP数据包时，将其mss设定为1356字节，这样就与ppp0接口的路径MTU向匹配了，数据自然就可以畅通无阻啦。</div><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; ">因为mss是在TCP连接建立开始时，通过带有syn标志的IP数据包进行传输的，所以我们在iptables里面规定，在转发数据时，只要发现带有 syn标志并且源地址为主机B的IP数据包时，将其mss设定为1356字节，这样就与ppp0接口的路径MTU向匹配了，数据自然就可以畅通无阻啦。</div><img src ="http://www.cppblog.com/inkcpp/aggbug/173463.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/inkcpp/" target="_blank">淋雨的感觉</a> 2012-05-02 13:45 <a href="http://www.cppblog.com/inkcpp/archive/2012/05/02/173463.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>vim配置文件</title><link>http://www.cppblog.com/inkcpp/archive/2012/03/05/167169.html</link><dc:creator>淋雨的感觉</dc:creator><author>淋雨的感觉</author><pubDate>Mon, 05 Mar 2012 06:48:00 GMT</pubDate><guid>http://www.cppblog.com/inkcpp/archive/2012/03/05/167169.html</guid><wfw:comment>http://www.cppblog.com/inkcpp/comments/167169.html</wfw:comment><comments>http://www.cppblog.com/inkcpp/archive/2012/03/05/167169.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/inkcpp/comments/commentRss/167169.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/inkcpp/services/trackbacks/167169.html</trackback:ping><description><![CDATA[<span style="font-family: Arial; background-color: #ffffff; ">"Description:适合自己使用的vimrc文件 for linux</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">"Last Change:2011-01-28</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">"Author:&nbsp;&nbsp;&nbsp;&nbsp; Moking</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">"Version:&nbsp;&nbsp;&nbsp; 2.0</span><br style="font-family: Arial; background-color: #ffffff; " /><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">"设置leader为</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">let mapleader=","</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">let g:mapleader=","</span><br style="font-family: Arial; background-color: #ffffff; " /><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">set nocompatible&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " 关闭 vi 兼容模式</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">syntax on&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " 自动语法高亮</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">filetype plugin indent on&nbsp;&nbsp; " 开启插件</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">set number&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " 显示行号</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">set nocursorline&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " 不突出显示当前行</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">set shiftwidth=4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " 设定 &lt;&lt; 和 &gt;&gt; 命令移动时的宽度为 4</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">set softtabstop=4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " 使得按退格键时可以一次删掉 4 个空格</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">set tabstop=4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " 设定 tab 长度为 4</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">set nobackup&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " 覆盖文件时不备份</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">set autochdir&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " 自动切换当前目录为当前文件所在的目录</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">set backupcopy=yes&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " 设置备份时的行为为覆盖</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">set autoread &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " set to auto read when a file changed from the outside</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">set ignorecase smartcase&nbsp;&nbsp;&nbsp; " 搜索时忽略大小写，但在有一个或以上大写字母时仍大小写敏感</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">set nowrapscan&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " 禁止在搜索到文件两端时重新搜索</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">set incsearch&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " 输入搜索内容时就显示搜索结果</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">set hlsearch&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " 搜索时高亮显示被找到的文本</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">set noerrorbells&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " 关闭错误信息响铃</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">set novisualbell&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " 关闭使用可视响铃代替呼叫</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">set t_vb=&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " 置空错误铃声的终端代码</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">" set showmatch&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " 插入括号时，短暂地跳转到匹配的对应括号</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">" set matchtime=2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " 短暂跳转到匹配括号的时间</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">"set nowrap&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " 不自动换行</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">set magic&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " 显示括号配对情况</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">set hidden&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " 允许在有未保存的修改时切换缓冲区，此时的修改由 vim 负责保存</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">set smartindent&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " 开启新行时使用智能自动缩进</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">set backspace=indent,eol,start</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " 不设定在插入状态无法用退格键和 Delete 键删除回车符</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">set cmdheight=1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " 设定命令行的行数为 1</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">set laststatus=2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " 显示状态栏 (默认值为 1, 无法显示状态栏)</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">set foldenable&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " 开始折叠</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">set foldmethod=syntax&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " 设置语法折叠</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">set foldcolumn=0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " 设置折叠区域的宽度</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">setlocal foldlevel=1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " 设置折叠层数为</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">" set foldclose=all&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " 设置为自动关闭折叠</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">" colorscheme colorzone&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " 设定配色方案</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">colorscheme darkblue&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " 设定配色方案</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">set statusline=\ %&lt;%F[%1*%M%*%n%R%H]%=\ %y\ %0(%{&amp;fileformat}\ [%{(&amp;fenc==\"\"?&amp;enc:&amp;fenc).(&amp;bomb?\",BOM\":\"\")}]\ %c:%l/%L%)\&nbsp;</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " 设置在状态行显示的信息</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">" 显示Tab符</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">set listchars=tab:\|\ ,trail:.,extends:&gt;,precedes:&lt;</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">set list</span><br style="font-family: Arial; background-color: #ffffff; " /><br style="font-family: Arial; background-color: #ffffff; " /><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">"设置代码折叠方式为 手工&nbsp; indent</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">set foldmethod=indent</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">"设置代码块折叠后显示的行数</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">set foldexpr=1</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">"编辑vim配置文件</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">set fileformats=unix,dos,mac</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">nmap &lt;Leader&gt;e :tabnew $HOME/.vimrc&lt;CR&gt;</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">let $VIMFILES = $HOME.'/.vim'</span><br style="font-family: Arial; background-color: #ffffff; " /><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">"设定doc文档目录</span><br style="font-family: Arial; background-color: #ffffff; " /><br style="font-family: Arial; background-color: #ffffff; " /><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">" {{{ plugin - NERD_commenter.vim 注释代码用的，</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">" &lt;leader&gt;ca 在可选的注释方式之间切换，比如C/C++ 的块注释/* */和行注释//</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">" &lt;leader&gt;cc 注释当前行</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">" &lt;leader&gt;cs 以&#8221;性感&#8221;的方式注释</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">" &lt;leader&gt;cA 在当前行尾添加注释符，并进入Insert模式</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">" &lt;leader&gt;cu 取消注释</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">" &lt;leader&gt;cm 添加块注释</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">" }}}</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">"自动添加作者信息</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">let g:vimrc_author='anchao1987'</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">let g:vimrc_email='anchao1987@gmail.com'</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">let g:vimrc_homepage='http://www.cppblog.com/anchao1987'</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">nmap &lt;F4&gt; :AuthorInfoDetect&lt;CR&gt;</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">"添加vim的STL支持</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">"需下载文件</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">"网址：http://vim.wikia.com/wiki/C++_code_completion</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">set tags+=~/.vim/tags/cpp</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">set tags+=~/.vim/tags/gl</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">set tags+=~/.vim/tags/sdl</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">set tags+=~/.vim/tags/qt4</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">"build tags of your own&nbsp; project with Ctrl-F12</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">map &lt;C-F12&gt; :!ctags -R --sort=yes --c++-kinds=+p --fields=+iaS --extra=+q .&lt;CR&gt;</span><br style="font-family: Arial; background-color: #ffffff; " /><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">"0mniCppComplete</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">let OmniCpp_NamespaceSearch = 1</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">let OmniCpp_GlobalScopeSearch = 1</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">let OmniCpp_ShowAccess = 1</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">let OmniCpp_ShowPrototypeInAbbr = 1 " show function parameters</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">let OmniCpp_MayCompleteDot = 1 " autocomplete after .</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">let OmniCpp_MayCompleteArrow = 1 " autocomplete after -&gt;</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">let OmniCpp_MayCompleteScope = 1 " autocomplete after ::</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">let OmniCpp_DefaultNamespaces = ["std", "_GLIBCXX_STD"]</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">" automatically open and close the popup menu / preview window</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">au CursorMovedI,InsertLeave * if pumvisible() == 0|silent! pclose|endif</span><br style="font-family: Arial; background-color: #ffffff; " /><span style="font-family: Arial; background-color: #ffffff; ">set completeopt=menuone,menu,longest,preview</span>&nbsp;<br /><br />来源：<a href="http://www.cppblog.com/anchao1987/articles/140855.html">http://www.cppblog.com/anchao1987/articles/140855.html</a><img src ="http://www.cppblog.com/inkcpp/aggbug/167169.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/inkcpp/" target="_blank">淋雨的感觉</a> 2012-03-05 14:48 <a href="http://www.cppblog.com/inkcpp/archive/2012/03/05/167169.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>linux中shell变量$#,$@,$0,$1,$2的含义解释</title><link>http://www.cppblog.com/inkcpp/archive/2012/02/20/166057.html</link><dc:creator>淋雨的感觉</dc:creator><author>淋雨的感觉</author><pubDate>Mon, 20 Feb 2012 05:13:00 GMT</pubDate><guid>http://www.cppblog.com/inkcpp/archive/2012/02/20/166057.html</guid><wfw:comment>http://www.cppblog.com/inkcpp/comments/166057.html</wfw:comment><comments>http://www.cppblog.com/inkcpp/archive/2012/02/20/166057.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/inkcpp/comments/commentRss/166057.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/inkcpp/services/trackbacks/166057.html</trackback:ping><description><![CDATA[<table style="table-layout: fixed; font-family: Arial; width: 900px; "><tbody><tr><td style="font-size: 12px; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; line-height: 18px; "><div id="blog_text" style="word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; overflow-x: hidden; overflow-y: hidden; position: relative !important; border-image: initial; "><span style="line-height: 19px; color: #333333; -webkit-border-horizontal-spacing: 1px; -webkit-border-vertical-spacing: 1px; "><p style="line-height: normal; ">变量说明:&nbsp;<br />$$&nbsp;<br />Shell本身的PID（ProcessID）&nbsp;<br />$!&nbsp;<br />Shell最后运行的后台Process的PID&nbsp;<br />$?&nbsp;<br />最后运行的命令的结束代码（返回值）&nbsp;<br />$-&nbsp;<br />使用Set命令设定的Flag一览&nbsp;<br />$*&nbsp;<br />所有参数列表。如"$*"用「"」括起来的情况、以"$1 $2 &#8230; $n"的形式输出所有参数。&nbsp;<br />$@&nbsp;<br />所有参数列表。如"$@"用「"」括起来的情况、以"$1" "$2" &#8230; "$n" 的形式输出所有参数。&nbsp;<br />$#&nbsp;<br />添加到Shell的参数个数&nbsp;<br />$0&nbsp;<br />Shell本身的文件名&nbsp;<br />$1～$n&nbsp;<br /><br />添加到Shell的各参数值。$1是第1参数、$2是第2参数&#8230;。&nbsp;<br />我们先写一个简单的脚本，执行以后再解释各个变量的意义&nbsp;<br /># touch variable&nbsp;<br /># vi variable&nbsp;<br />脚本内容如下：&nbsp;<br />#!/bin/sh&nbsp;<br />echo "number:$#"&nbsp;<br />echo "scname:$0"&nbsp;<br />echo "first :$1"&nbsp;<br />echo "second:$2"&nbsp;<br />echo "argume:$@"&nbsp;<br />保存退出&nbsp;<br />赋予脚本执行权限&nbsp;<br /># chmod +x variable&nbsp;<br />执行脚本&nbsp;<br /># ./variable aa bb&nbsp;<br />number:2&nbsp;<br />scname:./variable&nbsp;<br />first: aa&nbsp;<br />second:bb&nbsp;<br />argume:aa bb&nbsp;<br />通过显示结果可以看到：&nbsp;<br />$# 是传给脚本的参数个数&nbsp;<br />$0 是脚本本身的名字&nbsp;<br />$1是传递给该shell脚本的第一个参数&nbsp;<br />$2是传递给该shell脚本的第二个参数&nbsp;<br />$@ 是传给脚本的所有参数的列表<br /><br /><br /><br /></p></span></div></td></tr></tbody></table><br />Bash shell中的位置参数$#,$*,$@,$0,$1,$2...及特殊参数$?,$-等的含义<br /><br />在Bash shell中经常会见到一些比较特殊的符号，本人现收集与此，以供查阅：<br /><br /><span style="font-weight: bold; ">位置参数：</span><br />详见ABS(Advanced Bash Shell)中文翻译版103页第9章第一节内部变量，当然英文版ABS都一样啦<br /><br /><span style="color: #0001ff; ">$1, $2, $3等等...<br /><span style="color: #000102; ">位置参数,从命令行传递给脚本,或者是传递给函数.或者赋职给一个变量.<br /></span></span><span style="line-height: normal; color: #333333; font-family: Arial; font-size: 12px; ">此数目可以任意多，但只有前</span><span style="line-height: normal; color: #333333; font-family: Arial; font-size: 12px; ">9</span><span style="line-height: normal; color: #333333; font-family: Arial; font-size: 12px; ">个可以被访问，使用</span><span style="line-height: normal; color: #333333; font-family: Arial; font-size: 12px; ">shift</span><span style="line-height: normal; color: #333333; font-family: Arial; font-size: 12px; ">命令可以改变这个限制。</span><br style="line-height: normal; color: #333333; font-family: Arial; font-size: 12px; " /><span style="color: #0001ff; "><span style="color: #000102; ">(具体见Example 4-5 和Example 11-15)<br /></span></span><span style="color: #0001ff; ">$0</span><br style="line-height: normal; color: #333333; font-family: Arial; font-size: 12px; " />$0表示<span style="color: #333333; font-family: Arial; font-size: 12px; line-height: normal; ">当前执行的进程名,script 本身的名字,或者在正则表达式中表示整行输出</span><br style="line-height: normal; color: #333333; font-family: Arial; font-size: 12px; " /><br style="line-height: normal; color: #333333; font-family: Arial; font-size: 12px; " /><span style="color: #0001ff; "><span style="color: #000102; "><span style="color: #0001ff; ">$#</span><br />命令行或者是位置参数的个数.(见Example 33-2)<br /><span style="color: #0001ff; ">$*</span><br />所有的位置参数,被作为一个单词.<br />注意:"$*"必须被""引用.<br /><span style="color: #0001ff; ">$@</span><br />与$*同义,但是每个参数都是一个独立的""引用字串,这就意味着参数被完整地传递,<br />并没有被解释和扩展.这也意味着,每个参数列表中的每个参数都被当成一个独立的单词.<br />注意:"$@"必须被</span></span><span style="color: #0001ff; "><span style="color: #000102; ">""</span></span><span style="color: #0001ff; "><span style="color: #000102; ">引用.<br /></span></span><table cellspacing="0" cellpadding="0" border="0" style="table-layout: auto; font-family: Arial; width: 2px; height: 18px; "><tbody><tr></tr><tr><td style="font-size: 12px; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; "></td></tr></tbody></table><span style="line-height: normal; color: #333333; font-family: Arial; font-size: 12px; font-weight: bold; ">其他的特殊参数</span><br style="line-height: normal; color: #333333; font-family: Arial; font-size: 12px; " /><span style="line-height: normal; font-family: Arial; font-size: 12px; color: #0001ff; ">$-</span><br style="line-height: normal; color: #333333; font-family: Arial; font-size: 12px; " /><span style="color: #333333; font-family: Arial; font-size: 12px; line-height: normal; ">传递给脚本的falg(使用set 命令).参考Example 11-15.</span><span style="line-height: normal; color: #333333; font-family: Arial; font-size: 12px; "><br />显示</span><span style="line-height: normal; color: #333333; font-family: Arial; font-size: 12px; ">shell</span><span style="line-height: normal; color: #333333; font-family: Arial; font-size: 12px; ">使用的当前选项，与</span><span style="line-height: normal; color: #333333; font-family: Arial; font-size: 12px; ">set</span><span style="line-height: normal; color: #333333; font-family: Arial; font-size: 12px; ">命令功能相同</span><br style="line-height: normal; color: #333333; font-family: Arial; font-size: 12px; " /><span style="color: #333333; font-family: Arial; font-size: 12px; line-height: normal; ">注意:这起初是ksh 的特征,后来被引进到Bash 中,但不幸的是,在Bash 中它看上去也不</span><br style="line-height: normal; color: #333333; font-family: Arial; font-size: 12px; " /><span style="color: #333333; font-family: Arial; font-size: 12px; line-height: normal; ">能可靠的工作.使用它的一个可能的方法就是让这个脚本进行自我测试(查看是否是交</span><br style="line-height: normal; color: #333333; font-family: Arial; font-size: 12px; " /><span style="color: #333333; font-family: Arial; font-size: 12px; line-height: normal; ">互的).</span><br style="line-height: normal; color: #333333; font-family: Arial; font-size: 12px; " /><span style="line-height: normal; font-family: Arial; font-size: 12px; color: #0001ff; ">$!</span><br style="line-height: normal; color: #333333; font-family: Arial; font-size: 12px; " /><span style="color: #333333; font-family: Arial; font-size: 12px; line-height: normal; ">在后台运行的最后的工作的PID(进程ID).</span><br style="line-height: normal; color: #333333; font-family: Arial; font-size: 12px; " /><span style="line-height: normal; font-family: Arial; font-size: 12px; color: #0001ff; ">$_</span><br style="line-height: normal; color: #333333; font-family: Arial; font-size: 12px; " /><span style="color: #333333; font-family: Arial; font-size: 12px; line-height: normal; ">保存之前执行的命令的最后一个参数.</span><br style="line-height: normal; color: #333333; font-family: Arial; font-size: 12px; " /><span style="line-height: normal; font-family: Arial; font-size: 12px; color: #0001ff; ">$?</span><br style="line-height: normal; color: #333333; font-family: Arial; font-size: 12px; " /><span style="color: #333333; font-family: Arial; font-size: 12px; line-height: normal; ">命令,函数或者脚本本身的退出状态(见Example 23-7)</span><br style="line-height: normal; color: #333333; font-family: Arial; font-size: 12px; " /><span style="color: #333333; font-family: Arial; font-size: 12px; line-height: normal; ">用于检查上一个命令,函数或者脚本执行是否正确。（在Linux中，命令退出状态为0表示该命令正确执行，任何非0值表示命令出错。）</span><br style="line-height: normal; color: #333333; font-family: Arial; font-size: 12px; " /><span style="line-height: normal; font-family: Arial; font-size: 12px; color: #0001ff; ">$$</span><br style="line-height: normal; color: #333333; font-family: Arial; font-size: 12px; " /><span style="color: #333333; font-family: Arial; font-size: 12px; line-height: normal; ">脚本自身的进程ID.这个变量经常用来构造一个"unique"的临时文件名.</span><br style="line-height: normal; color: #333333; font-family: Arial; font-size: 12px; " /><span style="color: #333333; font-family: Arial; font-size: 12px; line-height: normal; ">(参考Example A-13,Example 29-6,Example 12-28 和Example 11-25).</span><br style="line-height: normal; color: #333333; font-family: Arial; font-size: 12px; " /><span style="color: #333333; font-family: Arial; font-size: 12px; line-height: normal; ">这通常比调用mktemp 来得简单.</span><br style="line-height: normal; color: #333333; font-family: Arial; font-size: 12px; " />注意事项:<br />[1] 当前运行的脚本的PID 为$$.<br />[2] "argument"和"parameter"这两个单词经常不加区分的使用.在这整本书中,这两个<br />单词的意思完全相同.(在翻译的时候就未加区分,统统翻译成参数)<br /><br /><span style="font-weight: bold; ">退出和返回</span><br />退出状态(exit status)<br />函数返回一个被称为退出状态的值. 退出状态可以由return 来指定statement, 否则函数的<br />退出状态是函数最后一个执行命令的退出状态(0 表示成功,非0 表示出错代码). 退出状态<br />(exit status)可以在脚本中由$? 引用. 这个机制使脚本函数也可以像C 函数一样有一个"<br />返回值".<br />return<br />终止一个函数.return 命令[1]可选地带一个整数参数,这个整数作为函数的"返回值"返回<br />给调用此函数的脚本,并且这个值也被赋给变量$?.<br /><br />while true可以写为while :&nbsp;<br /><br /><div>Example 23-7 两个数中的最大者</div><div>###################Start Script#################</div><div>#!/bin/bash</div><div># max.sh: 两个整数中的最大者.</div><div></div><div>E_PARAM_ERR=-198 # 如果传给函数的参数少于2 个时的返回值.</div><div>EQUAL=-199 # 如果两个整数值相等的返回值.</div><div># 任一个传给函数的参数值溢出</div><div>#</div><div></div><div>max2 () # 返回两个整数的较大值.</div><div>{ # 注意: 参与比较的数必须小于257.</div><div><span style="white-space:pre">	</span>if [ -z "$2" ] then</div><div><span style="white-space:pre">		</span>return $E_PARAM_ERR</div><div><span style="white-space:pre">	</span>fi</div><div></div><div><span style="white-space:pre">	</span>if [ "$1" -eq "$2" ]</div><div><span style="white-space:pre">	</span>then</div><div><span style="white-space:pre">		</span>return $EQUAL</div><div><span style="white-space:pre">	</span>else</div><div><span style="white-space:pre">		</span>if [ "$1" -gt "$2" ]</div><div><span style="white-space:pre">		</span>then</div><div><span style="white-space:pre">			</span>return $1</div><div><span style="white-space:pre">		</span>else</div><div><span style="white-space:pre">			</span>return $2</div><div><span style="white-space:pre">		</span>fi</div><div><span style="white-space:pre">	</span>fi</div><div>}</div><div></div><div>max2 33 34</div><div>return_val=$?</div><div></div><div>if [ "$return_val" -eq $E_PARAM_ERR ] then</div><div><span style="white-space:pre">	</span>echo "Need to pass two parameters to the function."</div><div>elif [ "$return_val" -eq $EQUAL ] then</div><div><span style="white-space:pre">	</span>echo "The two numbers are equal."</div><div>else</div><div><span style="white-space:pre">	</span>echo "The larger of the two numbers is $return_val."</div><div>fi</div><div></div><div></div><div>exit 0</div><div></div><div># 练习 (容易):</div><div># ---------------</div><div># 把这个脚本转化成交互式的脚本,</div><div>#+ 也就是说,让脚本可以要求调用者输入两个整数.</div><div>#####################End Script##################<br /><br /><br />来源：<a href="http://hi.baidu.com/xece/blog/item/3815a1d0038012c2562c842a.html">http://hi.baidu.com/xece/blog/item/3815a1d0038012c2562c842a.html</a></div><img src ="http://www.cppblog.com/inkcpp/aggbug/166057.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/inkcpp/" target="_blank">淋雨的感觉</a> 2012-02-20 13:13 <a href="http://www.cppblog.com/inkcpp/archive/2012/02/20/166057.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>向大家推荐一个C/C++通用Makefile</title><link>http://www.cppblog.com/inkcpp/archive/2012/02/15/165664.html</link><dc:creator>淋雨的感觉</dc:creator><author>淋雨的感觉</author><pubDate>Wed, 15 Feb 2012 05:08:00 GMT</pubDate><guid>http://www.cppblog.com/inkcpp/archive/2012/02/15/165664.html</guid><wfw:comment>http://www.cppblog.com/inkcpp/comments/165664.html</wfw:comment><comments>http://www.cppblog.com/inkcpp/archive/2012/02/15/165664.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/inkcpp/comments/commentRss/165664.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/inkcpp/services/trackbacks/165664.html</trackback:ping><description><![CDATA[<span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">[size=5]C/C++&nbsp;通用&nbsp;Makefile[/size]&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">[color=green]Generic&nbsp;Makefile&nbsp;for&nbsp;C/C++&nbsp;Program[/color]&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">==================================================&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">Keywords:&nbsp;Makefile,&nbsp;make,&nbsp;Generic,&nbsp;C/C++&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">Author:&nbsp;&nbsp;&nbsp;whyglinux&nbsp;(whyglinux&nbsp;AT&nbsp;hotmail&nbsp;DOT&nbsp;com)&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">Date:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2006-03-04&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">==================================================&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">本文提供了一个用于对&nbsp;C/C++&nbsp;程序进行编译和连接以产生可执行程序的通用&nbsp;Makefile。&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">在使用&nbsp;Makefile&nbsp;之前，只需对它进行一些简单的设置即可；而且一经设置，即使以后对源程序文件有所增减一般也不再需要改动&nbsp;Makefile。因此，即便是一个没有学习过&nbsp;Makefile&nbsp;书写规则的人，也可以为自己的&nbsp;C/C++&nbsp;程序快速建立一个可工作的&nbsp;Makefile。&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">这个&nbsp;Makefile&nbsp;可以在&nbsp;GNU&nbsp;Make&nbsp;和&nbsp;GCC&nbsp;编译器下正常工作。但是不能保证对于其它版本的&nbsp;Make&nbsp;和编译器也能正常工作。&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">如果你发现了本文中的错误，或者对本文有什么感想或建议，可通过&nbsp;whyglinux&nbsp;AT&nbsp;hotmail&nbsp;DOT&nbsp;com&nbsp;邮箱和作者联系。&nbsp;&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">此&nbsp;Makefile&nbsp;的使用方法如下：&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">[list=1][*]程序目录的组织&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">尽量将自己的源程序集中在一个目录中，并且把&nbsp;Makefile&nbsp;和源程序放在一起，这样用起来比较方便。当然，也可以将源程序分类存放在不同的目录中。&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">在程序目录中创建一个名为&nbsp;Makefile&nbsp;的文本文件，将后面列出的&nbsp;Makefile&nbsp;的内容复制到这个文件中。（注意：在复制的过程中，Makfile&nbsp;中各命令前面的&nbsp;Tab&nbsp;字符有可能被转换成若干个空格。这种情况下需要把&nbsp;Makefile&nbsp;命令前面的这些空格替换为一个&nbsp;Tab。）&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">将当前工作目录切换到&nbsp;Makefile&nbsp;所在的目录。目前，这个&nbsp;Makefile&nbsp;只支持在当前目录中的调用，不支持当前目录和&nbsp;Makefile&nbsp;所在的路径不是同一目录的情况。&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">[*]指定可执行文件&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">程序编译和连接成功后产生的可执行文件在&nbsp;Makefile&nbsp;中的&nbsp;PROGRAM&nbsp;变量中设定。这一项不能为空。为自己程序的可执行文件起一个有意义的名子吧。&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">[*]指定源程序&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">要编译的源程序由其所在的路径和文件的扩展名两项来确定。由于头文件是通过包含来使用的，所以在这里说的源程序不应包含头文件。&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">程序所在的路径在&nbsp;SRCDIRS&nbsp;中设定。如果源程序分布在不同的目录中，那么需要在&nbsp;SRCDIRS&nbsp;中一一指定，并且路径名之间用空格分隔。&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">在&nbsp;SRCEXTS&nbsp;中指定程序中使用的文件类型。C/C++&nbsp;程序的扩展名一般有比较固定的几种形式：.c、.C、.cc、.cpp、.CPP、.c++、.cp、或者.cxx（参见&nbsp;man&nbsp;gcc）。扩展名决定了程序是&nbsp;C&nbsp;还是&nbsp;C++&nbsp;程序：.c&nbsp;是&nbsp;C&nbsp;程序，其它扩展名表示&nbsp;C++&nbsp;程序。一般固定使用其中的一种扩展名即可。但是也有可能需要使用多种扩展名，这可以在&nbsp;SOURCE_EXT&nbsp;中一一指定，各个扩展名之间用空格分隔。&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">虽然并不常用，但是&nbsp;C&nbsp;程序也可以被作为&nbsp;C++&nbsp;程序编译。这可以通过在&nbsp;Makefile&nbsp;中设置&nbsp;CC&nbsp;=&nbsp;$(CXX)&nbsp;和&nbsp;CFLAGS&nbsp;=&nbsp;$(CXXFLAGS)&nbsp;两项即可实现。&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">这个&nbsp;Makefile&nbsp;支持&nbsp;C、C++&nbsp;以及&nbsp;C/C++&nbsp;混合三种编译方式：&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">[list][*]如果只指定&nbsp;.c&nbsp;扩展名，那么这是一个&nbsp;C&nbsp;程序，用&nbsp;$(CC)&nbsp;表示的编译命令进行编译和连接。&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">[*]如果指定的是除&nbsp;.c&nbsp;之外的其它扩展名（如&nbsp;.cc、.cpp、.cxx&nbsp;等），那么这是一个&nbsp;C++&nbsp;程序，用&nbsp;$(CXX)&nbsp;进行编译和连接。&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">[*]如果既指定了&nbsp;.c，又指定了其它&nbsp;C++&nbsp;扩展名，那么这是&nbsp;C/C++&nbsp;混合程序，将用&nbsp;$(CC)&nbsp;编译其中的&nbsp;C&nbsp;程序，用&nbsp;$(CXX)&nbsp;编译其中的&nbsp;C++&nbsp;程序，最后再用&nbsp;$(CXX)&nbsp;连接程序。&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">[/list]&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">这些工作都是&nbsp;make&nbsp;根据在&nbsp;Makefile&nbsp;中提供的程序文件类型（扩展名）自动判断进行的，不需要用户干预。&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">[*]指定编译选项&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">编译选项由三部分组成：预处理选项、编译选项以及连接选项，分别由&nbsp;CPPFLAGS、CFLAGS与CXXFLAGS、LDFLAGS&nbsp;指定。&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">CPPFLAGS&nbsp;选项可参考&nbsp;C&nbsp;预处理命令&nbsp;cpp&nbsp;的说明，但是注意不能包含&nbsp;-M&nbsp;以及和&nbsp;-M&nbsp;有关的选项。如果是&nbsp;C/C++&nbsp;混合编程，也可以在这里设置&nbsp;C/C++&nbsp;的一些共同的编译选项。&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">CFLAGS&nbsp;和&nbsp;CXXFLAGS&nbsp;两个变量通常用来指定编译选项。前者仅仅用于指定&nbsp;C&nbsp;程序的编译选项，后者仅仅用于指定&nbsp;C++&nbsp;程序的编译选项。其实也可以在两个变量中指定一些预处理选项（即一些本来应该放在&nbsp;CPPFLAGS&nbsp;中的选项），和&nbsp;CPPFLAGS&nbsp;并没有明确的界限。&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">连接选项在&nbsp;LDFLAGS&nbsp;中指定。如果只使用&nbsp;C/C++&nbsp;标准库，一般没有必要设置。如果使用了非标准库，应该在这里指定连接需要的选项，如库所在的路径、库名以及其它联接选项。&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">现在的库一般都提供了一个相应的&nbsp;.pc&nbsp;文件来记录使用库所需要的预编译选项、编译选项和连接选项等信息，通过&nbsp;pkg-config&nbsp;可以动态提取这些选项。与由用户显式指定各个选项相比，使用&nbsp;pkg-config&nbsp;来访问库提供的选项更方便、更具通用性。在后面可以看到一个&nbsp;GTK+&nbsp;程序的例子，其编译和连接选项的指定就是用&nbsp;pkg-config&nbsp;实现的。&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">[*]编译和连接&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">上面的各项设置好之后保存&nbsp;Makefile&nbsp;文件。执行&nbsp;make&nbsp;命令，程序就开始编译了。&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">命令&nbsp;make&nbsp;会根据&nbsp;Makefile&nbsp;中设置好的路径和文件类型搜索源程序文件，然后根据文件的类型调用相应的编译命令、使用相应的编译选项对程序进行编译。&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">编译成功之后程序的连接会自动进行。如果没有错误的话最终会产生程序的可执行文件。&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">注意：在对程序编译之后，会产生和源程序文件一一对应的&nbsp;.d&nbsp;文件。这是表示依赖关系的文件，通过它们&nbsp;make&nbsp;决定在源程序文件变动之后要进行哪些更新。为每一个源程序文件建立相应的&nbsp;.d&nbsp;文件这也是&nbsp;GNU&nbsp;Make&nbsp;推荐的方式。&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">[*]Makefile&nbsp;目标（Targets）&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">下面是关于这个&nbsp;Makefile&nbsp;提供的目标以及它所完成的功能：&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">[list][*]make&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">编译和连接程序。相当于&nbsp;make&nbsp;all。&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">[*]make&nbsp;objs&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">仅仅编译程序产生&nbsp;.o&nbsp;目标文件，不进行连接（一般很少单独使用）。&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">[*]make&nbsp;clean&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">删除编译产生的目标文件和依赖文件。&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">[*]make&nbsp;cleanall&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">删除目标文件、依赖文件以及可执行文件。&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">[*]make&nbsp;rebuild&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">重新编译和连接程序。相当于&nbsp;make&nbsp;clean&nbsp;&amp;&amp;&nbsp;make&nbsp;all。&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">[/list][/list]&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">关于这个&nbsp;Makefile&nbsp;的实现原理不准备详细解释了。如果有兴趣的话，可参考文末列出的&#8220;参考资料&#8221;。&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">Makefile&nbsp;的内容如下：&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><pre style="font-size: 14px; line-height: normal; background-color: #edf0f5; ">############################################################################### <br /># <br />#&nbsp;Generic&nbsp;Makefile&nbsp;for&nbsp;C/C++&nbsp;Program <br /># <br />#&nbsp;Author:&nbsp;whyglinux&nbsp;(whyglinux&nbsp;AT&nbsp;hotmail&nbsp;DOT&nbsp;com) <br />#&nbsp;Date:&nbsp;&nbsp;&nbsp;2006/03/04 <br /> <br />#&nbsp;Description: <br />#&nbsp;The&nbsp;makefile&nbsp;searches&nbsp;in&nbsp;&lt;SRCDIRS&gt;&nbsp;directories&nbsp;for&nbsp;the&nbsp;source&nbsp;files <br />#&nbsp;with&nbsp;extensions&nbsp;specified&nbsp;in&nbsp;&lt;SOURCE_EXT&gt;,&nbsp;then&nbsp;compiles&nbsp;the&nbsp;sources <br />#&nbsp;and&nbsp;finally&nbsp;produces&nbsp;the&nbsp;&lt;PROGRAM&gt;,&nbsp;the&nbsp;executable&nbsp;file,&nbsp;by&nbsp;linking <br />#&nbsp;the&nbsp;objectives. <br /> <br />#&nbsp;Usage: <br />#&nbsp;&nbsp;&nbsp;$&nbsp;make&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;compile&nbsp;and&nbsp;link&nbsp;the&nbsp;program. <br />#&nbsp;&nbsp;&nbsp;$&nbsp;make&nbsp;objs&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;compile&nbsp;only&nbsp;(no&nbsp;linking.&nbsp;Rarely&nbsp;used). <br />#&nbsp;&nbsp;&nbsp;$&nbsp;make&nbsp;clean&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;clean&nbsp;the&nbsp;objectives&nbsp;and&nbsp;dependencies. <br />#&nbsp;&nbsp;&nbsp;$&nbsp;make&nbsp;cleanall&nbsp;&nbsp;clean&nbsp;the&nbsp;objectives,&nbsp;dependencies&nbsp;and&nbsp;executable. <br />#&nbsp;&nbsp;&nbsp;$&nbsp;make&nbsp;rebuild&nbsp;&nbsp;&nbsp;rebuild&nbsp;the&nbsp;program.&nbsp;The&nbsp;same&nbsp;as&nbsp;make&nbsp;clean&nbsp;&amp;&amp;&nbsp;make&nbsp;all. <br />#============================================================================== <br /> <br />##&nbsp;Customizing&nbsp;Section:&nbsp;adjust&nbsp;the&nbsp;following&nbsp;if&nbsp;necessary. <br />##============================================================================= <br /> <br />#&nbsp;The&nbsp;executable&nbsp;file&nbsp;name. <br />#&nbsp;It&nbsp;must&nbsp;be&nbsp;specified. <br />#&nbsp;PROGRAM&nbsp;&nbsp;&nbsp;:=&nbsp;a.out&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;the&nbsp;executable&nbsp;name <br />PROGRAM&nbsp;&nbsp;&nbsp;:= <br /> <br />#&nbsp;The&nbsp;directories&nbsp;in&nbsp;which&nbsp;source&nbsp;files&nbsp;reside. <br />#&nbsp;At&nbsp;least&nbsp;one&nbsp;path&nbsp;should&nbsp;be&nbsp;specified. <br />#&nbsp;SRCDIRS&nbsp;&nbsp;&nbsp;:=&nbsp;.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;current&nbsp;directory <br />SRCDIRS&nbsp;&nbsp;&nbsp;:= <br /> <br />#&nbsp;The&nbsp;source&nbsp;file&nbsp;types&nbsp;(headers&nbsp;excluded). <br />#&nbsp;At&nbsp;least&nbsp;one&nbsp;type&nbsp;should&nbsp;be&nbsp;specified. <br />#&nbsp;The&nbsp;valid&nbsp;suffixes&nbsp;are&nbsp;among&nbsp;of&nbsp;.c,&nbsp;.C,&nbsp;.cc,&nbsp;.cpp,&nbsp;.CPP,&nbsp;.c++,&nbsp;.cp,&nbsp;or&nbsp;.cxx. <br />#&nbsp;SRCEXTS&nbsp;&nbsp;&nbsp;:=&nbsp;.c&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;C&nbsp;program <br />#&nbsp;SRCEXTS&nbsp;&nbsp;&nbsp;:=&nbsp;.cpp&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;C++&nbsp;program <br />#&nbsp;SRCEXTS&nbsp;&nbsp;&nbsp;:=&nbsp;.c&nbsp;.cpp&nbsp;#&nbsp;C/C++&nbsp;program <br />SRCEXTS&nbsp;&nbsp;&nbsp;:= <br /> <br />#&nbsp;The&nbsp;flags&nbsp;used&nbsp;by&nbsp;the&nbsp;cpp&nbsp;(man&nbsp;cpp&nbsp;for&nbsp;more). <br />#&nbsp;CPPFLAGS&nbsp;&nbsp;:=&nbsp;-Wall&nbsp;-Werror&nbsp;#&nbsp;show&nbsp;all&nbsp;warnings&nbsp;and&nbsp;take&nbsp;them&nbsp;as&nbsp;errors <br />CPPFLAGS&nbsp;&nbsp;:= <br /> <br />#&nbsp;The&nbsp;compiling&nbsp;flags&nbsp;used&nbsp;only&nbsp;for&nbsp;C. <br />#&nbsp;If&nbsp;it&nbsp;is&nbsp;a&nbsp;C++&nbsp;program,&nbsp;no&nbsp;need&nbsp;to&nbsp;set&nbsp;these&nbsp;flags. <br />#&nbsp;If&nbsp;it&nbsp;is&nbsp;a&nbsp;C&nbsp;and&nbsp;C++&nbsp;merging&nbsp;program,&nbsp;set&nbsp;these&nbsp;flags&nbsp;for&nbsp;the&nbsp;C&nbsp;parts. <br />CFLAGS&nbsp;&nbsp;&nbsp;&nbsp;:= <br />CFLAGS&nbsp;&nbsp;&nbsp;&nbsp;+= <br /> <br />#&nbsp;The&nbsp;compiling&nbsp;flags&nbsp;used&nbsp;only&nbsp;for&nbsp;C++. <br />#&nbsp;If&nbsp;it&nbsp;is&nbsp;a&nbsp;C&nbsp;program,&nbsp;no&nbsp;need&nbsp;to&nbsp;set&nbsp;these&nbsp;flags. <br />#&nbsp;If&nbsp;it&nbsp;is&nbsp;a&nbsp;C&nbsp;and&nbsp;C++&nbsp;merging&nbsp;program,&nbsp;set&nbsp;these&nbsp;flags&nbsp;for&nbsp;the&nbsp;C++&nbsp;parts. <br />CXXFLAGS&nbsp;&nbsp;:= <br />CXXFLAGS&nbsp;&nbsp;+= <br /> <br />#&nbsp;The&nbsp;library&nbsp;and&nbsp;the&nbsp;link&nbsp;options&nbsp;(&nbsp;C&nbsp;and&nbsp;C++&nbsp;common). <br />LDFLAGS&nbsp;&nbsp;&nbsp;:= <br />LDFLAGS&nbsp;&nbsp;&nbsp;+= <br /> <br />##&nbsp;Implict&nbsp;Section:&nbsp;change&nbsp;the&nbsp;following&nbsp;only&nbsp;when&nbsp;necessary. <br />##============================================================================= <br />#&nbsp;The&nbsp;C&nbsp;program&nbsp;compiler.&nbsp;Uncomment&nbsp;it&nbsp;to&nbsp;specify&nbsp;yours&nbsp;explicitly. <br />#CC&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;gcc <br /> <br />#&nbsp;The&nbsp;C++&nbsp;program&nbsp;compiler.&nbsp;Uncomment&nbsp;it&nbsp;to&nbsp;specify&nbsp;yours&nbsp;explicitly. <br />#CXX&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;g++ <br /> <br />#&nbsp;Uncomment&nbsp;the&nbsp;2&nbsp;lines&nbsp;to&nbsp;compile&nbsp;C&nbsp;programs&nbsp;as&nbsp;C++&nbsp;ones. <br />#CC&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;$(CXX) <br />#CFLAGS&nbsp;&nbsp;=&nbsp;$(CXXFLAGS) <br /> <br />#&nbsp;The&nbsp;command&nbsp;used&nbsp;to&nbsp;delete&nbsp;file. <br />#RM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;rm&nbsp;-f <br /> <br />##&nbsp;Stable&nbsp;Section:&nbsp;usually&nbsp;no&nbsp;need&nbsp;to&nbsp;be&nbsp;changed.&nbsp;But&nbsp;you&nbsp;can&nbsp;add&nbsp;more. <br />##============================================================================= <br />SHELL&nbsp;&nbsp;&nbsp;=&nbsp;/bin/sh <br />SOURCES&nbsp;=&nbsp;$(foreach&nbsp;d,$(SRCDIRS),$(wildcard&nbsp;$(addprefix&nbsp;$(d)/*,$(SRCEXTS)))) <br />OBJS&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;$(foreach&nbsp;x,$(SRCEXTS),&nbsp;\ <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$(patsubst&nbsp;%$(x),%.o,$(filter&nbsp;%$(x),$(SOURCES)))) <br />DEPS&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;$(patsubst&nbsp;%.o,%.d,$(OBJS)) <br /> <br />.PHONY&nbsp;:&nbsp;all&nbsp;objs&nbsp;clean&nbsp;cleanall&nbsp;rebuild <br /> <br />all&nbsp;:&nbsp;$(PROGRAM) <br /> <br />#&nbsp;Rules&nbsp;for&nbsp;creating&nbsp;the&nbsp;dependency&nbsp;files&nbsp;(.d). <br />#--------------------------------------------------- <br />%.d&nbsp;:&nbsp;%.c <br />	@$(CC)&nbsp;-MM&nbsp;-MD&nbsp;$(CFLAGS)&nbsp;$&lt; <br /> <br />%.d&nbsp;:&nbsp;%.C <br />	@$(CC)&nbsp;-MM&nbsp;-MD&nbsp;$(CXXFLAGS)&nbsp;$&lt; <br /> <br />%.d&nbsp;:&nbsp;%.cc <br />	@$(CC)&nbsp;-MM&nbsp;-MD&nbsp;$(CXXFLAGS)&nbsp;$&lt; <br /> <br />%.d&nbsp;:&nbsp;%.cpp <br />	@$(CC)&nbsp;-MM&nbsp;-MD&nbsp;$(CXXFLAGS)&nbsp;$&lt; <br /> <br />%.d&nbsp;:&nbsp;%.CPP <br />	@$(CC)&nbsp;-MM&nbsp;-MD&nbsp;$(CXXFLAGS)&nbsp;$&lt; <br /> <br />%.d&nbsp;:&nbsp;%.c++ <br />	@$(CC)&nbsp;-MM&nbsp;-MD&nbsp;$(CXXFLAGS)&nbsp;$&lt; <br /> <br />%.d&nbsp;:&nbsp;%.cp <br />	@$(CC)&nbsp;-MM&nbsp;-MD&nbsp;$(CXXFLAGS)&nbsp;$&lt; <br /> <br />%.d&nbsp;:&nbsp;%.cxx <br />	@$(CC)&nbsp;-MM&nbsp;-MD&nbsp;$(CXXFLAGS)&nbsp;$&lt; <br /> <br />#&nbsp;Rules&nbsp;for&nbsp;producing&nbsp;the&nbsp;objects. <br />#--------------------------------------------------- <br />objs&nbsp;:&nbsp;$(OBJS) <br /> <br />%.o&nbsp;:&nbsp;%.c <br />	$(CC)&nbsp;-c&nbsp;$(CPPFLAGS)&nbsp;$(CFLAGS)&nbsp;$&lt; <br /> <br />%.o&nbsp;:&nbsp;%.C <br />	$(CXX)&nbsp;-c&nbsp;$(CPPFLAGS)&nbsp;$(CXXFLAGS)&nbsp;$&lt; <br /> <br />%.o&nbsp;:&nbsp;%.cc <br />	$(CXX)&nbsp;-c&nbsp;$(CPPFLAGS)&nbsp;$(CXXFLAGS)&nbsp;$&lt; <br /> <br />%.o&nbsp;:&nbsp;%.cpp <br />	$(CXX)&nbsp;-c&nbsp;$(CPPFLAGS)&nbsp;$(CXXFLAGS)&nbsp;$&lt; <br /> <br />%.o&nbsp;:&nbsp;%.CPP <br />	$(CXX)&nbsp;-c&nbsp;$(CPPFLAGS)&nbsp;$(CXXFLAGS)&nbsp;$&lt; <br /> <br />%.o&nbsp;:&nbsp;%.c++ <br />	$(CXX&nbsp;-c&nbsp;$(CPPFLAGS)&nbsp;$(CXXFLAGS)&nbsp;$&lt; <br /> <br />%.o&nbsp;:&nbsp;%.cp <br />	$(CXX)&nbsp;-c&nbsp;$(CPPFLAGS)&nbsp;$(CXXFLAGS)&nbsp;$&lt; <br /> <br />%.o&nbsp;:&nbsp;%.cxx <br />	$(CXX)&nbsp;-c&nbsp;$(CPPFLAGS)&nbsp;$(CXXFLAGS)&nbsp;$&lt; <br /> <br />#&nbsp;Rules&nbsp;for&nbsp;producing&nbsp;the&nbsp;executable. <br />#---------------------------------------------- <br />$(PROGRAM)&nbsp;:&nbsp;$(OBJS) <br />ifeq&nbsp;($(strip&nbsp;$(SRCEXTS)),&nbsp;.c)&nbsp;&nbsp;#&nbsp;C&nbsp;file <br />	$(CC)&nbsp;-o&nbsp;$(PROGRAM)&nbsp;$(OBJS)&nbsp;$(LDFLAGS) <br />else&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;C++&nbsp;file <br />	$(CXX)&nbsp;-o&nbsp;$(PROGRAM)&nbsp;$(OBJS)&nbsp;$(LDFLAGS) <br />endif <br /> <br />-include&nbsp;$(DEPS) <br /> <br />rebuild:&nbsp;clean&nbsp;all <br /> <br />clean&nbsp;: <br />	@$(RM)&nbsp;*.o&nbsp;*.d <br /> <br />cleanall:&nbsp;clean <br />	@$(RM)&nbsp;$(PROGRAM)&nbsp;$(PROGRAM).exe <br /> <br />###&nbsp;End&nbsp;of&nbsp;the&nbsp;Makefile&nbsp;##&nbsp;&nbsp;Suggestions&nbsp;are&nbsp;welcome&nbsp;&nbsp;##&nbsp;All&nbsp;rights&nbsp;reserved&nbsp;### <br />############################################################################### <br /></pre><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">下面提供两个例子来具体说明上面&nbsp;Makefile&nbsp;的用法。&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">[color=darkred]例一　Hello&nbsp;World&nbsp;程序[/color]&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">这个程序的功能是输出&nbsp;Hello,&nbsp;world!&nbsp;这样一行文字。由&nbsp;hello.h、hello.c、main.cxx&nbsp;三个文件组成。前两个文件是&nbsp;C&nbsp;程序，后一个是&nbsp;C++&nbsp;程序，因此这是一个&nbsp;C&nbsp;和&nbsp;C++&nbsp;混编程序。&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><pre style="font-size: 14px; line-height: normal; background-color: #edf0f5; ">/*&nbsp;File&nbsp;name:&nbsp;hello.h <br />&nbsp;*&nbsp;C&nbsp;header&nbsp;file <br />&nbsp;*/ <br /> <br />#ifndef&nbsp;HELLO_H <br />#define&nbsp;HELLO_H <br /> <br />#ifdef&nbsp;__cplusplus <br />extern&nbsp;"C"&nbsp;{ <br />#endif <br /> <br />&nbsp;&nbsp;void&nbsp;print_hello(); <br /> <br />#ifdef&nbsp;__cplusplus <br />} <br />#endif <br /> <br />#endif <br /></pre><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><pre style="font-size: 14px; line-height: normal; background-color: #edf0f5; ">/*&nbsp;File&nbsp;name:&nbsp;hello.c <br />&nbsp;*&nbsp;C&nbsp;source&nbsp;file. <br />&nbsp;*/ <br />#include&nbsp;"hello.h" <br />#include&nbsp;&lt;stdio.h&gt; <br /> <br />void&nbsp;print_hello() <br />{ <br />&nbsp;&nbsp;puts(&nbsp;"Hello,&nbsp;world!"&nbsp;); <br />} <br /></pre><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><pre style="font-size: 14px; line-height: normal; background-color: #edf0f5; ">/*&nbsp;File&nbsp;name:&nbsp;main.cxx <br />&nbsp;*&nbsp;C++&nbsp;source&nbsp;file. <br />&nbsp;*/ <br />#include&nbsp;"hello.h" <br /> <br />int&nbsp;main() <br />{ <br />&nbsp;&nbsp;print_hello(); <br /> <br />&nbsp;&nbsp;return&nbsp;0; <br />} <br /></pre><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">建立一个新的目录，然后把这三个文件拷贝到目录中，也把&nbsp;Makefile&nbsp;文件拷贝到目录中。之后，对&nbsp;Makefile&nbsp;的相关项目进行如下设置：&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><pre style="font-size: 14px; line-height: normal; background-color: #edf0f5; ">PROGRAM&nbsp;&nbsp;&nbsp;:=&nbsp;hello&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;设置运行程序名 <br />SRCDIRS&nbsp;&nbsp;&nbsp;:=&nbsp;.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;源程序位于当前目录下 <br />SRCEXTS&nbsp;&nbsp;&nbsp;:=&nbsp;.c&nbsp;.cxx&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;源程序文件有&nbsp;.c&nbsp;和&nbsp;.cxx&nbsp;两种类型 <br />CFLAGS&nbsp;&nbsp;&nbsp;&nbsp;:=&nbsp;-g&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;为&nbsp;C&nbsp;目标程序包含&nbsp;GDB&nbsp;可用的调试信息 <br />CXXFLAGS&nbsp;&nbsp;:=&nbsp;-g&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;为&nbsp;C++&nbsp;目标程序包含&nbsp;GDB&nbsp;可用的调试信息 <br /></pre><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">由于这个简单的程序只使用了&nbsp;C&nbsp;标准库的函数（puts），所以对于&nbsp;CFLAGS&nbsp;和&nbsp;CXXFLAGS&nbsp;没有过多的要求，LDFLAGS&nbsp;和&nbsp;CPPFLAGS&nbsp;选项也无需设置。&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">经过上面的设置之后，执行&nbsp;make&nbsp;命令就可以编译程序了。如果没有错误出现的话，./hello&nbsp;&nbsp;就可以运行程序了。&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">如果修改了源程序的话，可以看到只有和修改有关的源文件被编译。也可以再为程序添加新的源文件，只要它们的扩展名是已经在&nbsp;Makefile&nbsp;中设置过的，那么就没有必要修改　Makefile。&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">[color=darkred]例二　GTK+&nbsp;版&nbsp;Hello&nbsp;World&nbsp;程序[/color]&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">这个&nbsp;GTK+&nbsp;2.0&nbsp;版的&nbsp;Hello&nbsp;World&nbsp;程序可以从下面的网址上得到：http://www.gtk.org/tutorial/c58.html#SEC-HELLOWORLD。当然，要编译&nbsp;GTK+&nbsp;程序，还需要你的系统上已经安装好了&nbsp;GTK+。&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">跟第一个例子一样，单独创建一个新的目录，把上面网页中提供的程序保存为&nbsp;main.c&nbsp;文件。对&nbsp;Makefile&nbsp;做如下设置：&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><pre style="font-size: 14px; line-height: normal; background-color: #edf0f5; ">PROGRAM&nbsp;&nbsp;&nbsp;:=&nbsp;hello&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;设置运行程序名 <br />SRCDIRS&nbsp;&nbsp;&nbsp;:=&nbsp;.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;源程序位于当前目录下 <br />SRCEXTS&nbsp;&nbsp;&nbsp;:=&nbsp;.c&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;源程序文件只有&nbsp;.c&nbsp;一种类型 <br />CFLAGS&nbsp;&nbsp;&nbsp;&nbsp;:=&nbsp;`pkg-config&nbsp;--cflags&nbsp;gtk+-2.0`&nbsp;&nbsp;#&nbsp;CFLAGS <br />LDFLAGS&nbsp;&nbsp;&nbsp;:=&nbsp;`pkg-config&nbsp;--libs&nbsp;gtk+-2.0`&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;LDFLAGS <br /></pre><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">这是一个&nbsp;C&nbsp;程序，所以&nbsp;CXXFLAGS&nbsp;没有必要设置&#8212;&#8212;即使被设置了也不会被使用。&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">编译和连接&nbsp;GTK+&nbsp;库所需要的&nbsp;CFLAGS&nbsp;和&nbsp;LDFLAGS&nbsp;由&nbsp;pkg-config&nbsp;程序自动产生。&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">现在就可以运行&nbsp;make&nbsp;命令编译、./hello&nbsp;执行这个&nbsp;GTK+&nbsp;程序了。&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">参考资料：&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">[list=1][*]Multi-file&nbsp;projects&nbsp;and&nbsp;the&nbsp;GNU&nbsp;Make&nbsp;utility&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">Author:&nbsp;George&nbsp;Foot&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">http://www.elitecoders.de/mags/cscene/CS2/CS2-10.html&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">[*]GNU&nbsp;Make&nbsp;Manual&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">http://www.gnu.org/software/make/manual/&nbsp;</span><br style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; " /><span style="font-family: Simsun; font-size: 14px; line-height: normal; background-color: #edf0f5; ">[/list]</span>&nbsp;<br /><br />来源：<a href="http://www.chinaunix.net/jh/23/711092.html">http://www.chinaunix.net/jh/23/711092.html</a><img src ="http://www.cppblog.com/inkcpp/aggbug/165664.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/inkcpp/" target="_blank">淋雨的感觉</a> 2012-02-15 13:08 <a href="http://www.cppblog.com/inkcpp/archive/2012/02/15/165664.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>收集一个好用的vimrc备份</title><link>http://www.cppblog.com/inkcpp/archive/2012/02/15/165660.html</link><dc:creator>淋雨的感觉</dc:creator><author>淋雨的感觉</author><pubDate>Wed, 15 Feb 2012 04:42:00 GMT</pubDate><guid>http://www.cppblog.com/inkcpp/archive/2012/02/15/165660.html</guid><wfw:comment>http://www.cppblog.com/inkcpp/comments/165660.html</wfw:comment><comments>http://www.cppblog.com/inkcpp/archive/2012/02/15/165660.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/inkcpp/comments/commentRss/165660.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/inkcpp/services/trackbacks/165660.html</trackback:ping><description><![CDATA[<div>" vgod's vimrc</div><div>" Tsung-Hsiang (Sean) Chang &lt;vgod@vgod.tw&gt;</div><div>" Fork me on GITHUB &nbsp;https://github.com/vgod/vimrc</div><div></div><div>" read https://github.com/vgod/vimrc/blob/master/README.md for more info</div><div></div><div></div><div>" For pathogen.vim: auto load all plugins in .vim/bundle</div><div>call pathogen#runtime_append_all_bundles()</div><div>call pathogen#helptags()</div><div></div><div>" General Settings</div><div></div><div>set nocompatible<span style="white-space:pre">	</span>" not compatible with the old-fashion vi mode</div><div>set bs=2<span style="white-space:pre">		</span>" allow backspacing over everything in insert mode</div><div>set history=50<span style="white-space:pre">		</span>" keep 50 lines of command line history</div><div>set ruler<span style="white-space:pre">		</span>" show the cursor position all the time</div><div>set autoread<span style="white-space:pre">		</span>" auto read when file is changed from outside</div><div>set number &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;" show line numbers</div><div></div><div>filetype on &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; " Enable filetype detection</div><div>filetype indent on &nbsp; &nbsp;" Enable filetype-specific indenting</div><div>filetype plugin on &nbsp; &nbsp;" Enable filetype-specific plugins</div><div></div><div></div><div>" auto reload vimrc when editing it</div><div>autocmd! bufwritepost .vimrc source ~/.vimrc</div><div></div><div></div><div>syntax on<span style="white-space:pre">		</span>" syntax highlight</div><div>set hlsearch<span style="white-space:pre">		</span>" search highlighting</div><div></div><div>if has("gui_running")<span style="white-space:pre">	</span>" GUI color and font settings</div><div>&nbsp; set guifont=Osaka-Mono:h20</div><div>&nbsp; set background=dark&nbsp;</div><div>&nbsp; set t_Co=256 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;" 256 color mode</div><div>&nbsp; set cursorline &nbsp; &nbsp; &nbsp; &nbsp;" highlight current line</div><div>&nbsp; colors moria</div><div>else</div><div>" terminal color settings</div><div>&nbsp; colors ir_black</div><div>endif</div><div></div><div>"set clipboard=unnamed<span style="white-space:pre">	</span>" yank to the system register (*) by default --</div><div>"somehow I hate this configuration</div><div>set showmatch<span style="white-space:pre">		</span>" Cursor shows matching ) and }</div><div>set showmode<span style="white-space:pre">		</span>" Show current mode</div><div>set wildchar=&lt;TAB&gt;<span style="white-space:pre">	</span>" start wild expansion in the command line using &lt;TAB&gt;</div><div>set wildmenu &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;" wild char completion menu</div><div></div><div>" ignore these files while expanding wild chars</div><div>set wildignore=*.o,*.class,*.pyc</div><div></div><div>set autoindent<span style="white-space:pre">		</span>" auto indentation</div><div>set incsearch<span style="white-space:pre">		</span>" incremental search</div><div>set nobackup<span style="white-space:pre">		</span>" no *~ backup files</div><div>set copyindent<span style="white-space:pre">		</span>" copy the previous indentation on autoindenting</div><div>set ignorecase<span style="white-space:pre">		</span>" ignore case when searching</div><div>set smartcase<span style="white-space:pre">		</span>" ignore case if search pattern is all lowercase,case-sensitive otherwise</div><div>set smarttab<span style="white-space:pre">		</span>" insert tabs on the start of a line according to context</div><div></div><div>" disable sound on errors</div><div>set noerrorbells</div><div>set novisualbell</div><div>set t_vb=</div><div>set tm=500</div><div></div><div>" TAB setting{</div><div>&nbsp; &nbsp;set expandtab &nbsp; &nbsp; &nbsp; &nbsp;"replace &lt;TAB&gt; with spaces</div><div>&nbsp; &nbsp;set softtabstop=3&nbsp;</div><div>&nbsp; &nbsp;set shiftwidth=3&nbsp;</div><div></div><div>&nbsp; &nbsp;au FileType Makefile set noexpandtab</div><div>"} &nbsp; &nbsp; &nbsp;<span style="white-space:pre">							</span></div><div></div><div>" status line {</div><div>set laststatus=2</div><div>set statusline=\ %{HasPaste()}%&lt;%-15.25(%f%)%m%r%h\ %w\ \&nbsp;</div><div>set statusline+=\ \ \ [%{&amp;ff}/%Y]&nbsp;</div><div>set statusline+=\ \ \ %&lt;%20.30(%{hostname()}:%{CurDir()}%)\&nbsp;</div><div>set statusline+=%=%-10.(%l,%c%V%)\ %p%%/%L</div><div></div><div>function! CurDir()</div><div>&nbsp; &nbsp; let curdir = substitute(getcwd(), $HOME, "~", "")</div><div>&nbsp; &nbsp; return curdir</div><div>endfunction</div><div></div><div>function! HasPaste()</div><div>&nbsp; &nbsp; if &amp;paste</div><div>&nbsp; &nbsp; &nbsp; &nbsp; return '[PASTE]'</div><div>&nbsp; &nbsp; else</div><div>&nbsp; &nbsp; &nbsp; &nbsp; return ''</div><div>&nbsp; &nbsp; endif</div><div>endfunction</div><div></div><div>"}</div><div></div><div></div><div>" C/C++ specific settings</div><div>autocmd FileType c,cpp,cc &nbsp;set cindent comments=sr:/*,mb:*,el:*/,:// cino=&gt;s,e0,n0,f0,{0,}0,^-1s,:0,=s,g0,h1s,p2,t0,+2,(2,)20,*30</div><div></div><div>"Restore cursor to file position in previous editing session</div><div>set viminfo='10,\"100,:20,%,n~/.viminfo</div><div>au BufReadPost * if line("'\"") &gt; 0|if line("'\"") &lt;= line("$")|exe("norm '\"")|else|exe "norm $"|endif|endif</div><div></div><div>"---------------------------------------------------------------------------&nbsp;</div><div>" Tip #382: Search for &lt;cword&gt; and replace with input() in all open buffers&nbsp;</div><div>"---------------------------------------------------------------------------&nbsp;</div><div>fun! Replace()&nbsp;</div><div>&nbsp; &nbsp; let s:word = input("Replace " . expand('&lt;cword&gt;') . " with:")&nbsp;</div><div>&nbsp; &nbsp; :exe 'bufdo! %s/\&lt;' . expand('&lt;cword&gt;') . '\&gt;/' . s:word . '/ge'&nbsp;</div><div>&nbsp; &nbsp; :unlet! s:word&nbsp;</div><div>endfun&nbsp;</div><div></div><div></div><div>"---------------------------------------------------------------------------&nbsp;</div><div>" USEFUL SHORTCUTS</div><div>"---------------------------------------------------------------------------&nbsp;</div><div>" set leader to ,</div><div>let mapleader=","</div><div>let g:mapleader=","</div><div></div><div>"replace the current word in all opened buffers</div><div>map &lt;leader&gt;r :call Replace()&lt;CR&gt;</div><div></div><div>" open the error console</div><div>map &lt;leader&gt;cc :botright cope&lt;CR&gt;&nbsp;</div><div>" move to next error</div><div>map &lt;leader&gt;] :cn&lt;CR&gt;</div><div>" move to the prev error</div><div>map &lt;leader&gt;[ :cp&lt;CR&gt;</div><div></div><div>" --- move around splits {</div><div>" move to and maximize the below split&nbsp;</div><div>map &lt;C-J&gt; &lt;C-W&gt;j&lt;C-W&gt;_</div><div>" move to and maximize the above split&nbsp;</div><div>map &lt;C-K&gt; &lt;C-W&gt;k&lt;C-W&gt;_</div><div>" move to and maximize the left split&nbsp;</div><div>nmap &lt;c-h&gt; &lt;c-w&gt;h&lt;c-w&gt;&lt;bar&gt;</div><div>" move to and maximize the right split &nbsp;</div><div>nmap &lt;c-l&gt; &lt;c-w&gt;l&lt;c-w&gt;&lt;bar&gt;</div><div>set wmw=0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; " set the min width of a window to 0 so we can maximize others&nbsp;</div><div>set wmh=0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; " set the min height of a window to 0 so we can maximize others</div><div>" }</div><div></div><div>" move around tabs. conflict with the original screen top/bottom</div><div>" comment them out if you want the original H/L</div><div>" go to prev tab&nbsp;</div><div>map &lt;S-H&gt; gT</div><div>" go to next tab</div><div>map &lt;S-L&gt; gt</div><div></div><div>" new tab</div><div>map &lt;C-t&gt;&lt;C-t&gt; :tabnew&lt;CR&gt;</div><div>" close tab</div><div>map &lt;C-t&gt;&lt;C-w&gt; :tabclose&lt;CR&gt;&nbsp;</div><div></div><div>" ,/ turn off search highlighting</div><div>nmap &lt;leader&gt;/ :nohl&lt;CR&gt;</div><div></div><div>" Bash like keys for the command line</div><div>cnoremap &lt;C-A&gt; &nbsp; &nbsp; &nbsp;&lt;Home&gt;</div><div>cnoremap &lt;C-E&gt; &nbsp; &nbsp; &nbsp;&lt;End&gt;</div><div>cnoremap &lt;C-K&gt; &nbsp; &nbsp; &nbsp;&lt;C-U&gt;</div><div></div><div>" ,p toggles paste mode</div><div>nmap &lt;leader&gt;p :set paste!&lt;BAR&gt;set paste?&lt;CR&gt;</div><div></div><div>" allow multiple indentation/deindentation in visual mode</div><div>vnoremap &lt; &lt;gv</div><div>vnoremap &gt; &gt;gv</div><div></div><div>" :cd. change working directory to that of the current file</div><div>cmap cd. lcd %:p:h</div><div></div><div>" Writing Restructured Text (Sphinx Documentation) {</div><div>&nbsp; &nbsp;" Ctrl-u 1: &nbsp; &nbsp;underline Parts w/ #'s</div><div>&nbsp; &nbsp;noremap &nbsp;&lt;C-u&gt;1 yyPVr#yyjp</div><div>&nbsp; &nbsp;inoremap &lt;C-u&gt;1 &lt;esc&gt;yyPVr#yyjpA</div><div>&nbsp; &nbsp;" Ctrl-u 2: &nbsp; &nbsp;underline Chapters w/ *'s</div><div>&nbsp; &nbsp;noremap &nbsp;&lt;C-u&gt;2 yyPVr*yyjp</div><div>&nbsp; &nbsp;inoremap &lt;C-u&gt;2 &lt;esc&gt;yyPVr*yyjpA</div><div>&nbsp; &nbsp;" Ctrl-u 3: &nbsp; &nbsp;underline Section Level 1 w/ ='s</div><div>&nbsp; &nbsp;noremap &nbsp;&lt;C-u&gt;3 yypVr=</div><div>&nbsp; &nbsp;inoremap &lt;C-u&gt;3 &lt;esc&gt;yypVr=A</div><div>&nbsp; &nbsp;" Ctrl-u 4: &nbsp; &nbsp;underline Section Level 2 w/ -'s</div><div>&nbsp; &nbsp;noremap &nbsp;&lt;C-u&gt;4 yypVr-</div><div>&nbsp; &nbsp;inoremap &lt;C-u&gt;4 &lt;esc&gt;yypVr-A</div><div>&nbsp; &nbsp;" Ctrl-u 5: &nbsp; &nbsp;underline Section Level 3 w/ ^'s</div><div>&nbsp; &nbsp;noremap &nbsp;&lt;C-u&gt;5 yypVr^</div><div>&nbsp; &nbsp;inoremap &lt;C-u&gt;5 &lt;esc&gt;yypVr^A</div><div>"}</div><div></div><div>"---------------------------------------------------------------------------&nbsp;</div><div>" PROGRAMMING SHORTCUTS</div><div>"---------------------------------------------------------------------------&nbsp;</div><div></div><div>" Ctrl-[ jump out of the tag stack (undo Ctrl-])</div><div>map &lt;C-[&gt; &lt;ESC&gt;:po&lt;CR&gt;</div><div></div><div>" ,g generates the header guard</div><div>map &lt;leader&gt;g :call IncludeGuard()&lt;CR&gt;</div><div>fun! IncludeGuard()</div><div>&nbsp; &nbsp;let basename = substitute(bufname(""), '.*/', '', '')</div><div>&nbsp; &nbsp;let guard = '_' . substitute(toupper(basename), '\.', '_', "H")</div><div>&nbsp; &nbsp;call append(0, "#ifndef " . guard)</div><div>&nbsp; &nbsp;call append(1, "#define " . guard)</div><div>&nbsp; &nbsp;call append( line("$"), "#endif // for #ifndef " . guard)</div><div>endfun</div><div></div><div></div><div></div><div>" Enable omni completion. (Ctrl-X Ctrl-O)</div><div>autocmd FileType html,markdown setlocal omnifunc=htmlcomplete#CompleteTags</div><div>autocmd FileType javascript setlocal omnifunc=javascriptcomplete#CompleteJS</div><div>autocmd FileType python setlocal omnifunc=pythoncomplete#Complete</div><div>autocmd FileType xml setlocal omnifunc=xmlcomplete#CompleteTags</div><div>autocmd FileType css set omnifunc=csscomplete#CompleteCSS</div><div>autocmd FileType c set omnifunc=ccomplete#Complete</div><div>autocmd FileType java set omnifunc=javacomplete#Complete</div><div></div><div>" use syntax complete if nothing else available</div><div>if has("autocmd") &amp;&amp; exists("+omnifunc")</div><div>&nbsp; autocmd Filetype *</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \<span style="white-space:pre">	</span>if &amp;omnifunc == "" |</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \<span style="white-space:pre">		</span>setlocal omnifunc=syntaxcomplete#Complete |</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \<span style="white-space:pre">	</span>endif</div><div>endif</div><div></div><div>set cot-=preview "disable doc preview in omnicomplete</div><div></div><div>" make CSS omnicompletion work for SASS and SCSS</div><div>autocmd BufNewFile,BufRead *.scss &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; set ft=scss.css</div><div>autocmd BufNewFile,BufRead *.sass &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; set ft=sass.css</div><div></div><div>"---------------------------------------------------------------------------&nbsp;</div><div>" ENCODING SETTINGS</div><div>"---------------------------------------------------------------------------&nbsp;</div><div>set encoding=utf-8 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div><div>set termencoding=utf-8</div><div>set fileencoding=utf-8</div><div>set fileencodings=ucs-bom,utf-8,big5,gb2312,latin1</div><div></div><div>fun! ViewUTF8()</div><div><span style="white-space:pre">	</span>set encoding=utf-8 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div><div><span style="white-space:pre">	</span>set termencoding=big5</div><div>endfun</div><div></div><div>fun! UTF8()</div><div><span style="white-space:pre">	</span>set encoding=utf-8 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div><div><span style="white-space:pre">	</span>set termencoding=big5</div><div><span style="white-space:pre">	</span>set fileencoding=utf-8</div><div><span style="white-space:pre">	</span>set fileencodings=ucs-bom,big5,utf-8,latin1</div><div>endfun</div><div></div><div>fun! Big5()</div><div><span style="white-space:pre">	</span>set encoding=big5</div><div><span style="white-space:pre">	</span>set fileencoding=big5</div><div>endfun</div><div></div><div></div><div>"---------------------------------------------------------------------------&nbsp;</div><div>" PLUGIN SETTINGS</div><div>"---------------------------------------------------------------------------&nbsp;</div><div></div><div>" ------- vim-latex - many latex shortcuts and snippets {</div><div></div><div>" IMPORTANT: win32 users will need to have 'shellslash' set so that latex</div><div>" can be called correctly.</div><div>set shellslash</div><div>set grepprg=grep\ -nH\ $*</div><div>" OPTIONAL: Starting with Vim 7, the filetype of empty .tex files defaults to</div><div>" 'plaintex' instead of 'tex', which results in vim-latex not being loaded.</div><div>" The following changes the default filetype back to 'tex':</div><div>let g:tex_flavor='latex'</div><div></div><div>"}</div><div></div><div></div><div>" --- AutoClose - Inserts matching bracket, paren, brace or quote&nbsp;</div><div>" fixed the arrow key problems caused by AutoClose</div><div>if !has("gui_running")<span style="white-space:pre">	</span></div><div>&nbsp; &nbsp;imap OA &lt;ESC&gt;ki</div><div>&nbsp; &nbsp;imap OB &lt;ESC&gt;ji</div><div>&nbsp; &nbsp;imap OC &lt;ESC&gt;li</div><div>&nbsp; &nbsp;imap OD &lt;ESC&gt;hi</div><div></div><div>&nbsp; &nbsp;nmap OA k</div><div>&nbsp; &nbsp;nmap OB j</div><div>&nbsp; &nbsp;nmap OC l</div><div>&nbsp; &nbsp;nmap OD h</div><div>endif</div><div></div><div></div><div></div><div>" --- Command-T</div><div>let g:CommandTMaxHeight = 15</div><div></div><div>" --- SuperTab</div><div>let g:SuperTabDefaultCompletionType = "context"</div><div></div><div>" --- EasyMotion</div><div>"let g:EasyMotion_leader_key = '&lt;Leader&gt;m' " default is &lt;Leader&gt;w</div><div>hi link EasyMotionTarget ErrorMsg</div><div>hi link EasyMotionShade &nbsp;Comment</div><div></div><div></div><div>" --- TagBar</div><div>" toggle TagBar with F7</div><div>nnoremap &lt;silent&gt; &lt;F7&gt; :TagbarToggle&lt;CR&gt;&nbsp;</div><div>" set focus to TagBar when opening it</div><div>let g:tagbar_autofocus = 1</div><div></div><div>" --- PowerLine</div><div>" let g:Powerline_symbols = 'fancy' " require fontpatcher<br /><br /><br />来源：<a href="https://github.com/g9yuayon/vimrc">https://github.com/g9yuayon/vimrc</a>&nbsp;<br /></div><img src ="http://www.cppblog.com/inkcpp/aggbug/165660.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/inkcpp/" target="_blank">淋雨的感觉</a> 2012-02-15 12:42 <a href="http://www.cppblog.com/inkcpp/archive/2012/02/15/165660.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>