-
Notifications
You must be signed in to change notification settings - Fork 7
/
ch02-01.htm
626 lines (425 loc) · 38.6 KB
/
ch02-01.htm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
<!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;"><iostream></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;"><<</span><span
style=" color:#008000;">"Hello</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">world!"</span><span style=" color:#000000;"><<</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 命令行环境方式是: 开始菜单 --> Qt 5.4.0 --> 5.4 --> MinGW 4.9
(32-bit) --> 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;"><cmath></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;"><iostream></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;"><<</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;"><<</span><span style=" color:#000000;">arect</span><span
style=" color:#000000;">.</span><span style=" color:#000000;">CalcDiagonal</span><span
style=" color:#000000;">()<<</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;"><<</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;"><<</span><span style=" color:#000000;">arect</span><span
style=" color:#000000;">.</span><span style=" color:#000000;">CalcArea</span><span
style=" color:#000000;">()<<</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;">$<</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;">$<</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;">$<</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;">$<</span><span style=" color:#c0c0c0;"> </span></pre>
</div>
是使用编译器生成目标文件 hellorect.o 和 rect.o ,$@ 是上一行冒号左边的目标,$< 是上一行冒号右边第一个依赖文件。
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>