64 Star 180 Fork 34

qtguide / qtguide

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
ch02-01.htm 38.59 KB
一键复制 编辑 原始数据 按行查看 历史
qtguide 提交于 2015-03-22 09:32 . add new bottom
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="content-type">
<title>ch02-01</title>
<link href="css/style.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="thumbnailviewer.css" type="text/css">
<script type="text/javascript" src="thumbnailviewer.js">
/***********************************************
* Image Thumbnail Viewer Script- © Dynamic Drive (www.dynamicdrive.com)
* This notice must stay intact for legal use.
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/
</script>
</head>
<body>
<div class="os1"> 2.1 Hello World</div>
<p>本节以经典的 C++ Hello World 程序介绍一下使用 MinGW (Linux 上是 GNU 工具集)环境下编译程序的过程,并示范一下简单 Makefile 的使用。如果对 MinGW 编译环境比较熟悉,可以直接进入下一节, 本节是针对不太熟悉 MinGW 环境的新手讲解的。
<br><br>
本教程以后主要在 MinGW 环境编译 Qt 项目代码,因为国内还是 Windows 用的多,讲 Linux 也没多少人会用。本教程中的代码只要没有特殊说明,都是可以跨平台编译的,在 Linux 系统里主要是文件路径有差异,可执行程序和库的后缀名不一样,其他的没啥区别。
</p>
<div class="os2">2.1.1 简单的 Hello World</div>
<p>一般编写的程序代码文件要放到全英文(可以有数字和划线-_)的路径里面新建工程。这里是简单程序,比如就放在 D:\QtProjects\ch02 , 新建一个文件夹
helloworld。
<br><br>
<font color="red"><b>注意项目和代码文件的全路径里不要包含任何中文字符、特殊字符和空格,这些字符对程序编译不方便,并且会导致开发工具找不到文件。 </b></font>
<br><br>现在进入
D:\QtProjects\ch02\helloworld 文件夹,新建一个 helloworld.cpp ,里面输入如下代码:</p>
<div class="code"><span style=" color:#008000;">//helloworld.cpp</span>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000080;">#include</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">&lt;iostream&gt;</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#808000;">using</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#808000;">namespace</span><span style=" color:#c0c0c0;"> </span>std<span
style=" color:#000000;">;</span></pre>
<pre style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#808000;">int</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">main</span><span
style=" color:#000000;">(</span><span style=" color:#808000;">int</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">argc</span><span style=" color:#000000;">,</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#808000;">char</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">**</span><span
style=" color:#000000;">argv</span><span style=" color:#000000;">)</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000000;">{</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span>cout<span style=" color:#000000;">&lt;&lt;</span><span
style=" color:#008000;">"Hello</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">world!"</span><span style=" color:#000000;">&lt;&lt;</span>endl<span
style=" color:#000000;">;</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">return</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000080;">0</span><span style=" color:#000000;">;</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000000;">}</span></pre>
</div>
<p> 打开 MinGW 命令行环境方式是: 开始菜单 --&gt; Qt 5.4.0 --&gt; 5.4 --&gt; MinGW 4.9
(32-bit) --&gt; Qt 5.4 for Desktop (MinGW 4.9 32 bit), 打开该命令行工具,就能使用 MinGW
和 Qt 库进行编译程序。如果是 Linux 系统按照“1.4.3 Qt开发环境脚本”一节的打开自制命令行即可。编译上面简单程序的命令为: </p>
<div class="cl"> cd /d D:\QtProjects\ch02\helloworld <br>
g++ helloworld.cpp -o helloworld </div>
上面第一句命令是进入 helloworld 文件夹,第二句命令是使用 g++ 编译 helloworld.cpp , -o 代表输出文件名为
helloworld (默认扩展名 .exe)。 执行该目标程序的命令为
<div class="cl"> helloworld </div>
Linux 系统里面执行当前文件夹的可执行程序,其命令为
<div class="cl"> ./helloworld </div>
Linux 系统管理比较严格,默认不从当前文件夹找可执行程序执行, 所以要加上 ./ 表示从当前文件夹找可执行程序运行。<br>
在 MinGW 环境里面的示范过程如下图:<br>
<center><img src="images/ch02/ch02-01-01.jpg" alt="helloworld"> </center>
<br>
这时候进入 D:\QtProjects\ch02\helloworld 文件夹,可以看到目标程序 helloworld.exe ,大小是48 KB,
这是使用动态链接库的程序,体积比较小,如果双击它,会出现提示“没有找到 libgcc_s_dw2-1.dll (GCC
异常处理库),因此这个应用程序未能启动。重新安装应用程序可能会修复此问题”, 这是因为之前 MinGW 命令行配置好了 PATH 环境变量,MinGW
命令行环境有该 dll 文件, 而操作系统的 PATH 环境变量没有该 dll 。另外该 helloworld.exe 还依赖
libstdc++-6.dll (C++标准库), 动态链接的程序就是这样,需要相应的 dll 才能正常执行。可以找到这两个 dll 复制到
helloworld 文件夹里, 或者使用如下静态编译命令:
<div class="cl"> g++ helloworld.cpp -static -o hellostatic </div>
新的静态链接版本程序 hellostatic.exe 是 1427 KB,比较大,双击运行不会报错,打印完字符串后会一闪而过, 普通的 CMD
命令行里可以直接运行该程序显示结果。 MinGW 编译程序的大概过程就是先用 g++ 编译器生成目标文件 *.o , 然后 g++ 自动调用了 ld
工具链接 *.o 和库文件,生成了 .exe 文件。<br>
g++ 默认情况下生成的都是 Release 版可执行程序,
如果希望在可执行程序里加入 Debug 调试信息以供 gdb 调试,那需要在 g++ 编译时加一个 -g 选项,比如
<div class="cl"> g++ helloworld.cpp -g -static -o hellodebug </div>
我们接下来看个稍微 复杂一点的代码,将编译和链接命令分拆开来试试。
<br><br>
<div class="os2">2.1.2 复杂一些的 Hello Rect</div>
<p> 我们在 D:\QtProjects\ch02 文件夹里新建一个 hellorect 文件夹,进入 hellorect 文件夹,新建一个
rect.h,输入代码: </p>
<div class="code"> <span style=" color:#008000;"></span><span style=" color:#008000;">//rect.h</span>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#808000;">class</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#800080;">Rect</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000000;">{</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#808000;">private</span><span style=" color:#000000;">:</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">double</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#800000;">m_dblWidth</span><span
style=" color:#000000;">;</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">double</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#800000;">m_dblHeight</span><span
style=" color:#000000;">;</span></pre>
<pre style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#808000;">public</span><span style=" color:#000000;">:</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800080;">Rect</span><span
style=" color:#000000;">(</span><span style=" color:#808000;">double</span><span
style=" color:#c0c0c0;"> </span>dblWidth<span style=" color:#000000;">,</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">double</span><span
style=" color:#c0c0c0;"> </span>dblHeight<span style=" color:#000000;">);</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">double</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">GetWidth</span><span
style=" color:#000000;">(){</span><span style=" color:#c0c0c0;"> </span><span style=" color:#808000;">return</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#800000;">m_dblWidth</span><span
style=" color:#000000;">;</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">}</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">double</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">GetHeight</span><span
style=" color:#000000;">(){</span><span style=" color:#c0c0c0;"> </span><span style=" color:#808000;">return</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#800000;">m_dblHeight</span><span
style=" color:#000000;">;</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">}</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//Functions</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">double</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">CalcDiagonal</span><span
style=" color:#000000;">();</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">double</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">CalcArea</span><span
style=" color:#000000;">();</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000000;">};</span></pre>
</div>
然后新建一个 rect.cpp,输入代码:
<div class="code"> <span style=" color:#008000;"></span><span style=" color:#008000;">//rect.cpp</span>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000080;">#include</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">&lt;cmath&gt;</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000080;">#include</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">"rect.h"</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#808000;">using</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#808000;">namespace</span><span style=" color:#c0c0c0;"> </span>std<span
style=" color:#000000;">;</span></pre>
<pre style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#800080;">Rect</span><span style=" color:#000000;">::</span><span
style=" color:#000000;">Rect</span><span style=" color:#000000;">(</span><span style=" color:#808000;">double</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">dblWidth</span><span
style=" color:#000000;">,</span><span style=" color:#c0c0c0;"> </span><span style=" color:#808000;">double</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">dblHeight</span><span
style=" color:#000000;">)</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000000;">{</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800000;">m_dblWidth</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span>abs<span
style=" color:#000000;">(</span><span style=" color:#000000;">dblWidth</span><span
style=" color:#000000;">);</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800000;">m_dblHeight</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span>abs<span
style=" color:#000000;">(</span><span style=" color:#000000;">dblHeight</span><span
style=" color:#000000;">);</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000000;">}</span></pre>
<pre style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#808000;">double</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#800080;">Rect</span><span style=" color:#000000;">::</span><span
style=" color:#000000;">CalcDiagonal</span><span style=" color:#000000;">()</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000000;">{</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">return</span><span
style=" color:#c0c0c0;"> </span>sqrt<span style=" color:#000000;">(</span><span
style=" color:#800000;">m_dblWidth</span><span style=" color:#000000;">*</span><span
style=" color:#800000;">m_dblWidth</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">+</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800000;">m_dblHeight</span><span
style=" color:#000000;">*</span><span style=" color:#800000;">m_dblHeight</span><span
style=" color:#000000;">);</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000000;">}</span></pre>
<pre style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#808000;">double</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#800080;">Rect</span><span style=" color:#000000;">::</span><span
style=" color:#000000;">CalcArea</span><span style=" color:#000000;">()</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000000;">{</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">return</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#800000;">m_dblWidth</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">*</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#800000;">m_dblHeight</span><span style=" color:#000000;">;</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000000;">}</span></pre>
</div>
再新建一个 hellorect.cpp,输入代码:
<div class="code"> <span style=" color:#008000;"></span><span style=" color:#008000;">//hellorect.cpp</span>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000080;">#include</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">&lt;iostream&gt;</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000080;">#include</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">"rect.h"</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#808000;">using</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#808000;">namespace</span><span style=" color:#c0c0c0;"> </span>std<span
style=" color:#000000;">;</span></pre>
<pre style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#808000;">int</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">main</span><span
style=" color:#000000;">(</span><span style=" color:#808000;">int</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">argc</span><span style=" color:#000000;">,</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#808000;">char</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">**</span><span
style=" color:#000000;">argv</span><span style=" color:#000000;">)</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000000;">{</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800080;">Rect</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">arect</span><span
style=" color:#000000;">(</span><span style=" color:#000080;">3.0</span><span style=" color:#000000;">,</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000080;">4.0</span><span style=" color:#000000;">);</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span>cout<span style=" color:#000000;">&lt;&lt;</span><span
style=" color:#008000;">"Diagonal</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">Length</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">is</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">"</span><span
style=" color:#000000;">&lt;&lt;</span><span style=" color:#000000;">arect</span><span
style=" color:#000000;">.</span><span style=" color:#000000;">CalcDiagonal</span><span
style=" color:#000000;">()&lt;&lt;</span>endl<span style=" color:#000000;">;</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span>cout<span style=" color:#000000;">&lt;&lt;</span><span
style=" color:#008000;">"Area</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">is</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">"</span><span
style=" color:#000000;">&lt;&lt;</span><span style=" color:#000000;">arect</span><span
style=" color:#000000;">.</span><span style=" color:#000000;">CalcArea</span><span
style=" color:#000000;">()&lt;&lt;</span>endl<span style=" color:#000000;">;</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">return</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000080;">0</span><span style=" color:#000000;">;</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000000;">}</span></pre>
</div>
<p>上述代码定义了一个非常简单的类 Rect,然后计算了对角线长度 CalcDiagonal 和面积 CalcArea, 在 MinGW 命令行进入
hellorect 文件夹,编译两个 cpp 文件的命令如下: </p>
<div class="cl">g++ -c rect.cpp -o rect.o <br>
g++ -c hellorect.cpp -o hellorect.o </div>
g++ -c 的意思是编译生成目标代码文件 *.o,而不进行链接。 链接 *.o 文件和链接库生成可执行程序的命令为:
<div class="cl">g++ rect.o hellorect.o -lm -static -o hellorect </div>
g++ 会自动调用链接器链接 .o 文件和系统里的链接库, -lm 是链接数学库, -static 是生成静态链接的程序,-o
是指定生成的输出文件名。然后执行 hellorect 就可以看到结果:<br>
<center><img src="images/ch02/ch02-01-02.jpg" alt="hellorect"> </center>
<br>
<div class="os2">2.1.3 给 Hello Rect 编写简单的 Makefile</div>
<p> 简单程序可以自己一句句敲 g++ 命令,如果项目复杂起来,代码太多了,自己敲命令编译就很麻烦, 而且一个 .cpp
文件修改后就得重新生成目标文件 *.o,因此实际开发项目时都是借助 make 工具 (MinGW 的是 mingw32-make),编写好
Makefile 之后,只需要在项目文件夹执行一句 make 命令,
其他生成目标文件、链接目标文件和库以及自动根据源代码改动重新生成等,这些事情全交给 make , 而程序员就不用操心构建程序的具体过程。<br>
Makefile 文件可以自己编写,其实绝大多数的集成开发环境都可以根据项目文件自动生成相应的
Makefile,所以实际中很多都是集成开发环境自动完成的。这里示范一个简单的 Makefile, 体会一下生成器 make 是如何工作的。<br>
在上面 hellorect 文件夹里新建一个文件,命名为 Makefile,不要带扩展名。 使用记事本或 Notepad2 工具打开该
Makefile 文件,里面输入如下脚本: </p>
<div class="code"> <span style=" color:#008000;"></span><span style=" color:#008000;">#</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">Makefile</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">for</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">building:</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">hellorect</span>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#800080;">CC</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">=</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">gcc</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#800080;">CXX</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">=</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">g++</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#800080;">LINKER</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">=</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">g++</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#800080;">LFLAGS</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">=</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">-lm</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">-static</span></pre>
<pre style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#800080;">OBJECTS</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">=</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">rect.o</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">hellorect.o</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#800080;">DSTTARGET</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;"> =</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">hellorect</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#008000;"># Default</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">rule</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000080;">all:</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">$(</span><span
style=" color:#800080;">DSTTARGET</span><span style=" color:#008000;">)</span></pre>
<pre style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br></pre>
<pre style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000080;">$(DSTTARGET):</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">$(</span><span style=" color:#800080;">OBJECTS</span><span
style=" color:#008000;">)</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">$(</span><span style=" color:#800080;">LINKER</span><span
style=" color:#008000;">)</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">$(</span><span
style=" color:#800080;">LFLAGS</span><span style=" color:#008000;">)</span><span
style=" color:#c0c0c0;"> </span>-o<span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">$@</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">$(</span><span style=" color:#800080;">OBJECTS</span><span
style=" color:#008000;">)</span></pre>
<pre style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000080;">hellorect.o:</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#800080;">hellorect.cpp</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">$(</span><span style=" color:#800080;">CXX</span><span
style=" color:#008000;">)</span><span style=" color:#c0c0c0;"> </span>-c<span style=" color:#c0c0c0;"> </span>-o<span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">$@</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">$&lt;</span><span style=" color:#c0c0c0;"> </span></pre>
<pre style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000080;">rect.o:</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#800080;">rect.cpp</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">$(</span><span style=" color:#800080;">CXX</span><span
style=" color:#008000;">)</span><span style=" color:#c0c0c0;"> </span>-c<span style=" color:#c0c0c0;"> </span>-o<span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">$@</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">$&lt;</span><span style=" color:#c0c0c0;"> </span></pre>
<pre style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000080;">clean:</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span>rm<span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">$(</span><span
style=" color:#800080;">OBJECTS</span><span style=" color:#008000;">)</span><span
style=" color:#c0c0c0;"> </span>hellorect.exe</pre>
</div>
<p> 这里解释一下上面脚本意思(# 打头的是注释,忽略掉):<br>
中间带有等于号的都是定义变量,引用变量的方式就是 $(变量名) , 脚本里 CC 是 C 语言编译器,CXX 是 C++ 编译器,LINKER
是链接器, LFLAGS 是链接器的参数。OBJECTS 是编译得到的目标文件,DSTTARGET 是可执行的目标程序。<br>
接下来是 Makefile 的生成规则,Makefile 的基本规则是: </p>
<div class="code"> 生成目标: 依赖文件 <br>
[tab字符] 系统命令 </div>
示例的 Makefile 中
<div class="code">all: $(DSTTARGET) </div>
是默认生成规则,依赖文件 $(DSTTARGET) ,它的下一行没有命令。 而如何生成 $(DSTTARGET) 呢,继续往下找 <br>
<div class="code">$(DSTTARGET): $(OBJECTS) </div>
生成 $(DSTTARGET) 需要 $(OBJECTS),有了目标文件之后执行命令<br>
<div class="code">
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">$(</span><span style=" color:#800080;">LINKER</span><span
style=" color:#008000;">)</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">$(</span><span
style=" color:#800080;">LFLAGS</span><span style=" color:#008000;">)</span><span
style=" color:#c0c0c0;"> </span>-o<span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">$@</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">$(</span><span style=" color:#800080;">OBJECTS</span><span
style=" color:#008000;">)</span></pre>
</div>
即调用链接器 $(LINKER),根据链接器参数 $(LFLAGS) 和 $(OBJECTS),生成 $@ 。 $@
就是上一行冒号左边的要生成的目标。注意系统命令 $(LINKER) 之前一定要有制表符 tab 字符, 不能用 4 个空格代替,否则 make
时会出现没有分隔符(separator)的错误。<br>
接下来的四句:
<div class="code">
<pre style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000080;">hellorect.o:</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#800080;">hellorect.cpp</span> </pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">$(</span><span style=" color:#800080;">CXX</span><span
style=" color:#008000;">)</span><span style=" color:#c0c0c0;"> </span>-c<span style=" color:#c0c0c0;"> </span>-o<span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">$@</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">$&lt;</span><span style=" color:#c0c0c0;"> </span></pre>
<pre style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000080;">rect.o:</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#800080;">rect.cpp</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">$(</span><span style=" color:#800080;">CXX</span><span
style=" color:#008000;">)</span><span style=" color:#c0c0c0;"> </span>-c<span style=" color:#c0c0c0;"> </span>-o<span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">$@</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">$&lt;</span><span style=" color:#c0c0c0;"> </span></pre>
</div>
是使用编译器生成目标文件 hellorect.o 和 rect.o ,$@ 是上一行冒号左边的目标,$&lt; 是上一行冒号右边第一个依赖文件。
hellorect.o 和 rect.o就是链接器需要的 $(OBJECTS) 。<br>
最后的两句是清除规则:
<div class="code">
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000080;">clean:</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span>rm<span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">$(</span><span
style=" color:#800080;">OBJECTS</span><span style=" color:#008000;">)</span><span
style=" color:#c0c0c0;"> </span>hellorect.exe</pre>
</div>
rm 是删除命令,如果 Windows 系统里没有 rm 命令,请安装一个 msysgit 工具(
<a href="http://msysgit.github.io/" target="new">http://msysgit.github.io/</a>),
然后系统环境变量里面会有 msysgit 工具路径,里面有 rm 工具。
clean 做的事情就是删除项目生成的 .o 和 .exe 文件。(注:Linux 系统里可执行程序没有 .exe
后缀,需要去掉 .exe 后缀。)<br><br>
编辑好 Makefile 文件之后,那么如何使用 make 工具呢?如果要生成项目,就在项目文件夹 hellorect 里执行:
<div class="cl">mingw32-make</div>
如果要清理项目就执行:
<div class="cl">mingw32-make clean</div>
MinGW 里面是用 mingw32-make 生成程序。(注:Linux 系统里直接用 make 。)
在 MinGW 环境的命令示范如下:<br>
<center><img src="images/ch02/ch02-01-03.jpg" alt="make"></img></center><br>
<br>
<table style="text-align: left; width: 100%;" border="0" cellpadding="2" cellspacing="2">
<tbody>
<tr>
<td style="width: 40%;">
<div style="text-align: center;"> <a href="ch02-00.htm"><img class="pic"
style="width: 32px; height: 32px;" alt="prev" src="images/pics/prev.png"></a></div>
</td>
<td style="width: 20%;"><div style="text-align: center;"><a href="contents.htm"><img class="pic" style="width: 32px; height: 32px;" alt="contents" src="images/pics/contents.png" /></a></div></td>
<td style="width: 40%;">
<div style="text-align: center;"> <a href="ch02-02.htm"><img class="pic"
style="width: 32px; height: 32px;" alt="next" src="images/pics/next.png"></a></div>
</td>
</tr>
</tbody>
</table>
</body>
</html>
HTML
1
https://gitee.com/qtguide/qtguide.git
git@gitee.com:qtguide/qtguide.git
qtguide
qtguide
qtguide
master

搜索帮助