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

<channel>
	<title>bootstrapping | 酷 壳 - CoolShell</title>
	<atom:link href="https://coolshell.cn/tag/bootstrapping/feed" rel="self" type="application/rss+xml" />
	<link>https://coolshell.cn</link>
	<description>享受编程和技术所带来的快乐 - Coding Your Ambition</description>
	<lastBuildDate>Mon, 20 Aug 2012 02:06:21 +0000</lastBuildDate>
	<language>zh-CN</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.2</generator>
	<item>
		<title>GCC 用 C++ 来编译</title>
		<link>https://coolshell.cn/articles/8115.html</link>
					<comments>https://coolshell.cn/articles/8115.html#comments</comments>
		
		<dc:creator><![CDATA[陈皓]]></dc:creator>
		<pubDate>Mon, 20 Aug 2012 00:40:04 +0000</pubDate>
				<category><![CDATA[杂项资源]]></category>
		<category><![CDATA[编程工具]]></category>
		<category><![CDATA[bootstrapping]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Compiler]]></category>
		<category><![CDATA[GNU]]></category>
		<guid isPermaLink="false">http://coolshell.cn/?p=8115</guid>

					<description><![CDATA[<p>GCC在2012年8月15日的时候，merge了一个patch &#8211; Merge from cxx-conversion branch，这意味着，以后...</p>
<p class="read-more"><a class="btn btn-default" href="https://coolshell.cn/articles/8115.html"> Read More<span class="screen-reader-text">  Read More</span></a></p>
The post <a href="https://coolshell.cn/articles/8115.html">GCC 用 C++ 来编译</a> first appeared on <a href="https://coolshell.cn">酷 壳 - CoolShell</a>.]]></description>
										<content:encoded><![CDATA[<p><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-3415450859608158"
     crossorigin="anonymous"></script>GCC在2012年8月15日的时候，merge了一个patch &#8211; <a href="http://gcc.gnu.org/git/?p=gcc.git;a=commitdiff;h=2b15d2ba7eb3a25dfb15a7300f4ee7a141ee8539" target="_blank">Merge from cxx-conversion branch</a>，这意味着，以后在GCC的编译只能用C++的编译器了，也意味着，gcc的实现代码开始转向C++了。</p>
<p>你可能会有两个问题，</p>
<ul>
<li>一个问题是为什么GCC要转成C++的实现？</li>
</ul>
<ul>
<li>没有C++的编译器，我怎么编译C++编译器的代码？这不是“鸡生蛋还是蛋生鸡”的问题么？</li>
</ul>
<p>那，我们来看一看吧。</p>
<h4>为什么要用C++</h4>
<p>在<a href="http://gcc.gnu.org/wiki/cxx-conversion" target="_blank">GNU的C++ Conversion文档</a>中，我们可以在Background中看到这样的描述：</p>
<blockquote><p>Whether we use C or C++, we need to try to ensure that interfaces are easy to understand, that the code is reasonably modular, that the internal documentation corresponds to the code, that it is possible for new developers to write new passes and to fix bugs. Those are the important issues for us to consider. The C++ features which are not present in C &#8212; features which are well documented in many books and many web sites &#8212; are not an important issue.</p></blockquote>
<p>这句话的意思可以理解为，今天GCC在用C语言的实现已经有点hold不住了，因为，开发人员觉得，不管我们用C或C++，都需要努力确保接口是容易理解的，这样我们的代码是想当理性地被模块化的，这样内部文档和代码一致，这样可以更好地组织代码，这样有利于新人了fix-bug。而C++正好可以让他们更好的完成这些东西。</p>
<p>GNU还给出了下面这些理由：</p>
<p><span id="more-8115"></span></p>
<ul>
<li>C++ 是一种标准化的，大众的，流行的语言。</li>
<li>C++ 是C90的超集。</li>
<li>C++作为C的扩展和C在性能上一样好。</li>
<li>C++ 在一些有意义的案例上支持更干净的代码。</li>
<li>C++ 让你更容易去写一个更干净的接口。</li>
<li>C++ 永远不会让你的代码变得更丑。</li>
<li>C++ 不是万灵药，他是C的一个改进。</li>
</ul>
<p>然后，给了一个PDF <a href="http://airs.com/ian/cxx-slides.pdf">http://airs.com/ian/cxx-slides.pdf</a>，这是Google 的<a href="http://airs.com/ian/" target="_blank"> Ian Lance Taylor</a>的的一个PPT，这个文档可以让大家更好地理解我在《<a title="C++的坑真的多吗？" href="https://coolshell.cn/articles/7992.html" target="_blank">C++的坑多吗？</a>》一文中那些观点。<strong>我都不知道我要说多少遍C++的封装，继承和多态比C语言在代码组织上要好得多得多</strong>。大家还是自己看一下代码吧：</p>
<p><strong><span style="color: #800000;">数据结构的操作</span> —— </strong>你写的一定不会有STL好</p>
<p style="padding-left: 30px;"><strong><a href="https://coolshell.cn/wp-content/uploads/2012/08/VEC-vs-vector.jpg"><img decoding="async" loading="lazy" class="size-full wp-image-8119 alignnone" title="VEC vs vector" src="https://coolshell.cn/wp-content/uploads/2012/08/VEC-vs-vector.jpg" alt="" width="542" height="343" srcset="https://coolshell.cn/wp-content/uploads/2012/08/VEC-vs-vector.jpg 542w, https://coolshell.cn/wp-content/uploads/2012/08/VEC-vs-vector-300x190.jpg 300w, https://coolshell.cn/wp-content/uploads/2012/08/VEC-vs-vector-427x270.jpg 427w" sizes="(max-width: 542px) 100vw, 542px" /></a></strong></p>
<p><span style="color: #800000;"><strong>结构套结构还是继承？</strong></span></p>
<p style="padding-left: 30px;"><a href="https://coolshell.cn/wp-content/uploads/2012/08/tree-structure.jpg"><img decoding="async" loading="lazy" class=" wp-image-8118 alignnone" title="tree-structure" src="https://coolshell.cn/wp-content/uploads/2012/08/tree-structure.jpg" alt="" width="629" height="550" srcset="https://coolshell.cn/wp-content/uploads/2012/08/tree-structure.jpg 699w, https://coolshell.cn/wp-content/uploads/2012/08/tree-structure-300x262.jpg 300w" sizes="(max-width: 629px) 100vw, 629px" /></a></p>
<p><span style="color: #800000;"><strong>函数指针还是多态？</strong></span></p>
<div style="padding-left: 30px;"><a href="https://coolshell.cn/wp-content/uploads/2012/08/TARGET-vs-Target.jpg"><img decoding="async" loading="lazy" class=" wp-image-8117 alignnone" title="TARGET vs Target" src="https://coolshell.cn/wp-content/uploads/2012/08/TARGET-vs-Target.jpg" alt="" width="391" height="470" srcset="https://coolshell.cn/wp-content/uploads/2012/08/TARGET-vs-Target.jpg 489w, https://coolshell.cn/wp-content/uploads/2012/08/TARGET-vs-Target-249x300.jpg 249w" sizes="(max-width: 391px) 100vw, 391px" /></a></div>
<p><span style="color: #800000;"><strong>垃圾回收 还是 智能指针？</strong></span></p>
<div style="padding-left: 30px;"><a href="https://coolshell.cn/wp-content/uploads/2012/08/GC-vs-Smart-Pointer.jpg"><img decoding="async" loading="lazy" class="wp-image-8116 alignnone" title="GC vs Smart Pointer" src="https://coolshell.cn/wp-content/uploads/2012/08/GC-vs-Smart-Pointer.jpg" alt="" width="473" height="337" srcset="https://coolshell.cn/wp-content/uploads/2012/08/GC-vs-Smart-Pointer.jpg 676w, https://coolshell.cn/wp-content/uploads/2012/08/GC-vs-Smart-Pointer-300x213.jpg 300w" sizes="(max-width: 473px) 100vw, 473px" /></a></div>
<p><span style="color: #800000;"><strong>Why not C++? </strong></span></p>
<ul>
<li><strong>C++慢吗</strong>？某些特性会慢，但是有时C++更快，你可以只用你喜欢的C++特性。</li>
<li><strong>C++复杂吗？</strong>它只不过是另一种编程语言，他可以让你对程序员维护更简单。</li>
<li><strong>FSF不喜欢C++！</strong>因为FSF（自由软件基金会）这些人不写代码。</li>
</ul>
<p><a href="https://coolshell.cn/wp-content/uploads/2012/08/Why-not-C++.jpg"><img decoding="async" loading="lazy" class="aligncenter  wp-image-8120" title="Why not C++" src="https://coolshell.cn/wp-content/uploads/2012/08/Why-not-C++.jpg" alt="" width="478" height="418" srcset="https://coolshell.cn/wp-content/uploads/2012/08/Why-not-C++.jpg 683w, https://coolshell.cn/wp-content/uploads/2012/08/Why-not-C++-300x262.jpg 300w" sizes="(max-width: 478px) 100vw, 478px" /></a></p>
<div></div>
<h4>Bootstrapping</h4>
<p>最后，我想来介绍一下<a href="http://en.wikipedia.org/wiki/Bootstrapping_%28compilers%29" target="_blank">Bootstrapping</a>。 所谓Bootstrapping，就是用自己这个语言写编译器来编译自己，也就是说如果你要编译gcc，你需要用一个c的编译器来编译之，这个就是bootstrapped process，自举过程。包括 <a title="BASIC" href="http://en.wikipedia.org/wiki/BASIC">BASIC</a>, <a title="Algol" href="http://en.wikipedia.org/wiki/Algol">Algol</a>, <a title="C (programming language)" href="http://en.wikipedia.org/wiki/C_(programming_language)">C</a>, <a title="C++" href="http://en.wikipedia.org/wiki/C%2B%2B">C++</a>, <a title="Pascal programming language" href="http://en.wikipedia.org/wiki/Pascal_programming_language">Pascal</a>, <a title="PL/I" href="http://en.wikipedia.org/wiki/PL/I">PL/I</a>, <a title="Factor programming language" href="http://en.wikipedia.org/wiki/Factor_programming_language">Factor</a>, <a title="Haskell (programming language)" href="http://en.wikipedia.org/wiki/Haskell_(programming_language)">Haskell</a>, <a title="Modula-2" href="http://en.wikipedia.org/wiki/Modula-2">Modula-2</a>, <a title="Oberon programming language" href="http://en.wikipedia.org/wiki/Oberon_programming_language">Oberon</a>, <a title="OCaml" href="http://en.wikipedia.org/wiki/OCaml">OCaml</a>,<a title="Common Lisp" href="http://en.wikipedia.org/wiki/Common_Lisp">Common Lisp</a>, <a title="Scheme (programming language)" href="http://en.wikipedia.org/wiki/Scheme_(programming_language)">Scheme</a>, <a title="Java (programming language)" href="http://en.wikipedia.org/wiki/Java_(programming_language)">Java</a>, <a title="Python (programming language)" href="http://en.wikipedia.org/wiki/Python_(programming_language)">Python</a>, <a title="Scala (programming language)" href="http://en.wikipedia.org/wiki/Scala_(programming_language)">Scala</a> 等语言都这么干。</p>
<p>这样干的好处主要是，自己可以测试自己，编译器的改善和语言的改善相辅相成。</p>
<p>但是，这是一个“鸡生蛋，还是蛋生鸡”的问题，如果你需要用X语言来写一个X语言编译器的语言，你可以这样干：</p>
<ul>
<li>用Y语言来实现X的语言解释器或编译器。 <a title="Niklaus Wirth" href="http://en.wikipedia.org/wiki/Niklaus_Wirth">Niklaus Wirth</a> 说 <a title="Pascal programming language" href="http://en.wikipedia.org/wiki/Pascal_programming_language">Pascal</a> 的第一个编译器是由 <a title="Fortran" href="http://en.wikipedia.org/wiki/Fortran">Fortran</a> 写的。</li>
<li>已存在用Y语言写的X语言的编译器或解释器。<a title="Scheme (programming language)" href="http://en.wikipedia.org/wiki/Scheme_(programming_language)">Scheme</a> 就是这么干的。</li>
<li>已经有一个编译器来编译一个早期版本的X语言，然后就可以用早期版本的X语言来编译新版本的X语言了。<a title="Java (programming language)" href="http://en.wikipedia.org/wiki/Java_(programming_language)">Java</a>，<a title="Haskell (programming language)" href="http://en.wikipedia.org/wiki/Haskell_(programming_language)">Haskell</a>, 和最初版的 <a title="Free Pascal" href="http://en.wikipedia.org/wiki/Free_Pascal">Free Pascal</a> 就是这么干的。</li>
<li>X在某平台上的编译器已经存在，可以使用交叉编译技术来编译另一个平台上X语言，C语言就是这么干的。</li>
<li>用X语言写一个编译器，然后手动编译之（不需要特别优化），（注：手动编译估计就是手动翻译成机器汇编代码），然后再运行这个手动编译的编译器来编译这个编译器的源码，并优化之。<a title="Donald Knuth" href="http://en.wikipedia.org/wiki/Donald_Knuth">Donald Knuth</a> 在他的 <a title="WEB" href="http://en.wikipedia.org/wiki/WEB">WEB</a> <a title="Literate programming" href="http://en.wikipedia.org/wiki/Literate_programming">literate programming</a> 系统里用到了这个方法。</li>
</ul>
<p>（全文完）<!--



<p align="center"><a href= target=_blank><img decoding="async" src=""></a></p>





<p align="center"><img decoding="async" src="https://coolshell.cn/wp-content/uploads/2020/03/coolshell.weixin.jpg"> <img decoding="async" loading="lazy" src="https://coolshell.cn/wp-content/uploads/2020/03/coolshell.mini_.jpg" width="300" height="300"> <br />关注CoolShell微信公众账号和微信小程序</p>

 

--></p>
<div style="margin-top: 15px; font-size: 16px;color: #cc0000;">
<p align="center"><strong>（转载本站文章请注明作者和出处 <a href="https://coolshell.cn/">酷 壳 &#8211; CoolShell</a> ，请勿用于任何商业用途）</strong></p>
</div>

<div class="wp_rp_wrap  wp_rp_vertical_m" id="wp_rp_first"><div class="wp_rp_content"><h3 class="related_post_title">相关文章</h3><ul class="related_post wp_rp"><li ><a href="https://coolshell.cn/articles/7886.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2012/07/muxnt-150x150.jpg" alt="代码执行的效率" width="150" height="150" /></a><a href="https://coolshell.cn/articles/7886.html" class="wp_rp_title">代码执行的效率</a></li><li ><a href="https://coolshell.cn/articles/20845.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2020/03/rust-social-wide-150x150.jpg" alt="Rust语言的编程范式" width="150" height="150" /></a><a href="https://coolshell.cn/articles/20845.html" class="wp_rp_title">Rust语言的编程范式</a></li><li ><a href="https://coolshell.cn/articles/18360.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2018/05/300x262-150x150.jpg" alt="程序员练级攻略（2018)  与我的专栏" width="150" height="150" /></a><a href="https://coolshell.cn/articles/18360.html" class="wp_rp_title">程序员练级攻略（2018)  与我的专栏</a></li><li ><a href="https://coolshell.cn/articles/18024.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2017/07/api-design-300x278-2-150x150.jpg" alt="API设计原则 &#8211; Qt官网的设计实践总结" width="150" height="150" /></a><a href="https://coolshell.cn/articles/18024.html" class="wp_rp_title">API设计原则 &#8211; Qt官网的设计实践总结</a></li><li ><a href="https://coolshell.cn/articles/12052.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/plugins/wordpress-23-related-posts-plugin/static/thumbs/29.jpg" alt="Leetcode 编程训练" width="150" height="150" /></a><a href="https://coolshell.cn/articles/12052.html" class="wp_rp_title">Leetcode 编程训练</a></li><li ><a href="https://coolshell.cn/articles/12012.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2014/10/edsm-150x150.gif" alt="State Threads 回调终结者" width="150" height="150" /></a><a href="https://coolshell.cn/articles/12012.html" class="wp_rp_title">State Threads 回调终结者</a></li></ul></div></div>The post <a href="https://coolshell.cn/articles/8115.html">GCC 用 C++ 来编译</a> first appeared on <a href="https://coolshell.cn">酷 壳 - CoolShell</a>.]]></content:encoded>
					
					<wfw:commentRss>https://coolshell.cn/articles/8115.html/feed</wfw:commentRss>
			<slash:comments>41</slash:comments>
		
		
			</item>
	</channel>
</rss>
