2019-04-21 11:50:48 +08:00

51 lines
3.3 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<h1 id="gcc">gcc</h1>
<p>基于C/C++的编译器</p>
<h2 id="补充说明">补充说明</h2>
<p><strong>gcc命令</strong> 使用GNU推出的基于C/C++的编译器是开放源代码领域应用最广泛的编译器具有功能强大编译代码支持性能优化等特点。现在很多程序员都应用GCC怎样才能更好的应用GCC。目前GCC可以用来编译C/C++、FORTRAN、JAVA、OBJC、ADA等语言的程序可根据需要选择安装支持的语言。</p>
<h3 id="语法">语法</h3>
<pre><code class="language-bash">gcc(选项)(参数)</code></pre>
<h3 id="选项">选项</h3>
<pre><code class="language-bash">-o指定生成的输出文件
-E仅执行编译预处理
-S将C代码转换为汇编代码
-wall显示警告信息
-c仅执行编译操作不进行连接操作。</code></pre>
<h3 id="参数">参数</h3>
<p>C源文件指定C语言源代码文件。</p>
<h3 id="实例">实例</h3>
<p><strong>常用编译命令选项</strong></p>
<p>假设源程序文件名为test.c</p>
<p><strong>无选项编译链接</strong></p>
<pre><code class="language-bash">gcc test.c</code></pre>
<p>将test.c预处理、汇编、编译并链接形成可执行文件。这里未指定输出文件默认输出为a.out。</p>
<p><strong>选项 -o</strong></p>
<pre><code class="language-bash">gcc test.c -o test</code></pre>
<p>将test.c预处理、汇编、编译并链接形成可执行文件test。-o选项用来指定输出文件的文件名。</p>
<p><strong>选项 -E</strong></p>
<pre><code class="language-bash">gcc -E test.c -o test.i</code></pre>
<p>将test.c预处理输出test.i文件。</p>
<p><strong>选项 -S</strong></p>
<pre><code class="language-bash">gcc -S test.i</code></pre>
<p>将预处理输出文件test.i汇编成test.s文件。</p>
<p><strong>选项 -c</strong></p>
<pre><code class="language-bash">gcc -c test.s</code></pre>
<p>将汇编输出文件test.s编译输出test.o文件。</p>
<p><strong>无选项链接</strong></p>
<pre><code class="language-bash">gcc test.o -o test</code></pre>
<p>将编译输出文件test.o链接成最终可执行文件test。</p>
<p><strong>选项 -O</strong></p>
<pre><code class="language-bash">gcc -O1 test.c -o test</code></pre>
<p>使用编译优化级别1编译程序。级别为1~3级别越大优化效果越好但编译时间越长。</p>
<p><strong>多源文件的编译方法</strong></p>
<p>如果有多个源文件,基本上有两种编译方法:</p>
<p>假设有两个源文件为test.c和testfun.c</p>
<p><strong>多个文件一起编译</strong></p>
<pre><code class="language-bash">gcc testfun.c test.c -o test</code></pre>
<p>将testfun.c和test.c分别编译后链接成test可执行文件。</p>
<p><strong>分别编译各个源文件,之后对编译后输出的目标文件链接。</strong></p>
<pre><code class="language-bash">gcc -c testfun.c #将testfun.c编译成testfun.o
gcc -c test.c #将test.c编译成test.o
gcc -o testfun.o test.o -o test #将testfun.o和test.o链接成test</code></pre>
<p>以上两种方法相比较,第一中方法编译时需要所有文件重新编译,而第二种方法可以只重新编译修改的文件,未修改的文件不用重新编译。</p>
<!-- Linux命令行搜索引擎https://jaywcjlove.github.io/linux-command/ -->