2024-08-09 04:58:16 +02:00
|
|
|
|
import{_ as s,c as a,o as i,a6 as n}from"./chunks/framework.CZRoMP2i.js";const l="/c/assets/1.D4a42fT1.jpg",e="/c/assets/2.D2wnxl5F.png",t="/c/assets/3.Dq625oo9.jpg",p="/c/assets/4.uqu1DEK4.png",r="/c/assets/5.C6Q0Q8kf.jpg",c="/c/assets/6.B4Srsb-0.png",h="/c/assets/7._D_c8Lrn.png",d="/c/assets/8.DlIyFGDg.png",o="/c/assets/9.7zkUmpQw.png",k="/c/assets/10.h1ugtjyX.png",u="/c/assets/11.D5PWTabw.png",g="/c/assets/12.Bvs6QYjl.jpg",b="/c/assets/9.7zkUmpQw.png",m="/c/assets/14.CsR5exrN.png",E="/c/assets/15.BRG9GQdT.png",C="/c/assets/16.C2sciGZn.png",y="/c/assets/17.DdyXXyxJ.png",F="/c/assets/18.DIeb_KXW.png",_="/c/assets/19.C-kvMz2p.png",v="/c/assets/20.CQqFxmDa.png",B="/c/assets/21.CjMWyIiX.png",f="/c/assets/22.DFQhcDgs.png",A="/c/assets/23.Cco1vsW8.png",x="/c/assets/24.D60Zf6_d.png",D="/c/assets/25.D2-G6nV2.png",q="/c/assets/26.DpzkVk5V.png",S="/c/assets/27.BXk95GIP.png",w="/c/assets/28.CbWTB08W.png",L="/c/assets/29.twO9TCIg.png",P="/c/assets/30.CKOjeIvS.png",T="/c/assets/31.DKmR0CxE.png",U="/c/assets/32.Cudf77_s.gif",I="/c/assets/33.BBUUlhRC.png",R="/c/assets/34.iLABkB7O.png",N="/c/assets/35.BQuYWOU9.png",W="/c/assets/36.CslpH6j0.png",M="/c/assets/37.BAyvZdKq.png",O="/c/assets/38.C3oC1iUd.png",G="/c/assets/39.Ds-jsEZn.png",X="/c/assets/40.Cd37gMYs.png",V="/c/assets/41.CAw2z3gc.png",H="/c/assets/42.DdPThnr-.png",$="/c/assets/43.q_rvlDnY.png",K="/c/assets/44.JRHg9RZA.png",J="/c/assets/45.Baq-Hb06.png",j="/c/assets/46.c1ht53mu.png",Q="/c/assets/47.CAZmH5Py.png",Y="/c/assets/48.DWeCYBe9.png",z="/c/assets/49.2XM8pYni.png",Z="/c/assets/50.CN0t-fuo.png",ss="/c/assets/51.AcDh3LXQ.png",as="/c/assets/52.CUkDFAc6.png",is="/c/assets/53.picNTNaJ.png",ns="/c/assets/54.DKX92RgJ.png",ls="/c/assets/55.Bxr7QKI3.png",es="/c/assets/56.DuFWsOgW.png",ts="/c/assets/57.Da9qUcfr.png",ps="/c/assets/58.7_gEABSX.gif",rs="/c/assets/59.C2KQYoVC.gif",cs="/c/assets/60.RTUP8Vtx.png",hs="/c/assets/61.Dq21DxwL.png",ds="/c/assets/62.QmpUaNxU.png",os="/c/assets/63.DP0C29gZ.png",ks="/c/assets/64.DwzHokd4.png",us="/c/assets/65.Bi6K26yA.png",gs="/c/assets/66.Bw8DcEQ_.png",bs="/c/assets/67.DPc8Z9rS.png",ms="/c/assets/68.S84xHMTl.png",Es="/c/assets/69.iw3JQUkn.png",Cs="/c/assets/70.ILeJt-HK.png",ys="/c/assets/71.CHgm7khh.png",Fs="/c/assets/72.N1nqJaEg.png",_s="/c/assets/73.DHsnibMi.png",vs="/c/assets/74.BQr6Q0uV.png",Bs="/c/assets/75.BVemi2Lh.png",fs="/c/assets/76.BHR69UXx.png",As="/c/assets/77.CPSU5SJI.png",xs="/c/assets/78.CaQLoDlw.png",Ds="/c/assets/79.aRGy4XbL.png",qs="/c/assets/80.kNO9fmPR.png",Ss="/c/assets/81.D4EkoC4o.png",ws="/c/assets/82.V23Lo9e_.png",Ls="/c/assets/83.Du9LVeMB.png",Ps="/c/assets/84.DdFg75U9.png",Ts="/c/assets/85.C6bz6KtT.png",Us="/c/assets/86.CB6LAhB5.png",Is="/c/assets/87.Bip8bkua.png",Rs="/c/assets/88.DRNHRduW.png",Ns="/c/assets/89.Cth4qqEn.png",Ws="/c/assets/90.DWGWZ64P.png",Ms="/c/assets/91.3hDIDdvx.png",Os="/c/assets/92.Bw_dQ4ap.png",Gs="/c/assets/93.Cn_ubTyA.png",Xs="/c/assets/94.C84EBqwB.png",Vs="/c/assets/95.dZfJY4eR.png",Hs="/c/assets/96.B0zm0Z79.png",$s="/c/assets/97.CaTcEy1Q.png",Ks="/c/assets/98.iEc8mo9g.png",Js="/c/assets/99.Co2KxM6t.png",js="/c/assets/100.CaUm8Qhc.png",Qs="/c/assets/101.kOkqIKG6.png",Ys="/c/assets/102.CyeASH-c.png",zs="/c/assets/103.CFdk60La.png",Zs="/c/assets/104.Bhl-0uK_.png",sa="/c/assets/105.Dni9KbXV.png",aa="/c/assets/106.BXx10MCV.png",ia="/c/assets/107.CXcIKX1T.png",na="/c/assets/108.CQ3_QCQT.png",la="/c/assets/109.CQoDRJM5.gif",ea="/c/assets/110.CPfwZvD9.gif",ta="/c/assets/111.bxEomEjU.gif",pa="/c/assets/112.C_onhf2k.png",ra="/c/assets/113.Budj3s38.gif",ca="/c/assets/114.CH8koQdu.gif",ha="/c/assets/115.kUJhxMtg.png",da="/c/assets/116.BJeJJW4_.png",oa="/c/assets/117.BCuK6eJL.gif",ka="/c/assets/118.rKC-z4z-.gif",ua="/c/assets/119.BJE3xxuT.gif",ga="/c/assets/120.DJOq__O0.gif",ba="/c/assets/121.BDt_0fRR.png",ma="/c/assets/122.Bo5OawW8.png",Ea="/c/assets/123.sUWUfl5e.png",Ca="/c/assets/124.BK4jpnB8.png",ya="/c/assets/125.EF8vdbSq.png",Fa="/c/assets/126.DjxdA_Co.png",_a="/c/assets/127.sp68q1hh.gif",va="/c/assets/128.DhMRyxNN.gif",Ba="/c/assets/129.CdcLZM0R.gif",fa="/c/assets/130.D-5AsTTr.png",Aa="/c/assets/1
|
2024-07-11 10:07:44 +02:00
|
|
|
|
<span class="line"><span>1 毫秒 = 1000 微妙,即 1 ms = 1000 us 。</span></span>
|
|
|
|
|
<span class="line"><span>1 微妙 = 1000 纳秒,即 1 us = 1000 ns。</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><ul><li>按照上图,将计算机世界的时间和人类世界的时间进行对比,即:</li></ul><div class="language-txt vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">txt</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span>如果 CPU 的时钟周期按照 1 秒计算,</span></span>
|
|
|
|
|
<span class="line"><span>那么,内存访问就需要 6 分钟;</span></span>
|
|
|
|
|
<span class="line"><span>那么,固态硬盘就需要 2-6 天;</span></span>
|
|
|
|
|
<span class="line"><span>那么,传统硬盘就需要 1-12 个月;</span></span>
|
|
|
|
|
<span class="line"><span>那么,网络访问就需要 4 年以上。</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><ul><li>所以,对于 CPU 来说,这个世界真的是太慢了!!!</li><li>其实,中国古代中的文人,通常以<code>蜉蝣</code>来表示时间的短暂(和其他生物的寿命比),也是类似的道理,即:</li></ul><div class="language-txt vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">txt</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span>鹤寿千岁,以极其游,蜉蝣朝生而暮死,尽其乐,盖其旦暮为期,远不过三日尔。</span></span>
|
|
|
|
|
<span class="line"><span> --- 出自 西汉淮南王刘安《淮南子》</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><div class="language-txt vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">txt</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span>寄蜉蝣于天地,渺沧海之一粟。 哀吾生之须臾,羡长江之无穷。 </span></span>
|
|
|
|
|
<span class="line"><span>挟飞仙以遨游,抱明月而长终。 知不可乎骤得,托遗响于悲风。</span></span>
|
2024-08-06 09:58:07 +02:00
|
|
|
|
<span class="line"><span> --- 出自 苏轼《赤壁赋》</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><p>对于<code>蜉蝣</code>来说,从早到晚就是一生;而对于我们<code>人类</code>而言,却仅仅只是一天。</p></div><ul><li>存储器的层次结构(CPU 中也有存储器,即:寄存器、高速缓存 L1、L2 和 L3),如下所示:</li></ul><p><img src="`+h+'" alt="img"></p><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><p>上图以层次化的方式,展示了价格信息,揭示了一个真理,即:天下没有免费的午餐(千万不要相信别人会有发财的机会和你共享,人都是自私的,你算个啥?为什么要找你?)</p><ul><li>① 存储器越往上速度越快,但是价格越来越贵, 越往下速度越慢,但是价格越来越便宜。</li><li>② 正是由于计算机各个部件的速度不同,容量不同,价格不同,导致了计算机系统/编程中的各种问题以及相应的解决方案</li></ul></div><h2 id="_1-4-计算机软件" tabindex="-1">1.4 计算机软件 <a class="header-anchor" href="#_1-4-计算机软件" aria-label="Permalink to "1.4 计算机软件""></a></h2><h3 id="_1-4-1-操作系统的来源" tabindex="-1">1.4.1 操作系统的来源 <a class="header-anchor" href="#_1-4-1-操作系统的来源" aria-label="Permalink to "1.4.1 操作系统的来源""></a></h3><ul><li>在上古时期,硬件资源不够丰富,计算机设计的也非常简陋。那个时候,很多应用程序都是直接跑在硬件上的,即:一个计算机只能跑一个应用程序。</li></ul><p><img src="'+d+'" alt=""></p><ul><li>随着技术的发展,硬件越来越丰富,功能也越来越强大,性能也越来越好。这种情况下,如果一台计算机只能跑一个程序,实在是太浪费了。而且,底层硬件不断丰富,应用程序需要对接的硬件也将越来越多,如果每个应用程序都这么干,不显示工作很重复吗?于是,操作系统应运而生了。</li></ul><p><img src="'+o+'" alt=""></p><ul><li>操作系统的功能: <ul><li>硬件驱动。</li><li>进程管理。</li><li>内存管理。</li><li>网络管理。</li><li>安全管理。</li><li>文件管理。</li></ul></li></ul><ul><li>那么,操作系统的作用,就是这样的,即: <ul><li>对下,管理计算机的硬件资源。</li><li>对上,提供使用计算机资源的操作方式,有: <ul><li><code>系统调用</code>:是一套已经写好的代码接口,应用程序通过调用这些接口来请求操作系统执行特定的硬件操作。它们直接与硬件交互,提供底层功能支持,如:文件操作、进程管理、内存管理等。<code>开发者</code>通过系统调用可以实现对底层资源的直接控制,确保程序能够高效、安全地运行。</li><li><code>终端命令</code>:是一种文本命令接口,通过命令行输入各种指令来控制操作系统和软件的行为。终端命令可以执行文件操作、系统配置、网络管理等各种任务。主要针对<code>开发人员</code>和<code>高级用户</code>,他们通过命令行可以快速、精确地完成各种操作,提高工作效率。</li><li><code>图形用户界面</code>(GUI)是通过图形元素(如:窗口、图标、按钮等)与用户进行交互的界面。供直观、易用的操作方式,使用户能够通过鼠标点击、拖拽等简单操作完成复杂任务。主要面向<code>普通用户</code>,降低了计算机操作的门槛,提高了用户体验和工作效率。</li></ul></li></ul></li></ul><p><img src="'+k+'" alt=""></p><h3 id="_1-4-2-用户态和内核态" tabindex="-1">1.4.2 用<EFBFBD>
|
2024-07-11 10:07:44 +02:00
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">import</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> java.nio.file.Files;</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">import</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> java.nio.file.Path;</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">import</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> java.nio.file.Paths;</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">import</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> java.util.List;</span></span>
|
|
|
|
|
<span class="line"></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">public</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> class</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> Demo</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> public</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> static</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> void</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> writeFile</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(String </span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;">filePath</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, String </span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;">content</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">) {</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> Path path </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> Paths.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">get</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(filePath);</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> try</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> Files.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">write</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(path, content.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">getBytes</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">());</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> } </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">catch</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> (IOException </span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;">e</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">) {</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> e.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">printStackTrace</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">();</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> public</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> static</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> void</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> main</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">String</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">[] </span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;">args</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">){</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> int</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> a </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 10</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">; </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">// 用户态</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> int</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> b </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 20</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">; </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">// 用户态</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> int</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> c </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> a </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">+</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> b; </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">// 用户态</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> string filePath </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> "c:/demo.txt"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">; </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">// 用户态</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> string txt </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> a </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">+</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> b </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">+</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> c; </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">// 用户态</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> </span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> writeFile</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(filePath, a); </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">// 从用户态切换到内核态完成文件写入</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> </span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> System.out.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">println</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(a); </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">// 从内核态切换回用户态</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> System.out.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">println</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(b); </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">// 用户态</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> System.out.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">println</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(c); </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">// 用户态</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br></div></div><h3 id="_1-4-3-isa、abi-和-api" tabindex="-1">1.4.3 ISA、ABI 和 API <a class="header-anchor" href="#_1-4-3-isa、abi-和-api" aria-label="Permalink to "1.4.3 ISA、ABI 和 API""></a></h3><ul><li>ISA 、ABI 和 API 的参考模型如下:</li></ul><p><img src="`+g+'" alt=""></p><ul><li>在底层,硬件模型以指令集架构 (ISA) 表示,该架构定义了处理器、寄存器、存储器和中断管理的指令集。ISA 是硬件和软件之间的接口,对于操作系统 (OS) 开发人员 (System ISA) 和直接管理底层硬件的应用程序 (User ISA) 的开发人员来说非常重要。</li></ul><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><ul><li>① ISA 是计算机体系结构中定义的一组指令,它规定了处理器能够执行的操作。ISA 包括指令的编码、寄存器的使用、内存访问模式等。不同的处理器可能有不同的 ISA,例如:x86、ARM、MIPS 等。</li><li>② 在设计一个新的操作系统时,开发者需要确保操作系统能够支持特定的 ISA ,以便在特定的硬件上运行。例如:如果操作系统旨在运行在 ARM 架构的处理器上,那么它必须能够理解和执行 ARM ISA 定义的指令集。</li></ul></div><ul><li>应用程序二进制接口 (ABI) 将<code>操作系统层</code>与由操作系统管理的<code>应用程序</code>和<code>库</code>分开。ABI 涵盖了低级数据类型、对齐方式和调用约定等详细信息,并定义了可执行程序的格式。系统调用在此级别定义。此接口允许应用程序和库在实现相同 ABI 的操作系统之间移植。</li></ul><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><ul><li>① ABI 是指在二进制级别上,应用程序与操作系统、库或应用程序的不同部分之间的接口。它定义了数据类型的大小、布局、对齐方式,以及函数调用的约定(如参数如何传递、返回值如何处理等)。ABI 确保了编译后的二进制文件能够在特定的操作系统和硬件平台上正确地运行。</li><li>② 在 windows 上的应用程序的运行格式是:<code>PE</code>(portable executable)格式、<code>.dll</code> (dynamic link library)格式和 <code>.lib</code> 格式;而在 Linux 上的应用程序的运行格式是:<code>ELF</code>(executable and linking format)格式、<code>.so</code> (shared object)格式和 <code>.a</code> 格式。</li><li>③ 在 Linux 中可以通过 <code>file /bin/ls</code> 命令查看指定可执行应用程序的 ABI 格式;从而也可以论证,在 Windows 上可以运行的程序,在 Linux 上运行不了。</li><li>④ 当开发者在 Linux 系统上<EFBFBD><EFBFBD>
|
|
|
|
|
<span class="line"></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">int</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> main</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">() {</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // 定义主函数</span></span>
|
|
|
|
|
<span class="line"></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"你好,世界!!!"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // 输出所指定的一行信息</span></span>
|
|
|
|
|
<span class="line"></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // 函数执行完毕时返回函数值0</span></span>
|
2024-08-01 07:49:31 +02:00
|
|
|
|
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><ul><li>编程语言就是用于控制计算机,让其完成我们需要的功能。而我们学习编程语言,其实就是学习这些文字和符号编写的规则。</li><li>因为 CPU 只能识别二进制的指令,而我们<code>编写</code>的程序叫做<code>源代码</code>,是人类能看懂;但是,计算机却不能识别。那么,我们就需要让计算机能识别我们编写的源程序,就需要将我们编写的源代码交给编译器程序,其会帮助我们将所编写的源代码转换为计算机能够识别的二进制指令。</li></ul><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><p>编译器就是运行在操作系统之上的程序,其作用就是用来将程序员编写的源代码转换为计算机能够识别的二进制指令。</p></div><ul><li>如果我们用 Java 语言编写了程序(源代码),那么编写的程序也是不能直接运行的,需要通过 Java 语言的编译器将 Java 程序编译为计算机能够识别的二进制指令。</li><li>如果我们用 Python 语言编写了程序(源代码),那么编写的程序也是不能直接运行的,需要通过 Python 语言的编译器将 Python 程序编译为计算机能够识别的二进制指令。</li><li>……</li><li>总而言之,无论我们学习任何一门编程语言,想要将程序运行起来,都必须做如下的两件事情: <ul><li>① 学习该语言的文字和符号编写的规则,即:<code>语法规则</code>。</li><li>② 需要在操作系统上安装对应编程语言的<code>编译器</code>程序,将源程序编译为计算机能够识别的二进制指令。</li></ul></li></ul><h2 id="_2-3-计算机语言简史" tabindex="-1">2.3 计算机语言简史 <a class="header-anchor" href="#_2-3-计算机语言简史" aria-label="Permalink to "2.3 计算机语言简史""></a></h2><h3 id="_2-3-1-机器语言-相当于人类的石器时代" tabindex="-1">2.3.1 机器语言(相当于人类的石器时代) <a class="header-anchor" href="#_2-3-1-机器语言-相当于人类的石器时代" aria-label="Permalink to "2.3.1 机器语言(相当于人类的石器时代)""></a></h3><ul><li>1946 年 2 月 14 日,世界上第一台计算机 <code>ENIAC</code> 诞生,使用的是最原始的<code>穿透卡片</code>。</li></ul><p><img src="`+C+`" alt=""></p><ul><li>这种卡片使用的是用<code>二进制代码</code>表示的语言,和人类语言差别极大,这种语言就称为<code>机器语言</code>,如:</li></ul><div class="language-txt vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">txt</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span>0000,0000,000000010000 代表 LOAD A, 16</span></span>
|
2024-07-11 10:07:44 +02:00
|
|
|
|
<span class="line"><span>0000,0001,000000000001 代表 LOAD B, 1</span></span>
|
2024-08-09 04:58:16 +02:00
|
|
|
|
<span class="line"><span>0001,0001,000000010000 代表 STORE B, 16</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><ul><li>这种语言本质上是计算机能识别的<code>唯一语言</code>,人类很难理解;换言之,当时的程序员 99.9% 都是异类!!!</li></ul><div class="important custom-block github-alert"><p class="custom-block-title">IMPORTANT</p><p></p><p>不同类型(CPU 架构,如:x86_64、arm 等)的处理器有不同的机器语言指令集,指令集架构(ISA)决定了机器语言的具体形式;换言之,机器语言与特定硬件架构紧密相关,机器语言程序几乎没有可移植性。</p></div><h3 id="_2-3-2-汇编语言-相当于人类的青铜-铁器时代" tabindex="-1">2.3.2 汇编语言(相当于人类的青铜&铁器时代) <a class="header-anchor" href="#_2-3-2-汇编语言-相当于人类的青铜-铁器时代" aria-label="Permalink to "2.3.2 汇编语言(相当于人类的青铜&铁器时代)""></a></h3><ul><li><code>汇编语言</code>使用<code>助记符</code>(如:MOV、ADD、SUB)代替二进制操作码,使程序更易于人类编写和理解;因此,<code>汇编语言</code>也被称为<code>符号语言</code>。</li></ul><p><img src="`+y+'" alt=""></p><ul><li>汇编语言的<code>优点</code>是能编写<code>高效率</code>的程序;但是,<code>缺点</code>和机器语言没什么不同,汇编语言同样<code>依赖于具体的计算机架构(面向机器)</code>,程序不具备跨平台的可移植性。</li></ul><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><p>汇编语言,目前仍然应用于工业电子编程领域、软件的加密解密、计算机病毒分析等。</p></div><h3 id="_2-3-3-高级语言-相当于人类的信息时代" tabindex="-1">2.3.3 高级语言(相当于人类的信息时代) <a class="header-anchor" href="#_2-3-3-高级语言-相当于人类的信息时代" aria-label="Permalink to "2.3.3 高级语言(相当于人类的信息时代)""></a></h3><ul><li><code>高级语言</code>是一种<code>接近于人们使用习惯</code>的程序设计语言。<code>它允许程序员使用接近日常英语的指令来编写程序</code>,程序中的符号和算式也和<code>日常使用的数学公式</code>差不多,接近于自然语言和数学语言,容易被人们掌握。</li></ul><p><img src="'+F+'" alt=""></p><ul><li>高级语言<code>独立于计算机硬件</code>,有一定的通用性;计算机不能直接识别和执行用高级语言编写的程序,需要使用<code>编译器</code>或<code>解释器</code>转换为机器语言,才能被计算机识别和执行。</li></ul><p><img src="'+_+'" alt=""></p><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><p>普遍使用的高级编程语言,有:C、C++、Java、Python、C#、JavaScript、Go、SQL 等。</p></div><h3 id="_2-3-4-总结" tabindex="-1">2.3.4 总结 <a class="header-anchor" href="#_2-3-4-总结" aria-label="Permalink to "2.3.4 总结""></a></h3><ul><li>编写语言的对比,如下所示:</li></ul><table tabindex="0"><thead><tr><th style="text-align:left;">类别</th><th style="text-align:left;">特征</th><th style="text-align:left;">优点</th><th style="text-align:left;">缺点</th><th style="text-align:left;">示例</th></tr></thead><tbody><tr><td style="text-align:left;"><strong>机器语言</strong></td><td style="text-align:left;">直接由计算机执行的二进制代码</td><td style="text-align:left;">执行速度快</td><td style="text-align:left;">编写困难,可读性差,与具体硬件强绑定</td><td style="text-align:left;">二进制代码</td></tr><tr><td style="text-align:left;"><strong>汇编语言</strong></td><td style="text-align:left;">用助记符代替二进制代码<EFBFBD><EFBFBD>
|
2024-07-11 10:07:44 +02:00
|
|
|
|
<span class="line"></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">int</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> main</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(){</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"Hello World"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
2024-08-09 04:52:37 +02:00
|
|
|
|
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p><img src="`+la+'" alt=""></p><ul><li>③ 通过 <code>gcc</code> 命令编译该文件:</li></ul><div class="language-shell vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">gcc</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> HelloWorld.c</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -o</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> HelloWorld.exe</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><p><img src="'+ea+'" alt=""></p><ul><li>④ 执行:</li></ul><div class="language-shell vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">./HelloWorld.exe</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><p><img src="'+ta+'" alt=""></p><h3 id="_4-3-2-vs-code-版" tabindex="-1">4.3.2 VS Code 版 <a class="header-anchor" href="#_4-3-2-vs-code-版" aria-label="Permalink to "4.3.2 VS Code 版""></a></h3><ul><li>① 新建一个<code>空</code>文件夹(目录),用于存放代码:</li></ul><p><img src="'+pa+'" alt=""></p><ul><li>② 通过 <code>vscode</code> 打开该目录:</li></ul><p><img src="'+ra+'" alt=""></p><ul><li>③ 在 <code>vscode</code> 中新建 <code>HelloWorld.c</code> 文件:</li></ul><p><img src="'+ca+'" alt=""></p><ul><li>④ 设置 VSCode 中 C/C++ 的代码格式为行尾风格(可选):</li></ul><p><img src="'+ha+'" alt=""></p><p><img src="'+da+`" alt=""></p><ul><li>⑤ 编写如下的代码,并保存:</li></ul><div class="language-c vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">#include</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> <stdio.h></span></span>
|
2024-07-11 10:07:44 +02:00
|
|
|
|
<span class="line"></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">int</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> main</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(){</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"Hello World"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
2024-08-01 07:49:31 +02:00
|
|
|
|
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p><img src="`+oa+'" alt=""></p><ul><li>⑥ 通过 <code>gcc</code> 命令编译该文件:</li></ul><div class="language-shell vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">gcc</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> HelloWorld.c</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -o</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> HelloWorld.exe</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><p><img src="'+ka+'" alt=""></p><ul><li>⑦ 执行:</li></ul><div class="language-shell vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">./HelloWorld.exe</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><p><img src="'+ua+'" alt=""></p><ul><li>⑧ 安装 Code Runner 插件(步骤略),实现右键直接编译执行(可选):</li></ul><p><img src="'+ga+'" alt=""></p><h3 id="_4-3-3-vs-版" tabindex="-1">4.3.3 VS 版 <a class="header-anchor" href="#_4-3-3-vs-版" aria-label="Permalink to "4.3.3 VS 版""></a></h3><ul><li>① 新建空项目:</li></ul><p><img src="'+ba+'" alt=""></p><p><img src="'+ma+'" alt=""></p><p><img src="'+Ea+'" alt=""></p><p><img src="'+Ca+'" alt=""></p><ul><li>② 打开<code>解决方案资源管理器</code>:</li></ul><p><img src="'+ya+'" alt=""></p><p><img src="'+Fa+'" alt=""></p><ul><li>③ 新建 <code>HelloWorld.c</code> 文件:</li></ul><p><img src="'+_a+`" alt=""></p><ul><li>④ 编写如下代码,并保存:</li></ul><div class="language-c vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">#include</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> <stdio.h></span></span>
|
2024-07-11 10:07:44 +02:00
|
|
|
|
<span class="line"></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">int</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> main</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(){</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"Hello World"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
2024-08-09 04:52:37 +02:00
|
|
|
|
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p><img src="`+va+'" alt=""></p><ul><li>⑤ 编译和执行:</li></ul><p><img src="'+Ba+'" alt=""></p><h3 id="_4-3-4-clion" tabindex="-1">4.3.4 CLion <a class="header-anchor" href="#_4-3-4-clion" aria-label="Permalink to "4.3.4 CLion""></a></h3><ul><li>① 新建空项目:</li></ul><p><img src="'+fa+'" alt=""></p><p><img src="'+Aa+'" alt=""></p><p><img src="'+xa+`" alt=""></p><ul><li>② 编写如下代码,并保存:</li></ul><div class="language-c vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">#include</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> <stdio.h></span></span>
|
2024-07-11 10:07:44 +02:00
|
|
|
|
<span class="line"></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">int</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> main</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(){</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"Hello World"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
2024-08-01 07:49:31 +02:00
|
|
|
|
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p><img src="`+Da+'" alt=""></p><ul><li>③ 编译和运行:</li></ul><p><img src="'+qa+'" alt=""></p><ul><li>④ 默认情况下,一个项目只能有一个 c 源文件包含 main 函数,但是 CLion 可以有多个,如下:</li></ul><p><img src="'+Sa+'" alt=""></p><ul><li>⑤ 如果之后,有中文乱码问题,那么请做如下步骤:</li></ul><p><img src="'+wa+'" alt=""></p><p><img src="'+La+`" alt=""></p><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><p>内容如下所示:</p><div class="language-txt vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">txt</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span>-Dfile.encoding=UTF-8</span></span>
|
2024-07-11 10:07:44 +02:00
|
|
|
|
<span class="line"><span>-Dconsole.encoding=UTF-8</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div></div><h1 id="第五章-注释" tabindex="-1">第五章:注释 <a class="header-anchor" href="#第五章-注释" aria-label="Permalink to "第五章:注释""></a></h1><h2 id="_5-1-概述" tabindex="-1">5.1 概述 <a class="header-anchor" href="#_5-1-概述" aria-label="Permalink to "5.1 概述""></a></h2><ul><li>编程语言中,<code>注释</code>是一种<code>特殊</code>的文本,它不会被编译器执行,而仅用于代码的解释和文档说明。</li></ul><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><ul><li>① 注释是一个程序员必须有具有的良好编程习惯。</li><li>② 在实际开发中,程序员可以将自己的思路通过<code>注释</code>整理出来,然后再用<code>代码</code>去实现。</li></ul></div><h2 id="_5-2-单行注释" tabindex="-1">5.2 单行注释 <a class="header-anchor" href="#_5-2-单行注释" aria-label="Permalink to "5.2 单行注释""></a></h2><ul><li>C 语言中的单行注释的格式,如下所示:</li></ul><div class="language-c vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">// 单行注释</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><p>在 CLion 中的快捷键是 <code>Ctrl + /</code> 。</p></div><ul><li>示例:</li></ul><div class="language-c vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">#include</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> <stdio.h></span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // 这是编译预处理指令</span></span>
|
|
|
|
|
<span class="line"></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">int</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> main</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">() {</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // 定义主函数</span></span>
|
|
|
|
|
<span class="line"></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"你好,世界!!!"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // 输出所指定的一行信息</span></span>
|
|
|
|
|
<span class="line"></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // 函数执行完毕时返回函数值0</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><h2 id="_5-3-多行注释" tabindex="-1">5.3 多行注释 <a class="header-anchor" href="#_5-3-多行注释" aria-label="Permalink to "5.3 多行注释""></a></h2><ul><li>C 语言中的多行注释的格式,如下所示:</li></ul><div class="language-c vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">/*</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> 这是第一行注释</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> 这是第二行注释</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> 这是第三行注释</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">*/</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><ul><li>① 多行注释不能嵌套使用!!!</li><li>② 在 CLion 中的快捷键是 <code>Ctrl + Alt + /</code> 。</li></ul></div><ul><li>示例:</li></ul><div class="language-c vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">#include</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> <stdio.h></span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> </span></span>
|
|
|
|
|
<span class="line"></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">int</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> main</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">() { </span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> </span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> /*</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> printf(1);</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> printf(2);</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> */</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"你好,世界!!!"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">); </span></span>
|
|
|
|
|
<span class="line"></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">; </span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><h1 id="第六章-helloworld-的规范-⭐" tabindex="-1">第六章:HelloWorld 的规范(⭐) <a class="header-anchor" href="#第六章-helloworld-的规范-⭐" aria-label="Permalink to "第六章:HelloWorld 的规范(⭐)""></a></h1><h2 id="_6-1-规范的代码风格" tabindex="-1">6.1 规范的代码风格 <a class="header-anchor" href="#_6-1-规范的代码风格" aria-label="Permalink to "6.1 规范的代码风格""></a></h2><h3 id="_6-1-1-正确的缩进和空白" tabindex="-1">6.1.1 正确的缩进和空白 <a class="header-anchor" href="#_6-1-1-正确的缩进和空白" aria-label="Permalink to "6.1.1 正确的缩进和空白""></a></h3><ul><li>① 使用一次 <code>tab</code> 操作,实现缩进,默认整体向右边移动;如果使用 <code>shift + tab</code> 则整体向左移动。</li><li>② 运算符两边习惯各加一个空格,如:<code>2 + 4 = 6</code>。</li></ul><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><p>各种 IDE 都有格式化的快捷键,如:CLion 的格式化快捷键是 <code>Ctrl + Alt + L</code> 。</p></div><ul><li>示例:</li></ul><div class="language-c vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">#include</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> <stdio.h></span></span>
|
|
|
|
|
<span class="line"></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">int</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> main</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">() {</span></span>
|
|
|
|
|
<span class="line"></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> int</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> a </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 1</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> int</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> b </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 2</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> int</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> c </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> a </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">+</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> b;</span></span>
|
|
|
|
|
<span class="line"></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"c = </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%d</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, c);</span></span>
|
|
|
|
|
<span class="line"></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><h3 id="_6-1-2-代码风格" tabindex="-1">6.1.2 代码风格 <a class="header-anchor" href="#_6-1-2-代码风格" aria-label="Permalink to "6.1.2 代码风格""></a></h3><ul><li>在 C 语言中,有两种代码风格:<code>行尾风格</code>和<code>次行风格</code>。</li></ul><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><p>看个人爱好,任选一种即可,本人喜欢<code>行尾分格</code>!!!</p></div><ul><li>示例:行尾风格</li></ul><div class="language-c vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">int</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> main</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(){ </span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> if</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(a </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">></span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> b) {</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> a;</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> } </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">else</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> b;</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> } </span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">; </span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><ul><li>示例:次行风格</li></ul><div class="language-c vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">int</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> main</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">()</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">{ </span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> if</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(a </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">></span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> b) </span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> a;</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> } </span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> else</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> </span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> b;</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> } </span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">; </span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><h2 id="_6-2-代码细节剖析" tabindex="-1">6.2 代码细节剖析 <a class="header-anchor" href="#_6-2-代码细节剖析" aria-label="Permalink to "6.2 代码细节剖析""></a></h2><h3 id="_6-2-1-main-函数" tabindex="-1">6.2.1 main() 函数 <a class="header-anchor" href="#_6-2-1-main-函数" aria-label="Permalink to "6.2.1 main() 函数""></a></h3><ul><li>在 C 语言中,一个程序或工程可以定义很多函数,但是有且只有一个 main() 函数,作为程序执行的入口,并且在 main() 函数结尾结束整个程序的运行,即:</li></ul><div class="language-c vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">int</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> main</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(){</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><ul><li>如果 main() 函数是空括号,即表示 main() 函数不接收任何参数。</li><li>在 main() 函数之前的 <code>int</code> 称为关键字,代表数据类型是<code>整型</code>,它是 main() 函数的返回值的类型,即在执行 main() 函数之后一定会得到一个整数类型的值,即函数值。</li></ul><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><ul><li>① 在 C 语言中,人们约定,如果 <code>return 0</code>,就表示 main() 函数终止运行,且运行成功;如果返回其它非零整数,则表示运行失败。</li><li>② 默认情况下,如果 main() 函数中省略 <code>return 0</code> ,则编译器会自动加上;但是,为了保持统一的代码风格,不建议省略。</li></ul></div><h3 id="_6-2-2-函数体" tabindex="-1">6.2.2 函数体 <a class="header-anchor" href="#_6-2-2-函数体" aria-label="Permalink to "6.2.2 函数体""></a></h3><ul><li>① 一对花括号 <code>{}</code> 定义了函数的主体,所有函数都必须以大括号开头和结尾,成对出现。</li><li>② C 程序中的函数体指的是作为该函数一部分的语句。它可以是任何操作,比如:搜索、排序、打印等。</li><li>③ 每一个执行语句后面都会有一个英文分号<code>;</code>作为语句结束的标志。</li><li>④ 一行内可写几条语句,一条语句也可写在几行上。</li></ul><h3 id="_6-2-3-printf-函数" tabindex="-1">6.2.3 printf() 函数 <a class="header-anchor" href="#_6-2-3-printf-函数" aria-label="Permalink to "6.2.3 printf() 函数""></a></h3><ul><li>printf() 函数的格式,如下所示:</li></ul><div class="language-c vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> (</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">const</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> char</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> *</span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;">__format</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, ...)</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><ul><li>printf() 函数是产生格式化输出的函数,作用是将参数文本输出到屏幕,f 表示 format(格式化),表示可以指定输出文本的格式,即:</li></ul><div class="language-c vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> (</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"Hello World"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // 将字符串输出到控制台,行尾不换行</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><ul><li>如果想让光标移动到下一行的开头,可以在输出文本的结尾,可以添加一个换行符 <code>\\n</code>,即:</li></ul><div class="language-c vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span clas
|
|
|
|
|
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">// 编译器将在系统的标准头文件目录中查找文件。</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">#include</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> <filename></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><div class="language-c vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">// 用于包含用户自定义的头文件。</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">// 编译器首先在当前目录中查找文件,如果未找到,再在标准头文件目录中查找。</span></span>
|
2024-08-09 04:52:37 +02:00
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">#include</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> "filename"</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h1 id="第七章-clion-高级配置-⭐" tabindex="-1">第七章:CLion 高级配置(⭐) <a class="header-anchor" href="#第七章-clion-高级配置-⭐" aria-label="Permalink to "第七章:CLion 高级配置(⭐)""></a></h1><h2 id="_7-1-安装和配置-wsl2" tabindex="-1">7.1 安装和配置 WSL2 <a class="header-anchor" href="#_7-1-安装和配置-wsl2" aria-label="Permalink to "7.1 安装和配置 WSL2""></a></h2><h3 id="_7-1-1-概述" tabindex="-1">7.1.1 概述 <a class="header-anchor" href="#_7-1-1-概述" aria-label="Permalink to "7.1.1 概述""></a></h3><ul><li>WSL 2,全称为 Windows Subsystem for Linux 2,是微软提供的一种技术,允许用户在 Windows 操作系统上运行 Linux 内核。WSL 2 是 WSL 的升级版,它引入了一个真正的 Linux 内核来代替 WSL 1 中使用的兼容层,从而提供更高的性能和更广泛的系统调用支持。</li><li>其架构图,如下所示:</li></ul><p><img src="`+Pa+'" alt=""></p><ul><li><p>WSL2 的功能,如下所示:</p><ul><li>① <strong>真实的Linux内核</strong>:WSL 2 使用了微软开发的轻量级虚拟机,它包含了一个完整的 Linux 内核。这意味着 WSL 2 能够运行更多的 Linux 应用程序,并且支持更多的系统调用。</li><li>② <strong>文件系统性能提升</strong>:WSL 2 的文件系统性能比 WSL 1 有显著提升。对于 I/O 密集型的操作,如:编译代码或数据库操作,WSL 2 能够提供更快的速度。</li><li>③ <strong>兼容性增强</strong>:由于使用了真实的 Linux 内核,WSL 2 对 Linux 应用程序的兼容性大幅提高。许多在 WSL 1上不能运行或需要调整的应用程序,可以在 WSL 2上直接运行。</li><li>④ <strong>网络功能改进</strong>:WSL 2 提供了更好的网络集成,能够更容易地与 Windows 上的其他网络资源进行交互。</li><li>⑤ <strong>资源使用优化</strong>:WSL 2 使用轻量级虚拟机,比传统的虚拟机占用更少的资源,同时提供了类似的隔离和安全性。</li></ul></li><li><p>WSL 的用途,如下所示:</p><ul><li>① <strong>开发环境</strong>:WSL 2 为开发者提供了一个原生的Linux开发环境,而无需离开 Windows 。这对于需要在 Linux 上开发、测试或运行应用程序的开发者非常有帮助。</li><li>② <strong>学习和实验</strong>:用户可以使用 WSL 2 在Windows上学习和实验 Linux 命令行工具和应用程序,而无需设置双重引导系统或安装虚拟机。</li><li>③ <strong>多平台开发</strong>:对于跨平台开发者来说,WSL 2 允许他们在一个操作系统上同时进行 Windows 和 Linux 平台的开发和测试,提高工作效率。</li><li>④ <strong>运行 Linux 工具和应用程序</strong>:WSL 2 支持在 Windows 上直接运行各种 Linux 工具和应用程序,如:Docker、数据库、编程语言环境等。</li></ul></li></ul><h3 id="_7-1-2-wsl2-的安装" tabindex="-1">7.1.2 WSL2 的安装 <a class="header-anchor" href="#_7-1-2-wsl2-的安装" aria-label="Permalink to "7.1.2 WSL2 的安装""></a></h3><ul><li>① BIOS 或 UEFI 中,开启虚拟化:步骤略。</li></ul><p><img src="'+Ta+'" alt=""></p><ul><li>② 查看是否开启了虚拟化:</li></ul><p><img src="'+Ua+'" alt=""></p><ul><li>③ 启用适用于 Linux 的 Windows 子系统:</li></ul><div class="language-powershell vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">powershell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span sty
|
2024-07-11 10:07:44 +02:00
|
|
|
|
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sudo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> dnf</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> groupinstall</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> "Development Tools"</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -y</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 安装开发工具包</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sudo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> dnf</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> install</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> gcc</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> gcc-c++</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -y</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 安装GCC相关工具链</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sudo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> dnf</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> install</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> cmake</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -y</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 安装 cmake</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sudo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> dnf</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> install</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> make</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -y</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 安装 make</span></span>
|
2024-08-06 09:58:07 +02:00
|
|
|
|
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sudo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> dnf</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> install</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> gdb</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -y</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 安装 gdb</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p><img src="`+Va+'" alt=""></p><ul><li>可以通过 CLion 测试是否安装成功:</li></ul><p><img src="'+Ha+`" alt=""></p><h3 id="_7-1-4-配置-wsl2" tabindex="-1">7.1.4 配置 WSL2 <a class="header-anchor" href="#_7-1-4-配置-wsl2" aria-label="Permalink to "7.1.4 配置 WSL2""></a></h3><ul><li>本人的安装的是 Ubuntu 24.04,所以需要执行如下命令,以便安装 cmake 相关工具链:</li></ul><div class="language-shell vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sudo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> apt</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> update</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> && </span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sudo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> apt</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> upgrade</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -y</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 更新包管理器</span></span>
|
2024-07-11 10:07:44 +02:00
|
|
|
|
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sudo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> apt</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> install</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> build-essential</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -y</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 安装开发工具包</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sudo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> apt</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> install</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> gcc</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> g++</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -y</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 安装 GCC 相关工具链</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sudo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> apt</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> install</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> cmake</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -y</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 安装 cmake</span></span>
|
2024-08-09 04:52:37 +02:00
|
|
|
|
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sudo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> apt</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> install</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> gdb</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -y</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 安装 gdb</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p><img src="`+$a+'" alt=""></p><ul><li>可以通过 CLion 测试是否安装成功:</li></ul><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><p>默认仓库中的 GDB 的版本会很高,CLion 会不支持,那么就需要手动下载源码,自行编译,本次不再演示!!!</p></div><p><img src="'+Ka+'" alt=""></p><h2 id="_7-2-切换-clion-中的-cmake-的工具链" tabindex="-1">7.2 切换 CLion 中的 cmake 的工具链 <a class="header-anchor" href="#_7-2-切换-clion-中的-cmake-的工具链" aria-label="Permalink to "7.2 切换 CLion 中的 cmake 的工具链""></a></h2><ul><li>可以在 CLoin 中切换 cmake 的工具链,以便支持不同平台的 cmake ,即:</li></ul><p><img src="'+Ja+'" alt=""></p><h2 id="_7-3-修改-cmakelists-txt-文件" tabindex="-1">7.3 修改 CMakeLists.txt 文件 <a class="header-anchor" href="#_7-3-修改-cmakelists-txt-文件" aria-label="Permalink to "7.3 修改 CMakeLists.txt 文件""></a></h2><ul><li>前文也提到了,在一个 C 语言项目中,只能有一个 main() 函数;但是,我们可以修改 <code>CMakeLists.txt</code> 文件的内容,以便其支持在一个 C 语言项目中,可以有多个包含 main() 函数的文件,如下所示:</li></ul><p><img src="'+ja+`" alt=""></p><ul><li><code>CMakeLists.txt</code> 的内容,如下所示:</li></ul><div class="language-txt vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">txt</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span>cmake_minimum_required(VERSION 3.10)</span></span>
|
2024-07-11 10:07:44 +02:00
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span># 项目名称和版本号</span></span>
|
|
|
|
|
<span class="line"><span>project(c-study VERSION 1.0 LANGUAGES C)</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span># 设置 C 标准</span></span>
|
2024-07-26 01:28:10 +02:00
|
|
|
|
<span class="line"><span>set(CMAKE_C_STANDARD 23)</span></span>
|
2024-07-11 10:07:44 +02:00
|
|
|
|
<span class="line"><span>set(CMAKE_C_STANDARD_REQUIRED True)</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span># 辅助函数,用于递归查找所有源文件</span></span>
|
|
|
|
|
<span class="line"><span>function(collect_sources result dir)</span></span>
|
|
|
|
|
<span class="line"><span> file(GLOB_RECURSE new_sources "\${dir}/*.c")</span></span>
|
|
|
|
|
<span class="line"><span> set(\${result} \${\${result}} \${new_sources} PARENT_SCOPE)</span></span>
|
|
|
|
|
<span class="line"><span>endfunction()</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span># 查找顶层 include 目录(如果存在)</span></span>
|
|
|
|
|
<span class="line"><span>if (EXISTS "\${CMAKE_SOURCE_DIR}/include")</span></span>
|
|
|
|
|
<span class="line"><span> include_directories(\${CMAKE_SOURCE_DIR}/include)</span></span>
|
|
|
|
|
<span class="line"><span>endif ()</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span># 查找所有源文件</span></span>
|
|
|
|
|
<span class="line"><span>set(SOURCES)</span></span>
|
|
|
|
|
<span class="line"><span>collect_sources(SOURCES \${CMAKE_SOURCE_DIR})</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span># 用于存储已经处理过的可执行文件名,防止重复</span></span>
|
|
|
|
|
<span class="line"><span>set(EXECUTABLE_NAMES)</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span># 创建可执行文件</span></span>
|
|
|
|
|
<span class="line"><span>foreach (SOURCE \${SOURCES})</span></span>
|
|
|
|
|
<span class="line"><span> # 获取文件的相对路径</span></span>
|
|
|
|
|
<span class="line"><span> file(RELATIVE_PATH REL_PATH \${CMAKE_SOURCE_DIR} \${SOURCE})</span></span>
|
|
|
|
|
<span class="line"><span> # 将路径中的斜杠替换为下划线,生成唯一的可执行文件名</span></span>
|
|
|
|
|
<span class="line"><span> string(REPLACE "/" "_" EXECUTABLE_NAME \${REL_PATH})</span></span>
|
|
|
|
|
<span class="line"><span> string(REPLACE "\\\\" "_" EXECUTABLE_NAME \${EXECUTABLE_NAME})</span></span>
|
|
|
|
|
<span class="line"><span> string(REPLACE "." "_" EXECUTABLE_NAME \${EXECUTABLE_NAME})</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span> # 处理与 CMakeLists.txt 文件同名的问题</span></span>
|
|
|
|
|
<span class="line"><span> if (\${EXECUTABLE_NAME} STREQUAL "CMakeLists_txt")</span></span>
|
|
|
|
|
<span class="line"><span> set(EXECUTABLE_NAME "\${EXECUTABLE_NAME}_exec")</span></span>
|
|
|
|
|
<span class="line"><span> endif ()</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span> # 检查是否已经创建过同名的可执行文件</span></span>
|
|
|
|
|
<span class="line"><span> if (NOT EXECUTABLE_NAME IN_LIST EXECUTABLE_NAMES)</span></span>
|
|
|
|
|
<span class="line"><span> list(APPEND EXECUTABLE_NAMES \${EXECUTABLE_NAME})</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span> # 创建可执行文件</span></span>
|
|
|
|
|
<span class="line"><span> add_executable(\${EXECUTABLE_NAME} \${SOURCE})</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span> # 查找源文件所在的目录,并添加为包含目录(头文件可能在同一目录下)</span></span>
|
|
|
|
|
<span class="line"><span> get_filename_component(DIR \${SOURCE} DIRECTORY)</span></span>
|
|
|
|
|
<span class="line"><span> target_include_directories(\${EXECUTABLE_NAME} PRIVATE \${DIR})</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span> # 检查并添加子目录中的 include 目录(如果存在)</span></span>
|
|
|
|
|
<span class="line"><span> if (EXISTS "\${DIR}/include")</span></span>
|
|
|
|
|
<span class="line"><span> target_include_directories(\${EXECUTABLE_NAME} PRIVATE \${DIR}/include)</span></span>
|
|
|
|
|
<span class="line"><span> endif ()</span></span>
|
2024-07-26 01:28:10 +02:00
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span> # 检查并添加 module 目录中的所有 C 文件(如果存在)</span></span>
|
|
|
|
|
<span class="line"><span> if (EXISTS "\${DIR}/module")</span></span>
|
|
|
|
|
<span class="line"><span> file(GLOB_RECURSE MODULE_SOURCES "\${DIR}/module/*.c")</span></span>
|
|
|
|
|
<span class="line"><span> target_sources(\${EXECUTABLE_NAME} PRIVATE \${MODULE_SOURCES})</span></span>
|
|
|
|
|
<span class="line"><span> endif ()</span></span>
|
2024-07-11 10:07:44 +02:00
|
|
|
|
<span class="line"><span> endif ()</span></span>
|
2024-08-09 04:52:37 +02:00
|
|
|
|
<span class="line"><span>endforeach ()</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br><span class="line-number">57</span><br><span class="line-number">58</span><br><span class="line-number">59</span><br><span class="line-number">60</span><br><span class="line-number">61</span><br><span class="line-number">62</span><br><span class="line-number">63</span><br><span class="line-number">64</span><br></div></div><h2 id="_7-4-配置-clang-format-文件" tabindex="-1">7.4 配置 .clang-format 文件 <a class="header-anchor" href="#_7-4-配置-clang-format-文件" aria-label="Permalink to "7.4 配置 .clang-format 文件""></a></h2><ul><li>配置 <code>.clang-format</code> 格式化文件,以便写代码的时候,可以自动保存并格式化 C 程序代码,如下所示:</li></ul><p><img src="`+Qa+`" alt=""></p><ul><li><code>.clang-format</code> 的内容,如下所示:</li></ul><div class="language-txt vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">txt</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span>BasedOnStyle: Google</span></span>
|
2024-07-11 10:07:44 +02:00
|
|
|
|
<span class="line"><span>IndentWidth: 4</span></span>
|
|
|
|
|
<span class="line"><span>UseTab: Never</span></span>
|
|
|
|
|
<span class="line"><span>ColumnLimit: 0</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span># 控制大括号的位置</span></span>
|
|
|
|
|
<span class="line"><span>BreakBeforeBraces: Attach</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span># 控制空行的使用</span></span>
|
|
|
|
|
<span class="line"><span>EmptyLineBeforeAccessModifier: Never</span></span>
|
|
|
|
|
<span class="line"><span>KeepEmptyLinesAtTheStartOfBlocks: true</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span># 控制短函数、短 if 语句和循环的格式</span></span>
|
|
|
|
|
<span class="line"><span>AllowShortFunctionsOnASingleLine: Empty</span></span>
|
|
|
|
|
<span class="line"><span>AllowShortIfStatementsOnASingleLine: false</span></span>
|
|
|
|
|
<span class="line"><span>AllowShortLoopsOnASingleLine: false</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span># 控制其他格式选项</span></span>
|
|
|
|
|
<span class="line"><span>AlignConsecutiveAssignments: true</span></span>
|
|
|
|
|
<span class="line"><span>AlignConsecutiveDeclarations: true</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span># 控制注释的格式化</span></span>
|
|
|
|
|
<span class="line"><span>ReflowComments: true</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span># 控制包含指令的格式化</span></span>
|
|
|
|
|
<span class="line"><span>IncludeBlocks: Regroup</span></span>
|
|
|
|
|
<span class="line"><span>SortIncludes: CaseInsensitive</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span>SpaceBeforeParens: ControlStatements</span></span>
|
|
|
|
|
<span class="line"><span>SpacesInParentheses: false</span></span>
|
|
|
|
|
<span class="line"><span>SpacesInAngles: false</span></span>
|
|
|
|
|
<span class="line"><span>SpacesInContainerLiterals: false</span></span>
|
2024-08-01 07:49:31 +02:00
|
|
|
|
<span class="line"><span>SpacesInCStyleCastParentheses: false</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br></div></div><ul><li>CLion 中配置<code>保存</code>的时候<code>自动格式化</code>,即:</li></ul><p><img src="`+Ya+'" alt=""></p><h2 id="_7-5-配置-gitignore-文件" tabindex="-1">7.5 配置 .gitignore 文件 <a class="header-anchor" href="#_7-5-配置-gitignore-文件" aria-label="Permalink to "7.5 配置 .gitignore 文件""></a></h2><ul><li>需要在项目中,配置 <code>.gitignore</code> 文件,以便在提交代码到 Git 仓库的时候,忽略某些文件或目录,如下所示:</li></ul><p><img src="'+za+`" alt=""></p><ul><li><code>.gitignore</code> 文件的内容,如下所示:</li></ul><div class="language-txt vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">txt</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span>.vscode</span></span>
|
2024-07-11 10:07:44 +02:00
|
|
|
|
<span class="line"><span>.idea</span></span>
|
|
|
|
|
<span class="line"><span>cmake-build-*</span></span>
|
2024-08-09 04:52:37 +02:00
|
|
|
|
<span class="line"><span>build</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h2 id="_7-6-演示" tabindex="-1">7.6 演示 <a class="header-anchor" href="#_7-6-演示" aria-label="Permalink to "7.6 演示""></a></h2><ul><li>我们可以在项目中,临时创建或复制一个文件,看上述配置是否生效,即:</li></ul><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><p>如果是复制并粘贴一个文件到项目中,请点击<code>重新加载 CMake 项目</code>!!!</p></div><p><img src="`+Za+'" alt=""></p><h1 id="第八章-c-语言的编译过程-⭐" tabindex="-1">第八章:C 语言的编译过程(⭐) <a class="header-anchor" href="#第八章-c-语言的编译过程-⭐" aria-label="Permalink to "第八章:C 语言的编译过程(⭐)""></a></h1><h2 id="_8-1-概述" tabindex="-1">8.1 概述 <a class="header-anchor" href="#_8-1-概述" aria-label="Permalink to "8.1 概述""></a></h2><ul><li>C 程序的编译过程,如下所示:</li></ul><p><img src="'+si+'" alt=""></p><ul><li>过程 ① :编写(编辑)源代码,即:编写 C 语言源程序代码,并以文件的形式存储在磁盘中。</li></ul><div class="important custom-block github-alert"><p class="custom-block-title">IMPORTANT</p><p></p><p>源程序需要以 <code>.c</code> 作为扩展名。</p></div><ul><li>过程 ② :编译,即:将 C 语言源程序转换为<code>目标程序(或目标文件)</code>。如果程序没有错误,没有任何提示,就会生成一个扩展名为 <code>.obj</code>或 <code>.o</code> 的二进制文件。C 语言中的每条可执行语句经过编译之后,最终都会转换为二进制的机器指令。</li></ul><div class="important custom-block github-alert"><p class="custom-block-title">IMPORTANT</p><p></p><ul><li><p>① 其实,<code>编译阶段</code>包含了<code>预处理</code>、<code>编译</code>和<code>汇编</code>。</p></li><li><p>② <code>预处理</code>是编译过程的第一个阶段。在这个阶段,预处理器处理源代码中的指令(例如:<code>#include</code>、<code>#define</code>等),主要任务包括:</p><ul><li><p><strong>头文件包含</strong>:将头文件的内容插入到源文件中。例如:<code>#include <stdio.h></code>会被替换为<code>stdio.h</code>文件的内容。</p></li><li><p><strong>宏展开</strong>:替换宏定义。例如:<code>#define PI 3.14</code>会将代码中的<code>PI</code>替换为<code>3.14</code>。</p></li><li><p><strong>条件编译</strong>:根据条件指令(如:<code>#ifdef</code>、<code>#ifndef</code>)有选择地编译代码。</p></li><li><p><strong>删除代码中的注释,但是不会进行语法检查</strong>。</p></li><li><p>预处理完成后,生成一个扩展名为<code>.i</code>的中间文件。</p></li></ul></li><li><p>③ <code>编译</code>是将预处理后的源代码转换为汇编代码的过程。在这个阶段,编译器会检查代码的语法和语义,将其转换为目标机器的汇编语言,生成一个扩展名为<code>.s</code>的汇编文件。</p></li><li><p>④ <code>汇编</code>是将汇编代码转换为机器代码(也称为目标代码或目标文件)的过程。在这个阶段,汇编器将汇编指令转换为二进制机器指令,生成一个扩展名为<code>.o</code>或 <code>.obj</code>的目标文件。</p></li></ul></div><ul><li>过程 ③ :链接(连接),即:将编译形成的目标文件 <code>*.obj</code> 或 <code>*.o</code>和库函数以及其他目录文件<code>链接</code>,形成一个统一的二进制文件 <code>*.exe</code>。</li></ul><div class="important custom-block github-alert"><p class="custom-block-title">IMPORTANT</p><p></p><ul><li>为什么需要链接库文件?</li><li>因为<EFBFBD><EFBFBD>
|
2024-07-26 01:17:43 +02:00
|
|
|
|
<span class="line"><span>networkingMode=mirrored</span></span>
|
|
|
|
|
<span class="line"><span>dnsTunneling=true</span></span>
|
|
|
|
|
<span class="line"><span>firewall=true</span></span>
|
|
|
|
|
<span class="line"><span>autoProxy=true</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span>[experimental]</span></span>
|
|
|
|
|
<span class="line"><span># requires dnsTunneling but are also OPTIONAL</span></span>
|
|
|
|
|
<span class="line"><span>bestEffortDnsParsing=true</span></span>
|
2024-08-09 04:52:37 +02:00
|
|
|
|
<span class="line"><span>useWindowsDnsCache=true</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><p><img src="`+pi+'" alt=""></p><ul><li>在命令行中,执行如下的命令:</li></ul><div class="language-shell vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">wsl</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --shutdown</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><p><img src="'+ri+'" alt=""></p><ul><li>此时,再打开终端,就没有这种提示了:</li></ul><p><img src="'+ci+'" alt=""></p><h2 id="_9-2-clion-调试问题" tabindex="-1">9.2 CLion 调试问题 <a class="header-anchor" href="#_9-2-clion-调试问题" aria-label="Permalink to "9.2 CLion 调试问题""></a></h2><ul><li>在 CLion 中进行 run(运行)程序的时候,对于 <code>printf</code> 函数或 <code>scanf</code> 函数很正常,如下所示:</li></ul><p><img src="'+hi+'" alt=""></p><ul><li>但是,当我们 debug(调试) 的时候,对于 <code>printf</code> 函数或 <code>scanf</code> 函数会一直没有输出,如下所示:</li></ul><p><img src="'+di+`" alt=""></p><ul><li>原因是 <code>scanf</code> 函数并不是直接让用户从键盘输入数据,而是先检查缓冲区,处理缓冲区中的数据;当遇到 <code>scanf</code> 函数时,程序会先检查输入缓冲区中是否有数据,所以解决方案就是<code>禁用缓冲区</code>,如下所示:</li></ul><div class="language-c vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">#include</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> <stdio.h></span></span>
|
2024-08-01 02:32:54 +02:00
|
|
|
|
<span class="line"></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">int</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> main</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">() {</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // 禁用 stdout 缓冲区</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> setbuf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(stdout, </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">NULL</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span></span>
|
|
|
|
|
<span class="line"></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> int</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> a, b, c;</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"请输入整数 a 、b 和 c 的值:"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> scanf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%d</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> %d</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> %d</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">&</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">a, </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">&</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">b, </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">&</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">c);</span></span>
|
|
|
|
|
<span class="line"></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> int</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> result </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> a </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">*</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> b </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">*</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> c;</span></span>
|
|
|
|
|
<span class="line"></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%d</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> × </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%d</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> × </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%d</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> = </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%d</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, a, b, c, result);</span></span>
|
|
|
|
|
<span class="line"></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
2024-08-01 07:49:31 +02:00
|
|
|
|
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><ul><li>那么,就会达到我们想要的效果了,如下所示:</li></ul><p><img src="`+oi+'" alt=""></p><h2 id="_9-3-内存泄露检测" tabindex="-1">9.3 内存泄露检测 <a class="header-anchor" href="#_9-3-内存泄露检测" aria-label="Permalink to "9.3 内存泄露检测""></a></h2><h3 id="_9-3-1-概述" tabindex="-1">9.3.1 概述 <a class="header-anchor" href="#_9-3-1-概述" aria-label="Permalink to "9.3.1 概述""></a></h3><ul><li>C 语言中的指针是否使用是个颇具争议的话题,现代化的高级编程语言通过各种策略和机制,在编译期就能解决指针危险的问题。但是,遗憾的是,C 语言的指针很大程度上,在运行期才会暴露问题。</li><li>幸运的是,我们可以使用 <code>Valgrind</code> 项目来进行<code>内存泄露检测</code>和<code>性能分析</code>,而 <code>Valgrind</code> 只支持 Linux 。</li></ul><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><p>win 11 中的 WSL2 就是个 Linux 环境,我们可以在上面跑各种 Linux 工具,这样我们就不需要再安装虚拟机软件了,如:VMware Workstation(它会完整的模拟一个硬件系统,并在上面跑各种 Linux ,实在是太笨重了)。</p></div><h3 id="_9-3-2-安装" tabindex="-1">9.3.2 安装 <a class="header-anchor" href="#_9-3-2-安装" aria-label="Permalink to "9.3.2 安装""></a></h3><ul><li>在 WSL2 上安装 Valgrind :</li></ul><div class="language-shell vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">dnf</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -y</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> upgrade</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> && </span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">dnf</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -y</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> install</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> valgrind</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # AlmaLinux</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><div class="language-shell vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">apt</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -y</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> update</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> && </span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">apt</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -y</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> upgrade</span><span style="--shiki-light:#24292
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span># 项目名称和版本号</span></span>
|
|
|
|
|
<span class="line"><span>project(c-study VERSION 1.0 LANGUAGES C)</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span># 设置 C 标准</span></span>
|
|
|
|
|
<span class="line"><span>set(CMAKE_C_STANDARD 23)</span></span>
|
|
|
|
|
<span class="line"><span>set(CMAKE_C_STANDARD_REQUIRED True)</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span># 辅助函数,用于递归查找所有源文件</span></span>
|
|
|
|
|
<span class="line"><span>function(collect_sources result dir)</span></span>
|
|
|
|
|
<span class="line"><span> file(GLOB_RECURSE new_sources "\${dir}/*.c")</span></span>
|
|
|
|
|
<span class="line"><span> set(\${result} \${\${result}} \${new_sources} PARENT_SCOPE)</span></span>
|
|
|
|
|
<span class="line"><span>endfunction()</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span># 查找顶层 include 目录(如果存在)</span></span>
|
|
|
|
|
<span class="line"><span>if (EXISTS "\${CMAKE_SOURCE_DIR}/include")</span></span>
|
|
|
|
|
<span class="line"><span> include_directories(\${CMAKE_SOURCE_DIR}/include)</span></span>
|
|
|
|
|
<span class="line"><span>endif ()</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span># 查找所有源文件</span></span>
|
|
|
|
|
<span class="line"><span>set(SOURCES)</span></span>
|
|
|
|
|
<span class="line"><span>collect_sources(SOURCES \${CMAKE_SOURCE_DIR})</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span># 用于存储已经处理过的可执行文件名,防止重复</span></span>
|
|
|
|
|
<span class="line"><span>set(EXECUTABLE_NAMES)</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span># 创建可执行文件</span></span>
|
|
|
|
|
<span class="line"><span>foreach (SOURCE \${SOURCES})</span></span>
|
|
|
|
|
<span class="line"><span> # 获取文件的相对路径</span></span>
|
|
|
|
|
<span class="line"><span> file(RELATIVE_PATH REL_PATH \${CMAKE_SOURCE_DIR} \${SOURCE})</span></span>
|
|
|
|
|
<span class="line"><span> # 将路径中的斜杠替换为下划线,生成唯一的可执行文件名</span></span>
|
|
|
|
|
<span class="line"><span> string(REPLACE "/" "_" EXECUTABLE_NAME \${REL_PATH})</span></span>
|
|
|
|
|
<span class="line"><span> string(REPLACE "\\\\" "_" EXECUTABLE_NAME \${EXECUTABLE_NAME})</span></span>
|
|
|
|
|
<span class="line"><span> string(REPLACE "." "_" EXECUTABLE_NAME \${EXECUTABLE_NAME})</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span> # 处理与 CMakeLists.txt 文件同名的问题</span></span>
|
|
|
|
|
<span class="line"><span> if (\${EXECUTABLE_NAME} STREQUAL "CMakeLists_txt")</span></span>
|
|
|
|
|
<span class="line"><span> set(EXECUTABLE_NAME "\${EXECUTABLE_NAME}_exec")</span></span>
|
|
|
|
|
<span class="line"><span> endif ()</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span> # 检查是否已经创建过同名的可执行文件</span></span>
|
|
|
|
|
<span class="line"><span> if (NOT EXECUTABLE_NAME IN_LIST EXECUTABLE_NAMES)</span></span>
|
|
|
|
|
<span class="line"><span> list(APPEND EXECUTABLE_NAMES \${EXECUTABLE_NAME})</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span> # 创建可执行文件</span></span>
|
|
|
|
|
<span class="line"><span> add_executable(\${EXECUTABLE_NAME} \${SOURCE})</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span> # 查找源文件所在的目录,并添加为包含目录(头文件可能在同一目录下)</span></span>
|
|
|
|
|
<span class="line"><span> get_filename_component(DIR \${SOURCE} DIRECTORY)</span></span>
|
|
|
|
|
<span class="line"><span> target_include_directories(\${EXECUTABLE_NAME} PRIVATE \${DIR})</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span> # 检查并添加子目录中的 include 目录(如果存在)</span></span>
|
|
|
|
|
<span class="line"><span> if (EXISTS "\${DIR}/include")</span></span>
|
|
|
|
|
<span class="line"><span> target_include_directories(\${EXECUTABLE_NAME} PRIVATE \${DIR}/include)</span></span>
|
|
|
|
|
<span class="line"><span> endif ()</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span> # 检查并添加 module 目录中的所有 C 文件(如果存在)</span></span>
|
|
|
|
|
<span class="line"><span> if (EXISTS "\${DIR}/module")</span></span>
|
|
|
|
|
<span class="line"><span> file(GLOB_RECURSE MODULE_SOURCES "\${DIR}/module/*.c")</span></span>
|
|
|
|
|
<span class="line"><span> target_sources(\${EXECUTABLE_NAME} PRIVATE \${MODULE_SOURCES})</span></span>
|
|
|
|
|
<span class="line"><span> endif ()</span></span>
|
|
|
|
|
<span class="line"><span> endif ()</span></span>
|
|
|
|
|
<span class="line"><span>endforeach ()</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br><span class="line-number">57</span><br><span class="line-number">58</span><br><span class="line-number">59</span><br><span class="line-number">60</span><br><span class="line-number">61</span><br><span class="line-number">62</span><br><span class="line-number">63</span><br><span class="line-number">64</span><br></div></div><ul><li>在 CLion 中正常运行代码:</li></ul><p><img src="`+Ei+'" alt=""></p><ul><li>在 CLion 中通过 valgrind 运行代码:</li></ul><p><img src="'+Ci+'" alt=""></p><h2 id="_9-4-性能分析" tabindex="-1">9.4 性能分析 <a class="header-anchor" href="#_9-4-性能分析" aria-label="Permalink to "9.4 性能分析""></a></h2><h3 id="_9-4-1-概述" tabindex="-1">9.4.1 概述 <a class="header-anchor" href="#_9-4-1-概述" aria-label="Permalink to "9.4.1 概述""></a></h3><ul><li><code>perf</code> 是一个 Linux 下的性能分析工具,主要用于监控和分析系统性能。它可以帮助开发者和系统管理员了解系统中哪些部分在消耗资源、识别性能瓶颈以及分析程序的运行效率。</li></ul><h3 id="_9-4-2-安装" tabindex="-1">9.4.2 安装 <a class="header-anchor" href="#_9-4-2-安装" aria-label="Permalink to "9.4.2 安装""></a></h3><h4 id="_9-4-2-1-almalinux9" tabindex="-1">9.4.2.1 AlmaLinux9 <a class="header-anchor" href="#_9-4-2-1-almalinux9" aria-label="Permalink to "9.4.2.1 AlmaLinux9""></a></h4><ul><li>在 WSL2 中的 AlmaLinux 安装 perf :</li></ul><div class="language-shell vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392
|
|
|
|
|
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> && </span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">apt</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -y</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> install</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> linux-tools-common</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> linux-tools-generic</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> linux-tools-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$(</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">uname</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -r</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p><img src="`+Fi+'" alt=""></p><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><p>之所以报错的原因,在于 WSL2 中的 Ubuntu 的内核是定制化的,并非 Ubuntu 的母公司 Canonical 发布的标准内核,所以需要我们手动编译安装。</p></div><ul><li>查看内核版本:</li></ul><div class="language-shell vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">uname</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -sr</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><p><img src="'+_i+'" alt=""></p><ul><li>设置环境变量,方便后续引用:</li></ul><div class="language-shell vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">export</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> KERNEL_VERSION</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$(</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">uname</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -r</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> |</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> cut</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -d</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'-'</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -f1</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><p><img src="'+vi+`" alt=""></p><ul><li>安装依赖库:</li></ul><div class="language-shell vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">apt</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -y</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> update</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> && </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">\\</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> apt</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -y</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> install</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> binutils-dev</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> debuginfod</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> default-jdk</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> default-jre</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> libaio-dev</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> \\</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> libbabeltrace-dev</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> libcap-dev</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> libdw-dev</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> libdwarf-dev</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> libelf-dev</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> libiberty-dev</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> \\</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> liblzma-dev</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> libnuma-dev</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> libperl-dev</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> libpfm4-dev</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> libslang2-dev</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> libssl-dev</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> \\</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> libtraceevent-dev</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> libunwind-dev</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> libzstd-dev</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> libzstd1</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> python3-setuptools</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> python3</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> \\</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> python3-dev</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> systemtap-sdt-dev</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> zlib1g-dev</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> bc</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> dwarves</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> bison</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> flex</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> libnewt-dev</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> libdwarf++0</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> \\</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> libelf++0</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> libbfb0-dev</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> python-dev-is-python3</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p><img src="`+Bi+`" alt=""></p><ul><li>下载源码:</li></ul><div class="language-shell vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">git</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> clone</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> \\</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --depth</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 1</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> \\</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --single-branch</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --branch=linux-msft-wsl-\${</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">KERNEL_VERSION</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">}</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> \\</span></span>
|
2024-08-09 04:52:37 +02:00
|
|
|
|
<span class="line"><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> https://github.com/microsoft/WSL2-Linux-Kernel.git</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p><img src="`+fi+'" alt=""></p><ul><li>编译内核代码:</li></ul><div class="language-shell vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">cd</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> WSL2-Linux-Kernel</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><div class="language-shell vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">make</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -j</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> $(</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">nproc</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">) </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">KCONFIG_CONFIG=Microsoft/config-wsl</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><p><img src="'+Ai+'" alt=""></p><ul><li>编译 perf 工具:</li></ul><div class="language-shell vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">cd</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> tools/perf</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><div class="language-shell vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">make</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> clean</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> && </span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">make</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><p><img src="'+xi+'" alt=""></p><ul><li>复制到 PATH 变量所指向的路径中:</li></ul><div class="language-shell vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">cp</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> perf</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> /usr/bin/</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><p><img src="'+Di+'" alt=""></p><h3 id="_9-4-3-整合" tabindex="-1">9.4.3 整合 <a class="header-anchor" href="#_9-4-3-整合" aria-label="Permalink to "9.4.3 整合""></a></h3><ul><li>CLion 中配置 perf 的路径:</li></ul><p><img src="'+qi+'" alt=""></p><ul><li>在 CLion 中通过 perf 运行代码:</li></ul><p><img src="'+Si+'" alt=""></p><h2 id="_9-5-win-中文乱码问题" tab
|