c/assets/notes_01_c-basic_01_xdx_index.md.8ISV4iA7.js
2024-08-26 02:54:04 +00:00

294 lines
177 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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

import{_ as s,c as a,o as i,a6 as n}from"./chunks/framework.hMCIpNYY.js";const l="/c/assets/1.D60Zf6_d.png",e="/c/assets/2.D2-G6nV2.png",p="/c/assets/3.DpzkVk5V.png",t="/c/assets/4.BXk95GIP.png",r="/c/assets/5.CbWTB08W.png",c="/c/assets/6.twO9TCIg.png",h="/c/assets/7.CKOjeIvS.png",d="/c/assets/8.DKmR0CxE.png",o="/c/assets/9.Cudf77_s.gif",k="/c/assets/10.BBUUlhRC.png",u="/c/assets/11.iLABkB7O.png",g="/c/assets/12.BQuYWOU9.png",b="/c/assets/13.CslpH6j0.png",m="/c/assets/14.BAyvZdKq.png",C="/c/assets/15.C3oC1iUd.png",E="/c/assets/16.Ds-jsEZn.png",y="/c/assets/17.Cd37gMYs.png",F="/c/assets/18.CAw2z3gc.png",_="/c/assets/19.DdPThnr-.png",v="/c/assets/20.q_rvlDnY.png",B="/c/assets/21.JRHg9RZA.png",f="/c/assets/22.Baq-Hb06.png",A="/c/assets/23.c1ht53mu.png",D="/c/assets/24.CAZmH5Py.png",x="/c/assets/25.DWeCYBe9.png",q="/c/assets/26.2XM8pYni.png",w="/c/assets/27.CN0t-fuo.png",S="/c/assets/28.AcDh3LXQ.png",L="/c/assets/29.CUkDFAc6.png",z="/c/assets/30.picNTNaJ.png",P="/c/assets/31.DKX92RgJ.png",T="/c/assets/32.Bxr7QKI3.png",W="/c/assets/33.DuFWsOgW.png",M="/c/assets/34.Da9qUcfr.png",U="/c/assets/35.7_gEABSX.gif",O="/c/assets/36.C2KQYoVC.gif",I="/c/assets/37.RTUP8Vtx.png",N="/c/assets/38.Dq21DxwL.png",R="/c/assets/39.QmpUaNxU.png",V="/c/assets/40.DP0C29gZ.png",G="/c/assets/41.DwzHokd4.png",X="/c/assets/42.Bi6K26yA.png",$="/c/assets/43.Bw8DcEQ_.png",H="/c/assets/44.DPc8Z9rS.png",K="/c/assets/45.S84xHMTl.png",Q="/c/assets/46.iw3JQUkn.png",Y="/c/assets/47.ILeJt-HK.png",j="/c/assets/48.CHgm7khh.png",J="/c/assets/49.N1nqJaEg.png",Z="/c/assets/50.DHsnibMi.png",ss="/c/assets/51.BQr6Q0uV.png",as="/c/assets/52.BVemi2Lh.png",is="/c/assets/53.BHR69UXx.png",ns="/c/assets/54.CPSU5SJI.png",ls="/c/assets/55.CaQLoDlw.png",es="/c/assets/56.aRGy4XbL.png",ps="/c/assets/57.kNO9fmPR.png",ts="/c/assets/58.D4EkoC4o.png",rs="/c/assets/59.V23Lo9e_.png",cs="/c/assets/60.Du9LVeMB.png",hs="/c/assets/61.DdFg75U9.png",ds="/c/assets/62.C6bz6KtT.png",os="/c/assets/63.CB6LAhB5.png",ks="/c/assets/64.Bip8bkua.png",us="/c/assets/65.DRNHRduW.png",gs="/c/assets/66.Cth4qqEn.png",bs="/c/assets/67.DWGWZ64P.png",ms="/c/assets/68.3hDIDdvx.png",Cs="/c/assets/69.Bw_dQ4ap.png",Es="/c/assets/70.Cn_ubTyA.png",ys="/c/assets/71.C84EBqwB.png",Fs="/c/assets/72.dZfJY4eR.png",_s="/c/assets/73.B0zm0Z79.png",vs="/c/assets/74.CaTcEy1Q.png",Bs="/c/assets/75.iEc8mo9g.png",fs="/c/assets/76.Co2KxM6t.png",As="/c/assets/77.CaUm8Qhc.png",Ds="/c/assets/78.kOkqIKG6.png",xs="/c/assets/79.CyeASH-c.png",qs="/c/assets/80.CFdk60La.png",ws="/c/assets/81.Bhl-0uK_.png",Ss="/c/assets/82.Dni9KbXV.png",Ls="/c/assets/83.BXx10MCV.png",zs="/c/assets/84.CXcIKX1T.png",Ps="/c/assets/85.DptbJqQo.png",Ts="/c/assets/86.CQ3_QCQT.png",Ws="/c/assets/87.CQoDRJM5.gif",Ms="/c/assets/88.CPfwZvD9.gif",Us="/c/assets/89.bxEomEjU.gif",Os="/c/assets/90.C_onhf2k.png",Is="/c/assets/91.Budj3s38.gif",Ns="/c/assets/92.CH8koQdu.gif",Rs="/c/assets/93.kUJhxMtg.png",Vs="/c/assets/94.BJeJJW4_.png",Gs="/c/assets/95.BCuK6eJL.gif",Xs="/c/assets/96.rKC-z4z-.gif",$s="/c/assets/97.BJE3xxuT.gif",Hs="/c/assets/98.DJOq__O0.gif",Ks="/c/assets/99.BDt_0fRR.png",Qs="/c/assets/100.Bo5OawW8.png",Ys="/c/assets/101.sUWUfl5e.png",js="/c/assets/102.BK4jpnB8.png",Js="/c/assets/103.EF8vdbSq.png",Zs="/c/assets/104.DjxdA_Co.png",sa="/c/assets/105.sp68q1hh.gif",aa="/c/assets/106.DhMRyxNN.gif",ia="/c/assets/107.CdcLZM0R.gif",na="/c/assets/108.D-5AsTTr.png",la="/c/assets/109.2RQKmAUa.png",ea="/c/assets/110.CI4xX8SM.png",pa="/c/assets/111.C8QUZ9v3.gif",ta="/c/assets/112.g5tQfhWz.gif",ra="/c/assets/113.CX0_1NsE.gif",ca="/c/assets/114.BrFJeoK6.png",ha="/c/assets/115.58TjSSjH.gif",da="/c/assets/116.DmiFIqcy.svg",oa="/c/assets/117.BasYWvkM.png",ka="/c/assets/118.CjFxJpBr.png",ua="/c/assets/119.Dm5Jspvp.gif",ga="/c/assets/120.1krCh8cy.gif",ba="/c/assets/121.DOSyahv7.gif",ma="/c/assets/122.BHEvX_FI.gif",Ca="/c/assets/123.DFA0g1_d.gif",Ea="/c/assets/124.D50nO5eA.gif",ya="/c/assets/125.cAvG1Qfq.png",Fa="/c/assets/126.DpakQFWz.gif",_a="/c/assets/127.Bg61j9QI.gif",va="/c/assets/128.DPvD9UCw.gif",Ba="/c/assets/129.Ceu65U_n.gif",fa="/c/assets/130.BWPFRK_8.gif",Aa="/c/assets/131.Ct4TfxPf.gif",Da="/c/assets/132.zsKtx_jV.png",xa="/c/assets/133.3oqDAW0w.png",qa="/c/assets/134.D9TSeAa9.gif",wa="/c/assets/135.CCRgYIVq.png",Sa="/c/assets/136.C1t2T6FV.gif",La="/c/assets/137.Rqa4uHq6.png",za="/c/assets/138.6yQYnjOa.gif",Pa="/c/assets/139.hYR9GiiD.gif",Ta="/c/assets/140.D3b-QIrA.gif",Wa="/c/assets/141.DGbA6qsW.gif",Ma="/c/assets/142.BToOkxf2.gif",Ua="/c/assets/143.C3wekjfY.png",Oa="/c/assets/144.CKuuzuQ_.png",Ia="/c/assets/145.B4r6Nv14.gif",Na="/c/assets/146.CvbhuZNK.png",Ra="/c/assets/147.BPgYm4ZX.gif",Va="/c/assets/148.DH2fri13.gif",Ga="/c/assets/149.CDDIqy03.gif",Xa="/c/assets/150.DCQsqYYC.gif",$a="/c/assets/151.CUjMoXNF.gif",Ha="/c/assets/152.BWbajkuB.gif",Ka="/c/assets/153.C1JnBDr7.png",Qa="/c/assets/154.DwN2p3bo.png",Ya="/c/assets/155.DdSfz-Pf.gif",ja="/c/assets/156.BMocjVt7.gif",Ja="/c/assets/157.BUU-wlaw.gif",Za="/c/assets/158.ssadIF0D.gif",si="/c/assets/159.Dq5lLq9v.gif",ai="/c/assets/160.CJ0_loZ8.gif",ii="/c/assets/161.BPOK9irZ.gif",ni="/c/assets/162.B1eB55rC.gif",li="/c/assets/163.VIcU-hkN.gif",ei="/c/assets/164.BM9viRZJ.gif",pi="/c/assets/165.DpMBA1SK.gif",ti="/c/assets/166.DgXuU6LF.png",ri="/c/assets/167.Dnw73s2h.gif",ci="/c/assets/168.SnFXRSaL.png",hi="/c/assets/169.DnoEqOjV.png",di="/c/assets/170.BJBdn7pw.png",oi="/c/assets/171.DXtQWYsM.gif",ki="/c/assets/172.hcERTQkN.gif",ui="/c/assets/173.CioraEUv.png",gi="/c/assets/174.kWHWwnc_.png",Di=JSON.parse('{"title":"第一章:开发环境的安装和配置(⭐)","description":"","frontmatter":{},"headers":[],"relativePath":"notes/01_c-basic/01_xdx/index.md","filePath":"notes/01_c-basic/01_xdx/index.md","lastUpdated":1724640802000}'),bi={name:"notes/01_c-basic/01_xdx/index.md"},mi=n('<h1 id="第一章-开发环境的安装和配置-⭐" tabindex="-1">第一章:开发环境的安装和配置(⭐) <a class="header-anchor" href="#第一章-开发环境的安装和配置-⭐" aria-label="Permalink to &quot;第一章:开发环境的安装和配置(⭐)&quot;"></a></h1><h2 id="_1-1-什么是编译器" tabindex="-1">1.1 什么是编译器? <a class="header-anchor" href="#_1-1-什么是编译器" aria-label="Permalink to &quot;1.1 什么是编译器?&quot;"></a></h2><ul><li>要开发 C/C++ 程序,需要安装 C/C++ 编译器,目前有两种主流实现,即: <ul><li>GCCGNU Compiler Collection全平台实现即支持 Windows、MacOS、Linux 等。</li><li>MSVCMicrosoft Visual C++):只支持 Windows 系统。</li></ul></li><li>GCC 在 Windows 上的版本有很多,如:<a href="https://www.mingw-w64.org/" target="_blank" rel="noreferrer">MinGW-w64</a>、<a href="https://cygwin.com/" target="_blank" rel="noreferrer">Gygwin</a>、<a href="https://www.msys2.org/" target="_blank" rel="noreferrer">MSYS2</a>。它们之间的区别,如下所示:</li></ul><table tabindex="0"><thead><tr><th>特性</th><th>MinGW-w64</th><th>Cygwin</th><th>MSYS2</th></tr></thead><tbody><tr><td><strong>简介</strong></td><td>Minimalist GNU for Windows</td><td>POSIX 兼容环境和工具集</td><td>结合了 MinGW 和 Cygwin 的工具集</td></tr><tr><td><strong>编译器</strong></td><td>提供 GCC 编译器</td><td>提供 POSIX 兼容环境,包含大量工具</td><td>提供 MinGW-w64 工具链和 Cygwin 环境</td></tr><tr><td><strong>生成文件</strong></td><td>Windows 原生可执行文件</td><td>POSIX 兼容的可执行文件</td><td>可以生成 Windows 原生可执行文件或 POSIX 文件</td></tr><tr><td><strong>依赖</strong></td><td>无需额外依赖</td><td>依赖 Cygwin DLL</td><td>根据使用工具链决定MinGW-w64 无依赖Cygwin 有依赖)</td></tr><tr><td><strong>工具和库</strong></td><td>基本的编译工具</td><td>丰富的 Unix 工具和库</td><td>丰富的工具和库,强大的包管理系统</td></tr><tr><td><strong>性能</strong></td><td>性能较好</td><td>可能较低,因为通过兼容层调用系统</td><td>取决于使用的工具链MinGW-w64 性能较好)</td></tr><tr><td><strong>复杂度</strong></td><td>简单易用</td><td>设置和使用相对复杂</td><td>较为灵活,复杂度介于 MinGW-w64 和 Cygwin 之间</td></tr><tr><td><strong>适用场景</strong></td><td>开发 Windows 原生应用</td><td>运行和开发 Unix 程序</td><td>混合使用 Unix 工具和开发 Windows 原生应用</td></tr><tr><td><strong>优点</strong></td><td>轻量级,直接生成 Windows 应用</td><td>完整的 POSIX 兼容环境,丰富的工具</td><td>灵活的环境,强大的包管理系统</td></tr><tr><td><strong>缺点</strong></td><td>工具和库较少</td><td>生成文件依赖 Cygwin DLL性能可能较低</td><td>环境较大,占用更多空间,复杂性比 MinGW-w64 高</td></tr></tbody></table><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><ul><li>① MinGW-w64 、Cygwin 以及 MSYS2 任选其一安装即可。</li><li>② 目前的 Win10 和 Win11 版本支持 WSL2 Windows Sub Linux 2 ,即 Windows 的子系统 Linux可以实现在 Windows 系统上安装一个 Linux ,然后再运行 Linux 中的 GCC 工具链。</li><li>③ 本人的操作系统是 Win11 ,安装和配置都将以该系统为基础作为演示,后续不再赘述!!!</li></ul></div><h2 id="_1-2-编译器的安装和配置" tabindex="-1">1.2 编译器的安装和配置 <a class="header-anchor" href="#_1-2-编译器的安装和配置" aria-label="Permalink to &quot;1.2 编译器的安装和配置&quot;"></a></h2><h3 id="_1-2-2-mingw-w64-的安装和配置" tabindex="-1">1.2.2 MinGW-w64 的安装和配置 <a class="header-anchor" href="#_1-2-2-mingw-w64-的安装和配置" aria-label="Permalink to &quot;1.2.2 MinGW-w64 的安装和配置&quot;"></a></h3><h4 id="_1-2-2-1-安装" tabindex="-1">1.2.2.1 安装 <a class="header-anchor" href="#_1-2-2-1-安装" aria-label="Permalink to &quot;1.2.2.1 安装&quot;"></a></h4><ul><li>下载到本地:略。</li></ul><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><p>下载地址在<a href="https://github.com/niXman/mingw-builds-binaries/releases/download/13.2.0-rt_v11-rev1/x86_64-13.2.0-release-posix-seh-ucrt-rt_v11-rev1.7z" target="_blank" rel="noreferrer">这里</a>。</p></div><p><img src="'+l+'" alt="" loading="lazy"></p><ul><li>解压到指定的目录,即:</li></ul><p><img src="'+e+'" alt="" loading="lazy"></p><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><p>本人的解压目录是:<code>D:\\develop\\mingw64</code> 。</p></div><h4 id="_1-2-2-2-配置-path-环境变量" tabindex="-1">1.2.2.2 配置 path 环境变量 <a class="header-anchor" href="#_1-2-2-2-配置-path-环境变量" aria-label="Permalink to &quot;1.2.2.2 配置 path 环境变量&quot;"></a></h4><ul><li>配置环境变量,以便任意目录都可以执行 gcc 命令,即:</li></ul><p><img src="'+p+'" alt="" loading="lazy"></p><p><img src="'+t+'" alt="" loading="lazy"></p><p><img src="'+r+'" alt="" loading="lazy"></p><p><img src="'+c+'" alt="" loading="lazy"></p><p><img src="'+h+'" alt="" loading="lazy"></p><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><p>因为,本人安装 MinGW-w64 的目录是 <code>D:\\develop\\mingw64</code>,所以本人需要配置的 path 环境变量就是<code>D:\\develop\\mingw64\\bin</code>。</p></div><p><img src="'+d+'" alt="" loading="lazy"></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;">gcc</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --version</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><p><img src="'+o+'" alt="" loading="lazy"></p><h3 id="_1-2-3-cygwin-的安装和配置" tabindex="-1">1.2.3 Cygwin 的安装和配置 <a class="header-anchor" href="#_1-2-3-cygwin-的安装和配置" aria-label="Permalink to &quot;1.2.3 Cygwin 的安装和配置&quot;"></a></h3><h4 id="_1-2-3-1-安装" tabindex="-1">1.2.3.1 安装 <a class="header-anchor" href="#_1-2-3-1-安装" aria-label="Permalink to &quot;1.2.3.1 安装&quot;"></a></h4><ul><li>下载到本地:略。</li></ul><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><p>下载地址在<a href="https://www.cygwin.com/install.html" target="_blank" rel="noreferrer">这里</a>。</p></div><p><img src="'+k+'" alt="" loading="lazy"></p><ul><li>点击安装:</li></ul><p><img src="'+u+'" alt="" loading="lazy"></p><p><img src="'+g+'" alt="" loading="lazy"></p><p><img src="'+b+'" alt="" loading="lazy"></p><p><img src="'+m+'" alt="" loading="lazy"></p><p><img src="'+C+'" alt="" loading="lazy"></p><p><img src="'+E+'" alt="" loading="lazy"></p><p><img src="'+y+'" alt="" loading="lazy"></p><p><img src="'+F+'" alt="" loading="lazy"></p><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><p>默认是最小化安装,没有 GCC需要选择 gcc-core、gcc-g++、make、gdb、binutils 。</p></div><p><img src="'+_+'" alt="" loading="lazy"></p><p><img src="'+v+'" alt="" loading="lazy"></p><p><img src="'+B+'" alt="" loading="lazy"></p><p><img src="'+f+'" alt="" loading="lazy"></p><p><img src="'+A+'" alt="" loading="lazy"></p><p><img src="'+D+'" alt="" loading="lazy"></p><p><img src="'+x+'" alt="" loading="lazy"></p><p><img src="'+q+'" alt="" loading="lazy"></p><p><img src="'+w+'" alt="" loading="lazy"></p><h4 id="_1-2-3-2-配置-path-环境变量" tabindex="-1">1.2.3.2 配置 path 环境变量 <a class="header-anchor" href="#_1-2-3-2-配置-path-环境变量" aria-label="Permalink to &quot;1.2.3.2 配置 path 环境变量&quot;"></a></h4><ul><li>和 <code>1.2.2.2 配置 path 环境变量</code> 步骤相同:略。</li></ul><h3 id="_1-2-4-msys2-推荐" tabindex="-1">1.2.4 MSYS2推荐 <a class="header-anchor" href="#_1-2-4-msys2-推荐" aria-label="Permalink to &quot;1.2.4 MSYS2推荐&quot;"></a></h3><h4 id="_1-2-4-1-安装" tabindex="-1">1.2.4.1 安装 <a class="header-anchor" href="#_1-2-4-1-安装" aria-label="Permalink to &quot;1.2.4.1 安装&quot;"></a></h4><ul><li>下载到本地:略。</li></ul><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><p>下载地址在<a href="https://www.msys2.org/" target="_blank" rel="noreferrer">这里</a>。</p></div><p><img src="'+S+'" alt="" loading="lazy"></p><ul><li>点击安装:</li></ul><p><img src="'+L+'" alt="" loading="lazy"></p><p><img src="'+z+'" alt="" loading="lazy"></p><p><img src="'+P+'" alt="" loading="lazy"></p><p><img src="'+T+'" alt="" loading="lazy"></p><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><p>可能很多人,会遇到安装到 <code>50%</code> 就一直卡死在那边,不用慌,关闭它,再次重新安装即可。</p></div><p><img src="'+W+'" alt="" loading="lazy"></p><ul><li>出现命令终端:</li></ul><p><img src="'+M+`" alt="" loading="lazy"></p><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><p>如果没有出现命令终端,也不要慌,去 Win11 操作系统的<code>开始</code>菜单,那边找一下,就能找到。</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;">sed</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -i</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> \\</span></span>
<span class="line"><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;s#https\\?://mirror.msys2.org/#https://mirrors.tuna.tsinghua.edu.cn/msys2/#g&quot;</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> \\</span></span>
<span class="line"><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> /etc/pacman.d/mirrorlist</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">*</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><p><img src="`+U+'" alt="" loading="lazy"></p><ul><li>安装 gcc 等相关开发包:</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;">pacman</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -Syu</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --noconfirm</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><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;">pacman</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -Sy</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> base-devel</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --noconfirm</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><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;">pacman</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -Sy</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> mingw-w64-ucrt-x86_64-toolchain</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --noconfirm</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 安装开发 gcc 相关工具链</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>也许,你会看到其他人的安装命令是 <code>pacman -Sy mingw-w64-x86_64-toolchain --noconfirm</code>,此处解释下两者的区别:</p><ul><li>① <code>mingw-w64-x86_64-toolchain</code> 使用更传统的 MSVCRT适合需要经典 MinGW 环境的项目或依赖较老 C 运行时的应用。</li><li>② <code>mingw-w64-ucrt-x86_64-toolchain</code> 使用 Microsoft 的 UCRT更适合现代 Windows 开发,提供更好的兼容性和性能。</li></ul></div><p><img src="'+O+'" alt="" loading="lazy"></p><h4 id="_1-2-4-2-配置-path-环境变量" tabindex="-1">1.2.4.2 配置 path 环境变量 <a class="header-anchor" href="#_1-2-4-2-配置-path-环境变量" aria-label="Permalink to &quot;1.2.4.2 配置 path 环境变量&quot;"></a></h4><ul><li>和 <code>1.2.2.2 配置 path 环境变量</code> 步骤相同:略。</li></ul><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><p>本人需要配置的 path 环境变量是 <code>C:\\msys64\\ucrt64\\bin</code> 。</p></div><h2 id="_1-3-什么是-ide-集成开发环境" tabindex="-1">1.3 什么是 IDE集成开发环境 <a class="header-anchor" href="#_1-3-什么是-ide-集成开发环境" aria-label="Permalink to &quot;1.3 什么是 IDE集成开发环境&quot;"></a></h2><ul><li><p>在实际开发中,除了<code>编译器</code>是必须安装的工具之外,我们往往还需要很多其他的辅助软件,如下所示:</p><ul><li>编辑器:用来编写代码,并且给代码着色,以方便阅读。</li><li>代码提示器:输入部分代码,即可提示全部代码,加速代码的编写过程。</li><li>调试器:观察程序的每一个运行步骤,发现程序的逻辑错误。</li><li>项目管理工具:对程序涉及到的所有资源进行管理,包括:源文件、图片、视频、第三方库等。</li><li>漂亮的界面:各种按钮、面板、菜单、窗口等控件整齐排布,操作更方便。</li></ul></li><li><p>这些工具通常被打包在一起统一安装和发布Visual Studio、CLion 以及 VS Code 通常统称为集成开发环境IDEIntegrated Development Environment。</p></li></ul><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><ul><li>① IDE集成开发环境就是一系列开发工具的组合套装。这就好比台式机核心部件是主机。主机就相当于 IDE 的代码编辑器和编译器有了它们开发者就可以进行基本的编程工作。然而正如我们在购买台式机时通常还会附带显示器、键盘、鼠标、U盘、摄像头等外围设备IDE 也同样提供了一系列额外的工具和插件,比如:调试器、版本控制集成、代码补全、代码重构工具等。这些“外围设备”让开发过程更加高效、直观,并且能够满足更多的开发需求,使得 IDE 成为一个完整的开发环境。</li><li>② 严格来讲, VS Code 属于编辑器,而不是 IDE但是可以通过安装各种插件来完成 IDE 的功能;而 Visual Studio 和 CLion 属于 IDE。</li><li>③ 在实际开发中,使用 IDE 进行编程并不是一种非常丢人的事情。而使用编辑器,如:<code>记事本</code>、<code>vi/vim</code> 等,进行编程,也并不是一件非常骄傲的事情。可能有些人会在网上发布这样的言论:“学习编程,刚开始需要使用<code>记事本</code>或 <code>vi/vim</code>等简单的编辑器软件,不要使用 IDE ”,目的可能是为了让初学者熟悉编程的基础概念和语法,并避免依赖 IDE 的辅助功能。但是,这种方法或许可以起到锻炼基础技能的功能,但这并不意味着 IDE 就不适合初学者。事实上,许多 IDE 还提供了初学者友好的界面和工具,可以帮助新手更快地入门和理解编程。</li></ul></div><h2 id="_1-4-ide-的安装和配置" tabindex="-1">1.4 IDE 的安装和配置 <a class="header-anchor" href="#_1-4-ide-的安装和配置" aria-label="Permalink to &quot;1.4 IDE 的安装和配置&quot;"></a></h2><h3 id="_1-4-1-clion" tabindex="-1">1.4.1 CLion <a class="header-anchor" href="#_1-4-1-clion" aria-label="Permalink to &quot;1.4.1 CLion&quot;"></a></h3><h4 id="_1-4-1-1-概述" tabindex="-1">1.4.1.1 概述 <a class="header-anchor" href="#_1-4-1-1-概述" aria-label="Permalink to &quot;1.4.1.1 概述&quot;"></a></h4><ul><li><a href="https://www.jetbrains.com/clion/" target="_blank" rel="noreferrer">CLion</a> 是一款由 JetBrains 推出的跨平台 C/C++ 集成开发环境IDE它具有智能编辑器、CMake 构建支持、调试器、单元测试、代码分析等功能,可以极大提高 C/C++ 开发效率。</li></ul><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><ul><li>① 本次,演示的 CLion 的安装版本是 2024.1.4 ,后续版本可能会更新,但是操作几乎不会发生太多变化!!!</li><li>② CLion 作为一个 IDE本身就携带了各个平台操作系统的 C 语言编译器Windows 中就是 MinGW但是CLion 中自带的 C 语言编译器版本可能并非我们实际开发所想要的,这也是在 Windows 中,为什么推荐使用 MSYS2 的原因所在。</li></ul></div><h4 id="_1-4-1-2-安装" tabindex="-1">1.4.1.2 安装 <a class="header-anchor" href="#_1-4-1-2-安装" aria-label="Permalink to &quot;1.4.1.2 安装&quot;"></a></h4><ul><li>鼠标双击,进入安装:</li></ul><p><img src="'+I+'" alt="" loading="lazy"></p><ul><li>下一步:</li></ul><p><img src="'+N+'" alt="" loading="lazy"></p><ul><li>下一步:</li></ul><p><img src="'+R+'" alt="" loading="lazy"></p><ul><li>下一步:</li></ul><p><img src="'+V+'" alt="" loading="lazy"></p><ul><li>安装:</li></ul><p><img src="'+G+'" alt="" loading="lazy"></p><ul><li>安装完成:</li></ul><p><img src="'+X+'" alt="" loading="lazy"></p><h4 id="_1-4-1-3-配置" tabindex="-1">1.4.1.3 配置 <a class="header-anchor" href="#_1-4-1-3-配置" aria-label="Permalink to &quot;1.4.1.3 配置&quot;"></a></h4><ul><li>打开 CLion </li></ul><p><img src="'+$+'" alt="" loading="lazy"></p><ul><li>安装中文插件(可选):</li></ul><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><p>对于以中文、韩语和日语为母语的开发者CLion 在 2024.2 之后的版本就绑定了本地化插件,即不需要再安装本地化插件了。</p></div><p><img src="'+H+'" alt="" loading="lazy"></p><p><img src="'+K+'" alt="" loading="lazy"></p><ul><li>配置新 UI </li></ul><p><img src="'+Q+'" alt="" loading="lazy"></p><p><img src="'+Y+'" alt="" loading="lazy"></p><ul><li>新 UI 配置自定义字体(可选):</li></ul><p><img src="'+j+'" alt="" loading="lazy"></p><ul><li>配置<code>自动保存</code>功能:</li></ul><p><img src="'+J+'" alt="" loading="lazy"></p><ul><li>配置<code>文件编码</code> 为 UTF-8</li></ul><p><img src="'+Z+'" alt="" loading="lazy"></p><ul><li>配置<code>控制台编码</code>为 UTF-8</li></ul><p><img src="'+ss+'" alt="" loading="lazy"></p><ul><li>配置<code>显示方法分隔符</code>功能:</li></ul><p><img src="'+as+'" alt="" loading="lazy"></p><ul><li>配置<code>编辑器</code>的字体(可选):</li></ul><p><img src="'+is+'" alt="" loading="lazy"></p><ul><li>检测 GCC 工具链是否安装成功:</li></ul><p><img src="'+ns+'" alt="" loading="lazy"></p><h3 id="_1-4-2-vs-code" tabindex="-1">1.4.2 VS Code <a class="header-anchor" href="#_1-4-2-vs-code" aria-label="Permalink to &quot;1.4.2 VS Code&quot;"></a></h3><h4 id="_1-4-2-1-概述" tabindex="-1">1.4.2.1 概述 <a class="header-anchor" href="#_1-4-2-1-概述" aria-label="Permalink to &quot;1.4.2.1 概述&quot;"></a></h4><ul><li><a href="https://code.visualstudio.com/" target="_blank" rel="noreferrer">Visual Studio Code (VS Code)</a> 是一个免费的开源代码编辑器,适用于 Windows、MacOS 和 Linux 平台。它支持语法高亮、智能代码补全IntelliSense、内置调试工具和Git集成。用户可以通过扩展来添加更多功能支持新的编程语言、主题和调试工具。VS Code 还支持在微软 Azure 上进行部署和托管,适用于各种编程语言和框架。</li></ul><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><ul><li>① Visual Studio Code 需要安装对应的插件,才能运行 C/C++ 代码。</li><li>② Visual Studio Code 除了开源免费的优点之外,还有一个优点就是插件巨多(几乎所有主流的编程语言都提供有对应的插件),这也是很多程序员喜欢使用它的原因。</li></ul></div><h4 id="_1-4-2-2-安装" tabindex="-1">1.4.2.2 安装 <a class="header-anchor" href="#_1-4-2-2-安装" aria-label="Permalink to &quot;1.4.2.2 安装&quot;"></a></h4><ul><li>鼠标双击,进入安装:</li></ul><p><img src="'+ls+'" alt="" loading="lazy"></p><ul><li>同意协议:</li></ul><p><img src="'+es+'" alt="" loading="lazy"></p><ul><li>下一步:</li></ul><p><img src="'+ps+'" alt="" loading="lazy"></p><ul><li>下一步:</li></ul><p><img src="'+ts+'" alt="" loading="lazy"></p><ul><li>下一步:</li></ul><p><img src="'+rs+'" alt="" loading="lazy"></p><ul><li>安装:</li></ul><p><img src="'+cs+'" alt="" loading="lazy"></p><ul><li>安装过程:</li></ul><p><img src="'+hs+'" alt="" loading="lazy"></p><ul><li>安装完成:</li></ul><p><img src="'+ds+'" alt="" loading="lazy"></p><h4 id="_1-4-2-3-配置" tabindex="-1">1.4.2.3 配置 <a class="header-anchor" href="#_1-4-2-3-配置" aria-label="Permalink to &quot;1.4.2.3 配置&quot;"></a></h4><ul><li>安装<code>中文</code>插件:</li></ul><p><img src="'+os+'" alt="" loading="lazy"></p><p><img src="'+ks+'" alt="" loading="lazy"></p><ul><li>安装 <code>C/C++</code> 插件:</li></ul><p><img src="'+us+'" alt="" loading="lazy"></p><p><img src="'+gs+'" alt="" loading="lazy"></p><h3 id="_1-4-3-microsoft-visual-studio" tabindex="-1">1.4.3 Microsoft Visual Studio <a class="header-anchor" href="#_1-4-3-microsoft-visual-studio" aria-label="Permalink to &quot;1.4.3 Microsoft Visual Studio&quot;"></a></h3><h4 id="_1-4-3-1-概述" tabindex="-1">1.4.3.1 概述 <a class="header-anchor" href="#_1-4-3-1-概述" aria-label="Permalink to &quot;1.4.3.1 概述&quot;"></a></h4><ul><li><a href="https://visualstudio.microsoft.com/" target="_blank" rel="noreferrer">Visual Studio</a>(简称 VS是由微软公司发布的集成开发环境。它包括了整个软件生命周期中所需要的大部分工具UML 工具、代码管控工具、项目版本控制 Git 等。</li><li>Visual Studio 支持 C/C++、C#、F#、VB 等多种程序语言的开发和测试,可以用于生成 Web 应用程序,也可以生成桌面应用程序,功能十分强大,但下载和安装很可能耗时数小时,还可能会塞满磁盘。</li><li>Visual Studio 有三种版本:社区版(免费,不支持企业使用),专业版(收费)和企业版(收费)。企业版拥有面向架构师的功能、高级调试和测试,这些功能是另外两种版本所没有的。</li><li>Visual Studio 旨在成为世界上最好的 IDE集成开发环境号称“宇宙第一强大 IDE”。</li></ul><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><p>本次,演示的 Visual Studio 的安装版本是 Visual Studio 2022 ,后续版本可能会更新,但是操作几乎不会发生太多变化!!!</p></div><h4 id="_1-4-3-2-安装" tabindex="-1">1.4.3.2 安装 <a class="header-anchor" href="#_1-4-3-2-安装" aria-label="Permalink to &quot;1.4.3.2 安装&quot;"></a></h4><ul><li>鼠标双击,进入安装:</li></ul><p><img src="'+bs+'" alt="" loading="lazy"></p><ul><li>继续:</li></ul><p><img src="'+ms+'" alt="" loading="lazy"></p><ul><li>等待:</li></ul><p><img src="'+Cs+'" alt="" loading="lazy"></p><ul><li>工作负荷(使用 C++ 的桌面开发):</li></ul><p><img src="'+Es+'" alt="" loading="lazy"></p><ul><li>单个组件:</li></ul><p><img src="'+ys+'" alt="" loading="lazy"></p><ul><li>语言包:</li></ul><p><img src="'+Fs+'" alt="" loading="lazy"></p><ul><li>安装位置(修改默认的安装位置):</li></ul><p><img src="'+_s+'" alt="" loading="lazy"></p><ul><li>如果不是第一次安装,可能会出现<code>共享组件、工具和 SDK</code>不可以修改,即:</li></ul><p><img src="'+vs+'" alt="" loading="lazy"></p><ul><li>此时,就需要打开<code>注册表编辑器</code>,将如下图中的除了第一个选项,全部删除,然后关闭再重新安装,即:</li></ul><p><img src="'+Bs+'" alt="" loading="lazy"></p><ul><li>开始安装:</li></ul><p><img src="'+fs+'" alt="" loading="lazy"></p><ul><li>安装中:</li></ul><p><img src="'+As+'" alt="" loading="lazy"></p><ul><li>安装完成,然后关闭:</li></ul><p><img src="'+Ds+'" alt="" loading="lazy"></p><h4 id="_1-4-3-3-配置" tabindex="-1">1.4.3.3 配置 <a class="header-anchor" href="#_1-4-3-3-配置" aria-label="Permalink to &quot;1.4.3.3 配置&quot;"></a></h4><ul><li>在开始菜单处,启动 VS </li></ul><p><img src="'+xs+'" alt="" loading="lazy"></p><ul><li>登录或跳过该选项(有微软账号就注册,没有就暂时跳过):</li></ul><p><img src="'+qs+'" alt="" loading="lazy"></p><ul><li>继续:</li></ul><p><img src="'+ws+'" alt="" loading="lazy"></p><ul><li>注册 VS </li></ul><p><img src="'+Ss+'" alt="" loading="lazy"></p><ul><li>填写注册码:</li></ul><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><ul><li>① Pro 版本:<code>TD244-P4NB7-YQ6XK-Y8MMM-YWV2J</code></li><li>② Enterprise 版本:<code>VHF9H-NXBBB-638P6-6JHCY-88JWH</code></li></ul></div><p><img src="'+Ls+'" alt="" loading="lazy"></p><p><img src="'+zs+'" alt="" loading="lazy"></p><h2 id="_1-5-什么是工程-项目" tabindex="-1">1.5 什么是工程/项目 <a class="header-anchor" href="#_1-5-什么是工程-项目" aria-label="Permalink to &quot;1.5 什么是工程/项目 &quot;"></a></h2><h3 id="_1-5-1-概述" tabindex="-1">1.5.1 概述 <a class="header-anchor" href="#_1-5-1-概述" aria-label="Permalink to &quot;1.5.1 概述&quot;"></a></h3><ul><li>一个真正的软件往往包含多项功能,每一项功能都需要几十行、几千行甚至几万行的代码,如果我们将这些代码都放到一个源文件中,不但打开的速度极慢,代码的编写和维护也会变得非常困难。</li><li>在实际开发中,随着软件规模的增加,代码的复杂性也会显著提升,为了提高代码的易读性、维护性等,程序员会将代码按照功能分别放到不同的源文件中。</li></ul><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><p>需要说明的是,一个真正的软件除了源代码之外,往往还会包括图片、视频、音频、库(框架)等其它资源,这些也是一个个的文件。</p></div><ul><li>为了有效的管理这些种类繁杂、数目众多的文件,我们会将这些文件按照功能放到不同的目录中进行统一管理,并且这个目录下只存放与当前程序有关的资源。其实,这就是工程或项目。</li></ul><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><ul><li>许多 IDE 都提供了工程或项目的概念,其目的就是为了帮助开发者合理的管理软件开发中所需要的资源,如:图片、视频、音频、库(框架)等。</li></ul><p><img src="'+Ps+'" alt="" loading="lazy"></p><h3 id="_1-5-2-工程类型-项目类型" tabindex="-1">1.5.2 工程类型/项目类型 <a class="header-anchor" href="#_1-5-2-工程类型-项目类型" aria-label="Permalink to &quot;1.5.2 工程类型/项目类型&quot;"></a></h3><ul><li><p>程序或软件是一个非常宽泛的概念,它可以细分为很多种类,如下所示:</p><ul><li>控制台程序Console Application控制台程序是一种不具备图形用户界面的程序它通过文本方式与用户交互通常运行在命令行窗口黑底白字的终端Unix/Linux 中的<code>ls</code>命令、Windows 中的<code>cmd.exe</code>等。</li><li>GUI 程序Graphical User Interface ProgramGUI 程序是一种具有图形用户界面的程序通过窗口、按钮、菜单等图形控件与用户交互微信、QQ 等。</li><li>静态库和动态库:不单独出现,而是作为其它程序的一个组成部分,普通用户很难接触到它们。 <ul><li>静态库指的是在编译时包含到程序中的库,程序不依赖外部文件运行,如:在 C/C++ 中,静态库通常以<code>.lib</code>Windows或<code>.a</code>Unix/Linux为扩展名。</li><li>动态库指的是在运行时加载的库,允许多个程序共享,并且程序在运行时依赖这些库,如: 在Windows中动态库通常以<code>.dll</code>为扩展名;在 Unix/Linux 中,以<code>.so</code>为扩展名。</li></ul></li></ul></li><li><p>不同类型的程序控制台程序、GUI 程序、静态库、动态库等)需要不同的配置和文件结构,因此在 IDE 中创建项目时,选择正确的工程类型非常重要。不同的工程类型决定了 IDE 为我们生成的初始文件、目录结构,以及预设的一些编译和链接参数。</p></li></ul><div class="important custom-block github-alert"><p class="custom-block-title">IMPORTANT</p><p></p><ul><li>① 控制台程序适合初学者,因为它更简单,没有复杂的界面元素,开发时可以专注于逻辑和代码本身。</li><li>② 而 GUI 程序则涉及到用户界面设计和事件驱动编程,更适合有一定编程基础的人进行学习和开发。</li></ul></div><h1 id="第二章-c-语言入门-helloworld-⭐" tabindex="-1">第二章C 语言入门HelloWorld <a class="header-anchor" href="#第二章-c-语言入门-helloworld-⭐" aria-label="Permalink to &quot;第二章C 语言入门HelloWorld&quot;"></a></h1><h2 id="_2-1-手动版" tabindex="-1">2.1 手动版 <a class="header-anchor" href="#_2-1-手动版" aria-label="Permalink to &quot;2.1 手动版&quot;"></a></h2><ul><li>① 新建一个 <code>HelloWorld.c</code> 的文件:</li></ul><p><img src="'+Ts+`" alt="" loading="lazy"></p><ul><li>② 通过<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;"> &lt;stdio.h&gt;</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:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;Hello World&quot;</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><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="`+Ws+'" alt="" loading="lazy"></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="'+Ms+'" alt="" loading="lazy"></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="'+Us+'" alt="" loading="lazy"></p><h2 id="_2-2-vs-code-版" tabindex="-1">2.2 VS Code 版 <a class="header-anchor" href="#_2-2-vs-code-版" aria-label="Permalink to &quot;2.2 VS Code 版&quot;"></a></h2><ul><li>① 新建一个<code>空</code>文件夹(目录),用于存放代码:</li></ul><p><img src="'+Os+'" alt="" loading="lazy"></p><ul><li>② 通过 <code>vscode</code> 打开该目录:</li></ul><p><img src="'+Is+'" alt="" loading="lazy"></p><ul><li>③ 在 <code>vscode</code> 中新建 <code>HelloWorld.c</code> 文件:</li></ul><p><img src="'+Ns+'" alt="" loading="lazy"></p><ul><li>④ 设置 VSCode 中 C/C++ 的代码格式为行尾风格(可选):</li></ul><p><img src="'+Rs+'" alt="" loading="lazy"></p><p><img src="'+Vs+`" alt="" loading="lazy"></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;"> &lt;stdio.h&gt;</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:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;Hello World&quot;</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><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="`+Gs+'" alt="" loading="lazy"></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="'+Xs+'" alt="" loading="lazy"></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="'+$s+'" alt="" loading="lazy"></p><ul><li>⑧ 安装 Code Runner 插件(步骤略),实现右键直接编译执行(可选):</li></ul><p><img src="'+Hs+'" alt="" loading="lazy"></p><h2 id="_2-3-vs-版" tabindex="-1">2.3 VS 版 <a class="header-anchor" href="#_2-3-vs-版" aria-label="Permalink to &quot;2.3 VS 版&quot;"></a></h2><ul><li>① 新建空项目:</li></ul><p><img src="'+Ks+'" alt="" loading="lazy"></p><p><img src="'+Qs+'" alt="" loading="lazy"></p><p><img src="'+Ys+'" alt="" loading="lazy"></p><p><img src="'+js+'" alt="" loading="lazy"></p><ul><li>② 打开<code>解决方案资源管理器</code></li></ul><p><img src="'+Js+'" alt="" loading="lazy"></p><p><img src="'+Zs+'" alt="" loading="lazy"></p><ul><li>③ 新建 <code>HelloWorld.c</code> 文件:</li></ul><p><img src="'+sa+`" alt="" loading="lazy"></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;"> &lt;stdio.h&gt;</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:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;Hello World&quot;</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><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="`+aa+'" alt="" loading="lazy"></p><ul><li>⑤ 编译和执行:</li></ul><p><img src="'+ia+'" alt="" loading="lazy"></p><h2 id="_2-4-clion-版" tabindex="-1">2.4 CLion 版 <a class="header-anchor" href="#_2-4-clion-版" aria-label="Permalink to &quot;2.4 CLion 版&quot;"></a></h2><ul><li>① 新建空项目:</li></ul><p><img src="'+na+'" alt="" loading="lazy"></p><p><img src="'+la+'" alt="" loading="lazy"></p><p><img src="'+ea+`" alt="" loading="lazy"></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;"> &lt;stdio.h&gt;</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:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;Hello World&quot;</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><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="`+pa+'" alt="" loading="lazy"></p><ul><li>③ 编译和运行:</li></ul><p><img src="'+ta+'" alt="" loading="lazy"></p><ul><li>④ 默认情况下,一个项目只能有一个 c 源文件包含 main 函数,但是 CLion 可以有多个,如下:</li></ul><p><img src="'+ra+'" alt="" loading="lazy"></p><ul><li>⑤ 如果之后,有中文乱码问题,那么请做如下步骤:</li></ul><p><img src="'+ca+'" alt="" loading="lazy"></p><p><img src="'+ha+`" alt="" loading="lazy"></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>
<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="第三章-五花八门的-c-语言编译器-⭐" tabindex="-1">第三章:五花八门的 C 语言编译器(⭐) <a class="header-anchor" href="#第三章-五花八门的-c-语言编译器-⭐" aria-label="Permalink to &quot;第三章:五花八门的 C 语言编译器(⭐)&quot;"></a></h1><h2 id="_3-1-概述" tabindex="-1">3.1 概述 <a class="header-anchor" href="#_3-1-概述" aria-label="Permalink to &quot;3.1 概述&quot;"></a></h2><ul><li>由于 C 语言的历史比较久,而且早期没有规范,整个计算机产业也都处于拓荒的年代,所以就涌现了很多款 C 语言编译器,它们各有特点,适用于不同的平台。</li></ul><h2 id="_3-2-桌面操作系统" tabindex="-1">3.2 桌面操作系统 <a class="header-anchor" href="#_3-2-桌面操作系统" aria-label="Permalink to &quot;3.2 桌面操作系统&quot;"></a></h2><ul><li>目前而言,主流的桌面操作系统就是 Windows、Linux 和 MacOS 。</li><li>对于 Windows 而言,使用的最多的 C/C++ 编译器是 <code>MSVC</code> Microsoft Visual C++),被集成在 Visual Studio 开发环境中,其特点如下: <ul><li>① 兼容性: 与 Windows 操作系统和 Windows API 深度集成,生成的二进制文件为 PE 格式。</li><li>② 调试工具: 提供强大的调试工具Visual Studio Debugger。</li><li>③ 优化: 支持各种编译器优化,特别是针对 Windows 平台的优化。</li><li>④ 库支持: 提供丰富的 Windows 专用库,如:<s>MFCMicrosoft Foundation Class Library</s>。</li></ul></li></ul><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><p>MSVC 不开源,我们可以使用 Visual Studio Community 社区版,但是如果想使用 Visual Studio Community 社区版生成出来的应用进行商用,就需要好好阅读微软的许可证和说明书了。</p></div><ul><li>对于 Linux 而言,使用的最多的 C/C++ 编译器是 <code>GCC</code>(支持多种架构和语言),并且很多 Linux 发行版本都将 GCC 作为默认的编译器,其特点如下所示: <ul><li>① 广泛支持: 支持各种 Linux 发行版,是大多数开源项目的默认编译器。</li><li>② 强大的优化: 提供各种编译优化选项,适合多种性能需求的开发。</li><li>③ 丰富的工具链: 和 GDBGNU 调试器、Make、Autoconf 等工具无缝集成。</li></ul></li></ul><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><p>目前而言GCC 已经属于跨平台的项目了,支持 Windows、Linux 和 MacOS ,在 Windows 上 GCC 的移植项目MinGW、Cygwin 以及 MSYS2其差别如下所示</p><ul><li>① MinGW 提供了 GCC 编译器的 Windows 版本,可以生成 Windows 兼容的本地代码。</li><li>② Cygwin 是一个在 Windows 上运行的类 Unix 环境,它提供了一套完整的 POSIX 兼容工具,包括 GCC 编译器。</li><li>③ MSYS2 是一个在 Windows 上运行的轻量级、开源的 Unix-like 环境,它为 Windows 用户提供了类似于 Linux 的开发环境。MSYS2 是 MinGW 和 Cygwin 的后继者,旨在提供更现代化和更强大的开发工具集。</li></ul></div><ul><li>对于 MacOS 而言,使用的最多的 C/C++ 编译器是 <code>Clang/LLVM</code>,其特点如下: <ul><li>① Xcode 集成: 深度集成到 Xcode 中,支持 Apple 的所有平台macOS、iOS、tvOS、watchOS的开发。</li><li>② 优化和兼容: 生成的代码针对 Apple 的硬件进行优化,并兼容 GCC 的大部分功能。</li><li>③ 现代化: Clang 提供了对 C 语言标准的全面支持,并且以其快速的编译速度和易读的错误报告而著称。</li></ul></li></ul><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><p>在 MacOS 中,尽管 Clang 是默认编译器;但是,也可以 Homebrew 等包管理器来安装 GCC ,以便开发 C/C++ 项目。</p></div><h2 id="_3-3-嵌入式系统" tabindex="-1">3.3 嵌入式系统 <a class="header-anchor" href="#_3-3-嵌入式系统" aria-label="Permalink to &quot;3.3 嵌入式系统&quot;"></a></h2><ul><li><p>在嵌入式系统开发中,可用的 C 语言编译器以及工具链非常丰富, 有很多是免费或开源的,如下所示:</p><ul><li><code>GCC (GNU Compiler Collection)</code> <ul><li>简介GCC 是最广泛使用的开源编译器集合之一,支持多种处理器架构,包括 ARM、AVR、MIPS、RISC-V 等。</li><li>开源或免费:完全开源且免费,受到广泛的社区支持。</li></ul></li><li><code>Clang/LLVM</code> <ul><li>简介Clang 是基于 LLVM 架构的开源编译器,支持多种架构,并且与 GCC 兼容。</li><li>开源或免费:开源且免费,具有快速的编译速度和现代化的代码分析工具。</li></ul></li><li><code>SDCC (Small Device C Compiler)</code> <ul><li>简介: SDCC 是一个开源的跨平台 C 编译器,主要用于 8 位和 16 位微控制器8051、Z80、PIC 等。</li><li>开源或免费:完全开源且免费,适合教育和小型项目开发。</li></ul></li><li><code>MPLAB XC Compilers</code> <ul><li>简介MPLAB XC 是由 Microchip 提供的编译器系列,专门用于其 PIC 和 dsPIC 微控制器。</li><li>开源或免费:提供免费版本(使用标准优化级别),但也有付费版本提供更高级的优化。</li></ul></li><li><code>ARM GCC</code> <ul><li>简介ARM GCC 是 GCC 的一个专门版本,针对 ARM Cortex-M 系列微控制器进行了优化。</li><li>开源或免费:完全开源且免费,广泛用于工业、教育和开源项目中。</li></ul></li><li><code>PlatformIO</code> <ul><li>简介PlatformIO 是一个开源的嵌入式开发生态系统,支持多种开发板、框架和编译器。</li><li>开源或免费:基本功能免费,部分高级功能和插件需要订阅服务。</li></ul></li><li><code>Eclipse</code> <ul><li>简介Eclipse 是一个开源的集成开发环境IDE可以通过插件支持嵌入式开发。</li><li>开源或免费Eclipse 和 GCC 都是开源免费的,适合跨平台开发。</li></ul></li><li><code>Arduino IDE</code> <ul><li>简介Arduino IDE 是一个简单易用的开源开发环境,广泛用于 Arduino 开发板和其他兼容开发板。</li><li>开源或免费:完全开源且免费,非常适合教育和入门级开发。</li></ul></li><li>...</li></ul></li><li><p>这些编译器以及工具链各有优势,开发者应根据目标硬件平台、项目需求和开发环境选择最适合的编译器。</p></li></ul><h2 id="_3-4-c-语言为什么有那么多的编译器" tabindex="-1">3.4 C 语言为什么有那么多的编译器? <a class="header-anchor" href="#_3-4-c-语言为什么有那么多的编译器" aria-label="Permalink to &quot;3.4 C 语言为什么有那么多的编译器?&quot;"></a></h2><ul><li>C 语言并没有一个官方机构,也不属于哪个公司,它只有一个制定标准的委员会,任何其他组织或者个人都可以开发 C 语言的编译器,而这个编译器要遵守哪个 C 语言标准,是 100% 遵守还是部分遵守,并没有强制性的措施,也没有任何约束。</li></ul><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><ul><li>① 各个厂商可以根据自己的利益和喜好来开发编译器。</li><li>② 市场和用户的选择通常是推动编译器开发者遵循标准的主要动力。</li></ul></div><ul><li>并且,不同硬件平台之间也存在差异,这会导致内存管理方式、寄存器、指令集等都有所不同,为了确保 C 语言程序能在这些硬件平台运行,就得针对该平台开发/定制不同的编译器。</li></ul><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><ul><li>① 上述的情况,在单片机和嵌入式领域更加常见。</li><li>② 总体而言C 语言具有开放性,并且要适应不同的硬件平台,这使得不同厂商可以根据自己的需求来进行个性化开发/定制。</li></ul></div><ul><li>这也导致了一个非常棘手的问题,有的编译器遵守较新的 C 语言标准,有的编译器只能遵守较老的 C 语言标准,有的编译器还进行了很多扩展。比如: <ul><li>GCC、LLVM/Clang 更新非常及时,能够支持最新的 C 语言标准。</li><li>MSVC 更新比较缓慢迟迟不能支持新标准例如VC6.0、VS2010 都在使用 C89 标准VS2015 部分支持 C99 标准。</li></ul></li></ul><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><p>微软官方给出的答复:最新的标准已经在 C++ 中支持了C 语言就没必要再重复了。</p></div><ul><li>初学者经常会遇到这种情况,有些代码在 MSVC 下能够正常运行,拿到 GCC 下就不行了,一堆报错信息; 或者反过来,在 GCC 上能运行的代码在 MSVC 下不能运行。这是因为不同的编译器支持的标准不同,每个编译器都进行了自己的扩展,假如你使用了 MSVC 自己的扩展函数,那么拿到 GCC 下肯定是不支持的。</li></ul><div class="important custom-block github-alert"><p class="custom-block-title">IMPORTANT</p><p></p><ul><li>① 在学习的时候,无所谓使用那个 C 语言编译器了。</li><li>② 但是,如果要开发实际项目(开源或商业),最好使用 GCC 编译器,因为其功能最强大、开源、跨平台、免费,支持最新的 C 语言标准。</li></ul></div><h1 id="第四章-注释-⭐" tabindex="-1">第四章:注释(⭐) <a class="header-anchor" href="#第四章-注释-⭐" aria-label="Permalink to &quot;第四章:注释(⭐)&quot;"></a></h1><h2 id="_4-1-概述" tabindex="-1">4.1 概述 <a class="header-anchor" href="#_4-1-概述" aria-label="Permalink to &quot;4.1 概述&quot;"></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="_4-2-单行注释" tabindex="-1">4.2 单行注释 <a class="header-anchor" href="#_4-2-单行注释" aria-label="Permalink to &quot;4.2 单行注释&quot;"></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;"> &lt;stdio.h&gt;</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;">&quot;你好,世界!!!&quot;</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="_4-3-多行注释" tabindex="-1">4.3 多行注释 <a class="header-anchor" href="#_4-3-多行注释" aria-label="Permalink to &quot;4.3 多行注释&quot;"></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;"> &lt;stdio.h&gt;</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;">&quot;你好,世界!!!&quot;</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 &quot;第五章HelloWorld 的规范(⭐)&quot;"></a></h1><h2 id="_5-1-规范的代码风格" tabindex="-1">5.1 规范的代码风格 <a class="header-anchor" href="#_5-1-规范的代码风格" aria-label="Permalink to &quot;5.1 规范的代码风格&quot;"></a></h2><h3 id="_5-1-1-正确的缩进和空白" tabindex="-1">5.1.1 正确的缩进和空白 <a class="header-anchor" href="#_5-1-1-正确的缩进和空白" aria-label="Permalink to &quot;5.1.1 正确的缩进和空白&quot;"></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;"> &lt;stdio.h&gt;</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;">&quot;c = </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%d</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;</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="_5-1-2-代码风格" tabindex="-1">5.1.2 代码风格 <a class="header-anchor" href="#_5-1-2-代码风格" aria-label="Permalink to &quot;5.1.2 代码风格&quot;"></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;">&gt;</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;">&gt;</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="_5-2-代码细节剖析" tabindex="-1">5.2 代码细节剖析 <a class="header-anchor" href="#_5-2-代码细节剖析" aria-label="Permalink to &quot;5.2 代码细节剖析&quot;"></a></h2><h3 id="_5-2-1-main-函数" tabindex="-1">5.2.1 main() 函数 <a class="header-anchor" href="#_5-2-1-main-函数" aria-label="Permalink to &quot;5.2.1 main() 函数&quot;"></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="_5-2-2-函数体" tabindex="-1">5.2.2 函数体 <a class="header-anchor" href="#_5-2-2-函数体" aria-label="Permalink to &quot;5.2.2 函数体&quot;"></a></h3><ul><li>① 一对花括号 <code>{}</code> 定义了函数的主体,所有函数都必须以大括号开头和结尾,成对出现。</li><li>② C 程序中的函数体指的是作为该函数一部分的语句。它可以是任何操作,比如:搜索、排序、打印等。</li><li>③ 每一个执行语句后面都会有一个英文分号<code>;</code>作为语句结束的标志。</li><li>④ 一行内可写几条语句,一条语句也可写在几行上。</li></ul><h3 id="_5-2-3-printf-函数" tabindex="-1">5.2.3 printf() 函数 <a class="header-anchor" href="#_5-2-3-printf-函数" aria-label="Permalink to &quot;5.2.3 printf() 函数&quot;"></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;">&quot;Hello World&quot;</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 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;">&quot;Hello World</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">\\n</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;</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><h3 id="_5-2-4-标准库和头文件" tabindex="-1">5.2.4 标准库和头文件 <a class="header-anchor" href="#_5-2-4-标准库和头文件" aria-label="Permalink to &quot;5.2.4 标准库和头文件&quot;"></a></h3><h4 id="_5-2-4-1-概述" tabindex="-1">5.2.4.1 概述 <a class="header-anchor" href="#_5-2-4-1-概述" aria-label="Permalink to &quot;5.2.4.1 概述&quot;"></a></h4><ul><li>printf() 函数是在标准库的头文件 <code>stdio.h</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;"> &lt;stdio.h&gt;</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><h4 id="_5-2-4-2-标准库-standard-library" tabindex="-1">5.2.4.2 标准库Standard Library <a class="header-anchor" href="#_5-2-4-2-标准库-standard-library" aria-label="Permalink to &quot;5.2.4.2 标准库Standard Library&quot;"></a></h4><ul><li>C 语言的<code>标准库</code>是由一组函数组成,这些函数提供了许多常用的操作和功能,如:输入输出、字符串处理、内存管理、数学计算等。标准库中的函数由编译器提供,遵循 ANSI C 标准C89/C90、C99、C11等。</li><li>换言之C 语言的<code>标准库</code>就是包含函数的实际代码这些代码在编译的时候被链接到我们的程序中无需手动包含。C 语言的<code>标准库</code>提供了可重用的函数实现,使得程序员不必编写常用的功能。</li></ul><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><p>实际的 printf() 函数的实现代码通常位于标准库的实现文件中,如:在 Linux 中的标准库<code>libc.so.6</code> 就包含了 printf() 函数的实现。</p></div><h4 id="_5-2-4-3-头文件-header-files" tabindex="-1">5.2.4.3 头文件Header Files <a class="header-anchor" href="#_5-2-4-3-头文件-header-files" aria-label="Permalink to &quot;5.2.4.3 头文件Header Files&quot;"></a></h4><ul><li><p><code>头文件</code>是包含函数声明、宏定义、数据类型定义等内容的文件。头文件的作用是为源代码提供必要的声明和定义,以便编译器能够正确解析和链接函数调用。头文件通常以<code>.h</code>作为文件扩展名。</p></li><li><p>换言之,头文件包含函数声明、宏定义和数据类型定义,但不包含函数的实现。头文件告知编译器如何使用标准库中的函数和定义,确保编译时的正确性。头文件需要在源代码文件中使用<code>#include</code>指令显式包含,如:<code>#include &lt;stdio.h&gt;</code>。</p></li><li><p>常见的 C 语言头文件及其功能和常用函数、宏等,如下所示:</p></li></ul><table tabindex="0"><thead><tr><th>头文件</th><th>功能说明</th><th>常用函数和宏</th></tr></thead><tbody><tr><td><strong>stdio.h</strong></td><td>标准输入输出库</td><td><code>printf</code> <code>scanf</code> <code>fprintf</code><code>fscanf</code><code>fopen</code> <code>fclose</code><code>fgets</code> <code>fputs</code></td></tr><tr><td><strong>stdlib.h</strong></td><td>标准库,提供内存分配、程序控制、类型转换、随机数生成等功能</td><td><code>malloc</code> <code>free</code> <code>exit</code> <code>atoi</code> <code>atof</code><code>rand</code><code>srand</code></td></tr><tr><td><strong>string.h</strong></td><td>字符串处理库</td><td><code>strlen</code> <code>strcpy</code> <code>strncpy</code> <code>strcat</code> <code>strcmp</code><code>strstr</code> <code>memset</code> <code>memcpy</code></td></tr><tr><td><strong>math.h</strong></td><td>数学库</td><td><code>sin</code> <code>cos</code> <code>tan</code> <code>exp</code> <code>log</code> <code>sqrt</code> <code>pow</code></td></tr><tr><td><strong>time.h</strong></td><td>时间和日期库</td><td><code>time</code> <code>clock</code> <code>difftime</code> <code>mktime</code> <code>strftime</code> <code>localtime</code><code>gmtime</code></td></tr><tr><td><strong>ctype.h</strong></td><td>字符处理库</td><td><code>isalnum</code> <code>isalpha</code> <code>isdigit</code> <code>islower</code> <code>isupper</code> <code>tolower</code> <code>toupper</code></td></tr><tr><td><strong>stdbool.h</strong></td><td>布尔类型库</td><td><code>bool</code> <code>true</code> <code>false</code></td></tr><tr><td><strong>assert.h</strong></td><td>断言库</td><td><code>assert</code></td></tr></tbody></table><h4 id="_5-2-4-4-预处理命令" tabindex="-1">5.2.4.4 预处理命令 <a class="header-anchor" href="#_5-2-4-4-预处理命令" aria-label="Permalink to &quot;5.2.4.4 预处理命令&quot;"></a></h4><ul><li><code>#include</code> 命令的作用是将指定文件的内容插入到包含该命令的源文件中。这通常用于包含头文件,以便使用头文件中声明的函数、宏和数据类型。</li><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:#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:#D73A49;--shiki-dark:#F97583;">#include</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &lt;filename&gt;</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>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">#include</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;filename&quot;</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 &quot;第六章CLion 高级配置(⭐)&quot;"></a></h1><h2 id="_6-1-安装和配置-wsl2" tabindex="-1">6.1 安装和配置 WSL2 <a class="header-anchor" href="#_6-1-安装和配置-wsl2" aria-label="Permalink to &quot;6.1 安装和配置 WSL2&quot;"></a></h2><h3 id="_6-1-1-概述" tabindex="-1">6.1.1 概述 <a class="header-anchor" href="#_6-1-1-概述" aria-label="Permalink to &quot;6.1.1 概述&quot;"></a></h3><ul><li>WSL2全称为 Windows Subsystem for Linux 2是微软提供的一种技术允许用户在 Windows 操作系统上运行 Linux 内核。WSL2 是 WSL1 的升级版,它引入了一个真正的 Linux 内核来代替 WSL1 中使用的兼容层,从而提供更高的性能和更广泛的系统调用支持。</li><li>和传统的虚拟化技术的对比,如下所示:</li></ul><p><img src="`+da+'" alt="" loading="lazy"></p><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><p>WSL2 的功能,如下所示:</p><ul><li>① <strong>真实的 Linux 内核</strong>WSL2 使用了微软开发的轻量级虚拟机,它包含了一个完整的 Linux 内核。这意味着 WSL2 能够运行更多的 Linux 应用程序,并且支持更多的系统调用。</li><li>② <strong>文件系统性能提升</strong>WSL2 的文件系统性能比 WSL1 有显著提升。对于 I/O 密集型的操作编译代码或数据库操作WSL2 能够提供更快的速度。</li><li>③ <strong>兼容性增强</strong>:由于使用了真实的 Linux 内核WSL2 对 Linux 应用程序的兼容性大幅提高。许多在 WSL1 上不能运行或需要调整的应用程序,可以在 WSL2 上直接运行。</li><li>④ <strong>网络功能改进</strong>WSL2 提供了更好的网络集成,能够更容易地与 Windows 上的其他网络资源进行交互。</li><li>⑤ <strong>资源使用优化</strong>WSL2 使用轻量级虚拟机,比传统的虚拟机占用更少的资源,同时提供了类似的隔离和安全性。</li></ul></div><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><p>WSL2 的用途,如下所示:</p><ul><li>① <strong>开发环境</strong>WSL2 为开发者提供了一个原生的 Linux 开发环境,而无需离开 Windows 。这对于需要在 Linux 上开发、测试或运行应用程序的开发者非常有帮助。</li><li>② <strong>学习和实验</strong>:用户可以使用 WSL2 在 Windows 上学习和实验 Linux 命令行工具和应用程序,而无需设置双重引导系统或安装虚拟机。</li><li>③ <strong>多平台开发</strong>对于跨平台开发者来说WSL2 允许他们在一个操作系统上同时进行 Windows 和 Linux 平台的开发和测试,提高工作效率。</li><li>④ <strong>运行 Linux 工具和应用程序</strong>WSL2 支持在 Windows 上直接运行各种 Linux 工具和应用程序Docker、数据库、编程语言环境等。</li></ul></div><h3 id="_6-1-2-wsl2-的安装" tabindex="-1">6.1.2 WSL2 的安装 <a class="header-anchor" href="#_6-1-2-wsl2-的安装" aria-label="Permalink to &quot;6.1.2 WSL2 的安装&quot;"></a></h3><ul><li>① BIOS 或 UEFI 中,开启虚拟化:步骤略。</li></ul><p><img src="'+oa+'" alt="" loading="lazy"></p><ul><li>② 查看是否开启了虚拟化:</li></ul><p><img src="'+ka+'" alt="" loading="lazy"></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 style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">dism.exe</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> /</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">online </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">/</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">enable-feature</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> /</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">featurename:Microsoft</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Windows</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Subsystem</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">Linux </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">/</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">all </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">/</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">norestart</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><div class="important custom-block github-alert"><p class="custom-block-title">IMPORTANT</p><p></p><p>以管理员身份打开 PowerShell 并运行,执行完上述命令之后,如果提示需要重启计算机,那就重启计算机。</p></div><p><img src="'+ua+'" alt="" loading="lazy"></p><ul><li>④ 启用虚拟机功能:</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 style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">dism.exe</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> /</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">online </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">/</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">enable-feature</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> /</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">featurename:VirtualMachinePlatform </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">/</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">all </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">/</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">norestart</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><div class="important custom-block github-alert"><p class="custom-block-title">IMPORTANT</p><p></p><p>以管理员身份打开 PowerShell 并运行,执行完上述命令之后,如果提示需要重启计算机,那就重启计算机。</p></div><p><img src="'+ga+'" alt="" loading="lazy"></p><ul><li>⑤ 更新 Linux 内核包:</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 style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">wsl </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">--</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">update</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>wsl2 的最新 Linux 内核包托管在 GitHub 上,某些国家可能会污染 Github 相关的域名,那么就需要手动下载,然后安装即可,下载地址在<a href="https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi" target="_blank" rel="noreferrer">这里</a>。</p></div><p><img src="'+ba+'" alt="" loading="lazy"></p><ul><li>⑥ 将 wsl2 设置为默认版本:</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 style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">wsl </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">--</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">set-default</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">version </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">2</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><p><img src="'+ma+'" alt="" loading="lazy"></p><ul><li>⑦ 查看官方在线支持的 Linux 版本:</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 style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">wsl </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">--</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">list </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">--</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">online</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><p><img src="'+Ca+'" alt="" loading="lazy"></p><ul><li>⑧ 安装指定版本的 Linux </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 style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">wsl </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">--</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">install Ubuntu</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">24.04</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>官方支持的 Linux 版本,托管在 Github 上,某些国家可能会污染 Github 的域名;此时,要么走科学上网,要么在 <code>Microsoft Store</code> 中搜索并安装。</p></div><p><img src="'+Ea+'" alt="" loading="lazy"></p><ul><li>⑨ 在 Microsoft Store 中搜索并安装(可选):</li></ul><p><img src="'+ya+'" alt="" loading="lazy"></p><ul><li>⑩ 查询本地安装的 Linux 版本:</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 style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">wsl </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">--</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">list</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><p><img src="'+Fa+`" alt="" loading="lazy"></p><h3 id="_6-1-3-配置-wsl2" tabindex="-1">6.1.3 配置 WSL2 <a class="header-anchor" href="#_6-1-3-配置-wsl2" aria-label="Permalink to &quot;6.1.3 配置 WSL2&quot;"></a></h3><ul><li>本人的安装的是 AlmaLinux9 ,所以需要执行如下命令,以便安装 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;"> dnf</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> update</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;"> groupinstall</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Development Tools&quot;</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>
<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="`+_a+'" alt="" loading="lazy"></p><ul><li>可以通过 CLion 测试是否安装成功:</li></ul><p><img src="'+va+`" alt="" loading="lazy"></p><h3 id="_6-1-4-配置-wsl2" tabindex="-1">6.1.4 配置 WSL2 <a class="header-anchor" href="#_6-1-4-配置-wsl2" aria-label="Permalink to &quot;6.1.4 配置 WSL2&quot;"></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;"> &amp;&amp; </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>
<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>
<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="`+Ba+'" alt="" loading="lazy"></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="'+fa+'" alt="" loading="lazy"></p><h2 id="_6-2-切换-clion-中的-cmake-的工具链" tabindex="-1">6.2 切换 CLion 中的 cmake 的工具链 <a class="header-anchor" href="#_6-2-切换-clion-中的-cmake-的工具链" aria-label="Permalink to &quot;6.2 切换 CLion 中的 cmake 的工具链&quot;"></a></h2><ul><li>可以在 CLoin 中切换 cmake 的工具链,以便支持不同平台的 cmake ,即:</li></ul><p><img src="'+Aa+'" alt="" loading="lazy"></p><h2 id="_6-3-修改-cmakelists-txt-文件" tabindex="-1">6.3 修改 CMakeLists.txt 文件 <a class="header-anchor" href="#_6-3-修改-cmakelists-txt-文件" aria-label="Permalink to &quot;6.3 修改 CMakeLists.txt 文件&quot;"></a></h2><ul><li>前文也提到了,在一个 C 语言项目中,只能有一个 main() 函数;但是,我们可以修改 <code>CMakeLists.txt</code> 文件的内容,以便其支持在一个 C 语言项目中,可以有多个包含 main() 函数的文件,如下所示:</li></ul><p><img src="'+Da+`" alt="" loading="lazy"></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>
<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 &quot;\${dir}/*.c&quot;)</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 &quot;\${CMAKE_SOURCE_DIR}/include&quot;)</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 &quot;/&quot; &quot;_&quot; EXECUTABLE_NAME \${REL_PATH})</span></span>
<span class="line"><span> string(REPLACE &quot;\\\\&quot; &quot;_&quot; EXECUTABLE_NAME \${EXECUTABLE_NAME})</span></span>
<span class="line"><span> string(REPLACE &quot;.&quot; &quot;_&quot; 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 &quot;CMakeLists_txt&quot;)</span></span>
<span class="line"><span> set(EXECUTABLE_NAME &quot;\${EXECUTABLE_NAME}_exec&quot;)</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 &quot;\${DIR}/include&quot;)</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 &quot;\${DIR}/module&quot;)</span></span>
<span class="line"><span> file(GLOB_RECURSE MODULE_SOURCES &quot;\${DIR}/module/*.c&quot;)</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><h2 id="_6-4-配置-clang-format-文件" tabindex="-1">6.4 配置 .clang-format 文件 <a class="header-anchor" href="#_6-4-配置-clang-format-文件" aria-label="Permalink to &quot;6.4 配置 .clang-format 文件&quot;"></a></h2><ul><li>配置 <code>.clang-format</code> 格式化文件,以便写代码的时候,可以自动保存并格式化 C 程序代码,如下所示:</li></ul><p><img src="`+xa+`" alt="" loading="lazy"></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>
<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>
<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="`+qa+'" alt="" loading="lazy"></p><h2 id="_6-5-配置-gitignore-文件" tabindex="-1">6.5 配置 .gitignore 文件 <a class="header-anchor" href="#_6-5-配置-gitignore-文件" aria-label="Permalink to &quot;6.5 配置 .gitignore 文件&quot;"></a></h2><ul><li>需要在项目中,配置 <code>.gitignore</code> 文件,以便在提交代码到 Git 仓库的时候,忽略某些文件或目录,如下所示:</li></ul><p><img src="'+wa+`" alt="" loading="lazy"></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>
<span class="line"><span>.idea</span></span>
<span class="line"><span>cmake-build-*</span></span>
<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="_6-6-演示" tabindex="-1">6.6 演示 <a class="header-anchor" href="#_6-6-演示" aria-label="Permalink to &quot;6.6 演示&quot;"></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="`+Sa+'" alt="" loading="lazy"></p><h1 id="第七章-c-语言的编译过程-⭐" tabindex="-1">第七章C 语言的编译过程(⭐) <a class="header-anchor" href="#第七章-c-语言的编译过程-⭐" aria-label="Permalink to &quot;第七章C 语言的编译过程(⭐)&quot;"></a></h1><h2 id="_7-1-概述" tabindex="-1">7.1 概述 <a class="header-anchor" href="#_7-1-概述" aria-label="Permalink to &quot;7.1 概述&quot;"></a></h2><ul><li>C 程序的编译过程,如下所示:</li></ul><p><img src="'+La+'" alt="" loading="lazy"></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>头文件包含:将头文件的内容插入到源文件中。例如:<code>#include &lt;stdio.h&gt;</code>会被替换为<code>stdio.h</code>文件的内容。</p></li><li><p>宏展开:替换宏定义。例如:<code>#define PI 3.14</code>会将代码中的<code>PI</code>替换为<code>3.14</code>。</p></li><li><p>条件编译:根据条件指令(如:<code>#ifdef</code>、<code>#ifndef</code>)有选择地编译代码。</p></li><li><p>删除代码中的注释,但是不会进行语法检查。</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>因为我们的 C 程序会使用 C 程序库中的内容,如:<code>&lt;stdio.h&gt;</code> 中的 <code>printf()</code> 函数,这些函数不是程序员自己写的,而是 C 程序库中提供的因此需要链接。其实在链接过程中还会加入启动代码这个启动代码和系统相关Linux 下主要有 crt0.c、crti.c 等,它们设置堆栈后,再调用 main() 函数)负责初始化程序运行时的环境。</li></ul></div><ul><li>过程 ④ :执行,即:有了可执行的 <code>*.exe</code>文件,我们就可以在控制台上执行运行此 <code>*.exe</code> 文件。</li></ul><div class="important custom-block github-alert"><p class="custom-block-title">IMPORTANT</p><p></p><p>如果<code>修改</code>了源代码,还需要重新<code>编译</code>、<code>链接</code>,并生成新的 <code>*.exe</code>文件,再执行,方能生效。</p></div><h2 id="_7-2-gcc-编译器的介绍" tabindex="-1">7.2 GCC 编译器的介绍 <a class="header-anchor" href="#_7-2-gcc-编译器的介绍" aria-label="Permalink to &quot;7.2 GCC 编译器的介绍&quot;"></a></h2><ul><li>编辑器vim 、vscode 等,是指我们用它来编写源程序的(编辑代码),而我们写的代码语句,电脑是不懂的,我们需要把它转成电脑能懂的语句,编译器就是这样的转化工具。换言之,我们用编辑器编写程序,由编译器编译后才可以运行!</li><li>编译器是将易于编写、阅读和维护的高级计算机语言翻译为计算机能解读、运行的低级机器语言的程序。</li><li>gccGNU Compiler CollectionGNU 编译器套件),是由 GNU 开发的编程语言编译器。gcc 原本作为 GNU 操作系统的官方编译器,现已被大多数类 Unix 操作系统Linux、BSD、Mac OS X 等采纳为标准的编译器gcc 同样适用于微软的 Windows 。</li><li>gcc 最初用于编译 C 语言,随着项目的发展, gcc 已经成为了能够编译 C、C++、Java、Ada、fortran、Object C、Object C++、Go 语言的编译器大家族。</li></ul><h2 id="_7-3-通过-gcc-直接生成可执行文件" tabindex="-1">7.3 通过 gcc 直接生成可执行文件 <a class="header-anchor" href="#_7-3-通过-gcc-直接生成可执行文件" aria-label="Permalink to &quot;7.3 通过 gcc 直接生成可执行文件&quot;"></a></h2><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;">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="'+za+`" alt="" loading="lazy"></p><h2 id="_7-4-通过-gcc-分步编译" tabindex="-1">7.4 通过 gcc 分步编译 <a class="header-anchor" href="#_7-4-通过-gcc-分步编译" aria-label="Permalink to &quot;7.4 通过 gcc 分步编译&quot;"></a></h2><h3 id="_7-3-1-概述" tabindex="-1">7.3.1 概述 <a class="header-anchor" href="#_7-3-1-概述" aria-label="Permalink to &quot;7.3.1 概述&quot;"></a></h3><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:#6A737D;--shiki-dark:#6A737D;"># 通常以 .i 结尾表示这个文件是一个中间状态</span></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">gcc</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -E</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 源文件.c</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -o</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 源文件.i</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><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:#6A737D;--shiki-dark:#6A737D;"># 在 Linux 中,通常以 .s 结尾;在 Windows 中,通常以 .asm 结尾</span></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">gcc</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -S</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 源文件.i</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -o</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 源文件.s</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><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:#6A737D;--shiki-dark:#6A737D;"># 在 Linux 中,通常以 .o 结尾;在 Windows 中,通常以 .obj 结尾</span></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">gcc</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -c</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 源文件.s</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -o</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 源文件.o</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><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:#6A737D;--shiki-dark:#6A737D;"># 在 Linux 中,通常以 .out 结尾;在 Windows 中,通常以 .exe 结尾</span></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">gcc</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 源文件.o</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -o</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 源文件.exe</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><h3 id="_7-4-2-应用示例" tabindex="-1">7.4.2 应用示例 <a class="header-anchor" href="#_7-4-2-应用示例" aria-label="Permalink to &quot;7.4.2 应用示例&quot;"></a></h3><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;">gcc</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -E</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.i</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><p><img src="`+Pa+'" alt="" loading="lazy"></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;">gcc</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -S</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> HelloWorld.i</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -o</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> HelloWorld.s</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="" loading="lazy"></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;">gcc</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -c</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> HelloWorld.s</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -o</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> HelloWorld.o</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><p><img src="'+Wa+'" alt="" loading="lazy"></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;">gcc</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> HelloWorld.o</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="'+Ma+'" alt="" loading="lazy"></p><h1 id="第八章-附录" tabindex="-1">第八章:附录 <a class="header-anchor" href="#第八章-附录" aria-label="Permalink to &quot;第八章:附录&quot;"></a></h1><h2 id="_8-1-wsl2-代理问题" tabindex="-1">8.1 WSL2 代理问题 <a class="header-anchor" href="#_8-1-wsl2-代理问题" aria-label="Permalink to &quot;8.1 WSL2 代理问题&quot;"></a></h2><ul><li>在安装和配置 WSL2 之后,可能会出现如下的提示,即:</li></ul><p><img src="'+Ua+`" alt="" loading="lazy"></p><ul><li>那么,只需要修改 <code>%USERPROFILE%\\.wslconfig</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>[wsl2]</span></span>
<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>
<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="`+Oa+'" alt="" loading="lazy"></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="'+Ia+'" alt="" loading="lazy"></p><ul><li>此时,再打开终端,就没有这种提示了:</li></ul><p><img src="'+Na+'" alt="" loading="lazy"></p><h2 id="_8-2-clion-调试问题" tabindex="-1">8.2 CLion 调试问题 <a class="header-anchor" href="#_8-2-clion-调试问题" aria-label="Permalink to &quot;8.2 CLion 调试问题&quot;"></a></h2><ul><li>在 CLion 中进行 run运行程序的时候对于 <code>printf</code> 函数或 <code>scanf</code> 函数很正常,如下所示:</li></ul><p><img src="'+Ra+'" alt="" loading="lazy"></p><ul><li>但是,当我们 debug调试 的时候,对于 <code>printf</code> 函数或 <code>scanf</code> 函数会一直没有输出,如下所示:</li></ul><p><img src="'+Va+`" alt="" loading="lazy"></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;"> &lt;stdio.h&gt;</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:#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;">&quot;请输入整数 a 、b 和 c 的值:&quot;</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;">&quot;</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;">&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">&amp;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">a, </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">&amp;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">b, </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">&amp;</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;">&quot;</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;">&quot;</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>
<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="`+Ga+'" alt="" loading="lazy"></p><h2 id="_8-3-内存泄露检测" tabindex="-1">8.3 内存泄露检测 <a class="header-anchor" href="#_8-3-内存泄露检测" aria-label="Permalink to &quot;8.3 内存泄露检测&quot;"></a></h2><h3 id="_8-3-1-概述" tabindex="-1">8.3.1 概述 <a class="header-anchor" href="#_8-3-1-概述" aria-label="Permalink to &quot;8.3.1 概述&quot;"></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="_8-3-2-安装" tabindex="-1">8.3.2 安装 <a class="header-anchor" href="#_8-3-2-安装" aria-label="Permalink to &quot;8.3.2 安装&quot;"></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;"> &amp;&amp; </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;"> &amp;&amp; </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:#24292E;--shiki-dark:#E1E4E8;"> &amp;&amp; </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;"> valgrind</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # Ubuntu</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><p><img src="'+Xa+'" alt="" loading="lazy"></p><ul><li>查看 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:#005CC5;--shiki-dark:#79B8FF;">which</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> valgrind</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><p><img src="'+$a+'" alt="" loading="lazy"></p><h3 id="_8-3-3-整合" tabindex="-1">8.3.3 整合 <a class="header-anchor" href="#_8-3-3-整合" aria-label="Permalink to &quot;8.3.3 整合&quot;"></a></h3><ul><li>CLion 中将工具链设置为 WSL2 </li></ul><p><img src="'+Ha+'" alt="" loading="lazy"></p><ul><li>CLion 中配置 valgrind 的路径:</li></ul><p><img src="'+Ka+'" alt="" loading="lazy"></p><ul><li>查看 WSL2 中 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;">cmake</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --version</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><p><img src="'+Qa+`" alt="" loading="lazy"></p><ul><li>修改项目中 CMakeLists.txt 中 cmake 的版本:</li></ul><div class="language- vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang"></span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line highlighted"><span>cmake_minimum_required(VERSION 3.26.5) # 3.26.5</span></span>
<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 &quot;\${dir}/*.c&quot;)</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 &quot;\${CMAKE_SOURCE_DIR}/include&quot;)</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 &quot;/&quot; &quot;_&quot; EXECUTABLE_NAME \${REL_PATH})</span></span>
<span class="line"><span> string(REPLACE &quot;\\\\&quot; &quot;_&quot; EXECUTABLE_NAME \${EXECUTABLE_NAME})</span></span>
<span class="line"><span> string(REPLACE &quot;.&quot; &quot;_&quot; 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 &quot;CMakeLists_txt&quot;)</span></span>
<span class="line"><span> set(EXECUTABLE_NAME &quot;\${EXECUTABLE_NAME}_exec&quot;)</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 &quot;\${DIR}/include&quot;)</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 &quot;\${DIR}/module&quot;)</span></span>
<span class="line"><span> file(GLOB_RECURSE MODULE_SOURCES &quot;\${DIR}/module/*.c&quot;)</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="`+Ya+'" alt="" loading="lazy"></p><ul><li>在 CLion 中通过 valgrind 运行代码:</li></ul><p><img src="'+ja+'" alt="" loading="lazy"></p><h2 id="_8-4-性能分析" tabindex="-1">8.4 性能分析 <a class="header-anchor" href="#_8-4-性能分析" aria-label="Permalink to &quot;8.4 性能分析&quot;"></a></h2><h3 id="_8-4-1-概述" tabindex="-1">8.4.1 概述 <a class="header-anchor" href="#_8-4-1-概述" aria-label="Permalink to &quot;8.4.1 概述&quot;"></a></h3><ul><li><code>perf</code> 是一个 Linux 下的性能分析工具,主要用于监控和分析系统性能。它可以帮助开发者和系统管理员了解系统中哪些部分在消耗资源、识别性能瓶颈以及分析程序的运行效率。</li></ul><h3 id="_8-4-2-安装" tabindex="-1">8.4.2 安装 <a class="header-anchor" href="#_8-4-2-安装" aria-label="Permalink to &quot;8.4.2 安装&quot;"></a></h3><h4 id="_8-4-2-1-almalinux9" tabindex="-1">8.4.2.1 AlmaLinux9 <a class="header-anchor" href="#_8-4-2-1-almalinux9" aria-label="Permalink to &quot;8.4.2.1 AlmaLinux9&quot;"></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:#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;"> perf</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><p><img src="'+Ja+`" alt="" loading="lazy"></p><h4 id="_8-4-2-2-ubuntu-22-04" tabindex="-1">8.4.2.2 Ubuntu 22.04 <a class="header-anchor" href="#_8-4-2-2-ubuntu-22-04" aria-label="Permalink to &quot;8.4.2.2 Ubuntu 22.04&quot;"></a></h4><ul><li>在 WSL2 中的 Ubuntu 安装 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:#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:#005CC5;--shiki-dark:#79B8FF;"> \\</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> &amp;&amp; </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:#005CC5;--shiki-dark:#79B8FF;"> \\</span></span>
<span class="line"><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><span class="line-number">3</span><br></div></div><p><img src="`+Za+'" alt="" loading="lazy"></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="'+si+'" alt="" loading="lazy"></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;">&#39;-&#39;</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="'+ai+`" alt="" loading="lazy"></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;"> &amp;&amp; </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:#005CC5;--shiki-dark:#79B8FF;"> \\</span></span>
<span class="line"><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:#032F62;--shiki-dark:#9ECBFF;"> libbabeltrace-dev</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> libcap-dev</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> \\</span></span>
<span class="line"><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:#005CC5;--shiki-dark:#79B8FF;"> \\</span></span>
<span class="line"><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:#032F62;--shiki-dark:#9ECBFF;"> libtraceevent-dev</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> libunwind-dev</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> \\</span></span>
<span class="line"><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:#005CC5;--shiki-dark:#79B8FF;"> \\</span></span>
<span class="line"><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><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="`+ii+`" alt="" loading="lazy"></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>
<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="`+ni+'" alt="" loading="lazy"></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="'+li+'" alt="" loading="lazy"></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;"> &amp;&amp; </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="'+ei+'" alt="" loading="lazy"></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="'+pi+'" alt="" loading="lazy"></p><h3 id="_8-4-3-整合" tabindex="-1">8.4.3 整合 <a class="header-anchor" href="#_8-4-3-整合" aria-label="Permalink to &quot;8.4.3 整合&quot;"></a></h3><ul><li>CLion 中配置 perf 的路径:</li></ul><p><img src="'+ti+'" alt="" loading="lazy"></p><ul><li>在 CLion 中通过 perf 运行代码:</li></ul><p><img src="'+ri+'" alt="" loading="lazy"></p><h2 id="_8-5-win-中文乱码问题" tabindex="-1">8.5 Win 中文乱码问题 <a class="header-anchor" href="#_8-5-win-中文乱码问题" aria-label="Permalink to &quot;8.5 Win 中文乱码问题&quot;"></a></h2><ul><li>前文,我们提及到,在 Win 中,如果出现<code>中文乱码</code>问题,就需要去<code>语言和区别</code>设置<code>系统区域</code>的编码为 UTF-8 但是这样可能会造成其它的软件出现中文乱码问题Xshell 等。</li></ul><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><ul><li>① 之所以,修改系统的编码为 UTF-8 会出现问题,是因为早期的 Win 系统的中文默认编码是 GBK目前也是Win 并没有强制第三方软件使用 UTF-8 编码) ,而 Xshell 等也使用的这些编码,一旦我们修改为 UTF-8 之后,可能会造成这些第三方软件出现中文乱码问题(第三方软件适配问题,相信将来应该都会切换为 UTF-8 编码),体验较差!!!</li><li>② 在 Linux 或 MacOS 之所以不会出现中文乱码的问题,是因为这些系统默认的编码就是 UTF-8 。</li></ul></div><ul><li>其实,还有一种解决方案,如下所示:</li></ul><p><img src="'+ci+'" alt="" loading="lazy"></p><p><img src="'+hi+'" alt="" loading="lazy"></p><p><img src="'+di+'" alt="" loading="lazy"></p><ul><li>测试一下,是否配置成功:</li></ul><p><img src="'+oi+'" alt="" loading="lazy"></p><h2 id="_8-6-clion-中自动导入头文件" tabindex="-1">8.6 CLion 中自动导入头文件 <a class="header-anchor" href="#_8-6-clion-中自动导入头文件" aria-label="Permalink to &quot;8.6 CLion 中自动导入头文件&quot;"></a></h2><ul><li>在 CLion 中,最为强大的功能就是直接输入函数,然后让 IDE 帮我们自动导入头文件,包括自定义的头文件,相当实用。</li></ul><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><ul><li>① CLion 中的<code>自动导入头文件</code>的<code>快捷键</code>是 <code>Alt + Enter</code> 。</li><li>② CLion 中的<code>自动提取变量的类型</code>的<code>快捷键</code>是 <code>Ctrl + Alt + V</code> 。</li></ul></div><p><img src="'+ki+'" alt="" loading="lazy"></p><ul><li>开启自动导入头文件的步骤,如下所示:</li></ul><p><img src="'+ui+'" alt="" loading="lazy"></p><p><img src="'+gi+'" alt="" loading="lazy"></p>',572),Ci=[mi];function Ei(yi,Fi,_i,vi,Bi,fi){return i(),a("div",null,Ci)}const xi=s(bi,[["render",Ei]]);export{Di as __pageData,xi as default};