mirror of
https://github.com/Aexiar/c.git
synced 2024-10-22 14:05:45 +02:00
286 lines
166 KiB
JavaScript
286 lines
166 KiB
JavaScript
import{_ as s,c as a,o as i,a6 as n}from"./chunks/framework.CZRoMP2i.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",E="/c/assets/15.C3oC1iUd.png",C="/c/assets/16.Ds-jsEZn.png",F="/c/assets/17.Cd37gMYs.png",y="/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",P="/c/assets/30.picNTNaJ.png",T="/c/assets/31.DKX92RgJ.png",W="/c/assets/32.Bxr7QKI3.png",M="/c/assets/33.DuFWsOgW.png",U="/c/assets/34.Da9qUcfr.png",R="/c/assets/35.7_gEABSX.gif",O="/c/assets/36.C2KQYoVC.gif",N="/c/assets/37.RTUP8Vtx.png",I="/c/assets/38.Dq21DxwL.png",V="/c/assets/39.QmpUaNxU.png",G="/c/assets/40.DP0C29gZ.png",X="/c/assets/41.DwzHokd4.png",$="/c/assets/42.Bi6K26yA.png",H="/c/assets/43.Bw8DcEQ_.png",K="/c/assets/44.DPc8Z9rS.png",Q="/c/assets/45.S84xHMTl.png",j="/c/assets/46.iw3JQUkn.png",Y="/c/assets/47.ILeJt-HK.png",J="/c/assets/48.CHgm7khh.png",Z="/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",Es="/c/assets/69.Bw_dQ4ap.png",Cs="/c/assets/70.Cn_ubTyA.png",Fs="/c/assets/71.C84EBqwB.png",ys="/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",Ps="/c/assets/84.CXcIKX1T.png",Ts="/c/assets/85.CQ3_QCQT.png",Ws="/c/assets/86.CQoDRJM5.gif",Ms="/c/assets/87.CPfwZvD9.gif",Us="/c/assets/88.bxEomEjU.gif",Rs="/c/assets/89.C_onhf2k.png",Os="/c/assets/90.Budj3s38.gif",Ns="/c/assets/91.CH8koQdu.gif",Is="/c/assets/92.kUJhxMtg.png",Vs="/c/assets/93.BJeJJW4_.png",Gs="/c/assets/94.BCuK6eJL.gif",Xs="/c/assets/95.rKC-z4z-.gif",$s="/c/assets/96.BJE3xxuT.gif",Hs="/c/assets/97.DJOq__O0.gif",Ks="/c/assets/98.BDt_0fRR.png",Qs="/c/assets/99.Bo5OawW8.png",js="/c/assets/100.sUWUfl5e.png",Ys="/c/assets/101.BK4jpnB8.png",Js="/c/assets/102.EF8vdbSq.png",Zs="/c/assets/103.DjxdA_Co.png",zs="/c/assets/104.sp68q1hh.gif",sa="/c/assets/105.DhMRyxNN.gif",aa="/c/assets/106.CdcLZM0R.gif",ia="/c/assets/107.D-5AsTTr.png",na="/c/assets/108.2RQKmAUa.png",la="/c/assets/109.CI4xX8SM.png",ea="/c/assets/110.C8QUZ9v3.gif",pa="/c/assets/111.g5tQfhWz.gif",ta="/c/assets/112.CX0_1NsE.gif",ra="/c/assets/113.BrFJeoK6.png",ca="/c/assets/114.58TjSSjH.gif",ha="/c/assets/115.CgqNV2hL.png",da="/c/assets/116.BasYWvkM.png",oa="/c/assets/117.CjFxJpBr.png",ka="/c/assets/118.Dm5Jspvp.gif",ua="/c/assets/119.1krCh8cy.gif",ga="/c/assets/120.DOSyahv7.gif",ba="/c/assets/121.BHEvX_FI.gif",ma="/c/assets/122.DFA0g1_d.gif",Ea="/c/assets/123.D50nO5eA.gif",Ca="/c/assets/124.cAvG1Qfq.png",Fa="/c/assets/125.DpakQFWz.gif",ya="/c/assets/126.Bg61j9QI.gif",_a="/c/assets/127.DPvD9UCw.gif",va="/c/assets/128.Ceu65U_n.gif",Ba="/c/assets/129.BWPFRK_8.gif",fa="/c/assets/130.Ct4TfxPf.gif",Aa="/c/assets/131.zsKtx_jV.png",Da="/c/assets/132.3oqDAW0w.png",xa="/c/assets/133.D9TSeAa9.gif",qa="/c/assets/134.CCRgYIVq.png",wa="/c/assets/135.C1t2T6FV.gif",Sa="/c/assets/136.Rqa4uHq6.png",La="/c/assets/137.6yQYnjOa.gif",Pa="/c/assets/138.hYR9GiiD.gif",Ta="/c/assets/139.D3b-QIrA.gif",Wa="/c/assets/140.DGbA6qsW.gif",Ma="/c/assets/141.BToOkxf2.gif",Ua="/c/assets/142.C3wekjfY.png",Ra="/c/assets/143.CKuuzuQ_.png",Oa="/c/assets/144.B4r6Nv14.gif",Na="/c/assets/145.CvbhuZNK.png",Ia="/c/assets/146.BPgYm4ZX.gif",Va="/c/assets/147.DH2fri13.gif",Ga="/c/assets/148.CDDIqy03.gif",Xa="/c/assets/149.DCQsqYYC.gif",$a="/c/assets/150.CUjMoXNF.gif",Ha="/c/assets/151.BWbajkuB.gif",Ka="/c/assets/152.C1JnBDr7.png",Qa="/c/assets/153.DwN2p3bo.png",ja="/c/assets/154.DdSfz-Pf.gif",Ya="/c/assets/155.BMocjVt7.gif",Ja="/c/assets/156.BUU-wlaw.gif",Za="/c/assets/157.ssadIF0D.gif",za="/c/assets/158.Dq5lLq9v.gif",si="/c/assets/159.CJ0_loZ8.gif",ai="/c/assets/160.BPOK9irZ.gif",ii="/c/assets/161.B1eB55rC.gif",ni="/c/assets/162.VIcU-hkN.gif",li="/c/assets/163.BM9viRZJ.gif",ei="/c/assets/164.DpMBA1SK.gif",pi="/c/assets/165.DgXuU6LF.png",ti="/c/assets/166.Dnw73s2h.gif",ri="/c/assets/167.SnFXRSaL.png",ci="/c/assets/168.DnoEqOjV.png",hi="/c/assets/169.BJBdn7pw.png",di="/c/assets/170.DXtQWYsM.gif",oi="/c/assets/171.hcERTQkN.gif",ki="/c/assets/172.CioraEUv.png",ui="/c/assets/173.kWHWwnc_.png",Ai=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":1723426385000}'),gi={name:"notes/01_c-basic/01_xdx/index.md"},bi=n('<h1 id="第一章-开发环境的安装和配置-⭐" tabindex="-1">第一章:开发环境的安装和配置(⭐) <a class="header-anchor" href="#第一章-开发环境的安装和配置-⭐" aria-label="Permalink to "第一章:开发环境的安装和配置(⭐)""></a></h1><h2 id="_1-1-什么是编译器" tabindex="-1">1.1 什么是编译器? <a class="header-anchor" href="#_1-1-什么是编译器" aria-label="Permalink to "1.1 什么是编译器?""></a></h2><ul><li>要开发 C/C++ 程序,需要安装 C/C++ 编译器,目前有两种主流实现,即: <ul><li>GCC(GNU Compiler Collection):全平台实现,即支持 Windows、MacOS、Linux 等。</li><li>MSVC(Microsoft 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 "1.2 编译器的安装和配置""></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 "1.2.2 MinGW-w64 的安装和配置""></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 "1.2.2.1 安装""></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=""></p><ul><li>解压到指定的目录,即:</li></ul><p><img src="'+e+'" alt=""></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 "1.2.2.2 配置 path 环境变量""></a></h4><ul><li>配置环境变量,以便任意目录都可以执行 gcc 命令,即:</li></ul><p><img src="'+p+'" alt=""></p><p><img src="'+t+'" alt=""></p><p><img src="'+r+'" alt=""></p><p><img src="'+c+'" alt=""></p><p><img src="'+h+'" alt=""></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=""></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=""></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 "1.2.3 Cygwin 的安装和配置""></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 "1.2.3.1 安装""></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=""></p><ul><li>点击安装:</li></ul><p><img src="'+u+'" alt=""></p><p><img src="'+g+'" alt=""></p><p><img src="'+b+'" alt=""></p><p><img src="'+m+'" alt=""></p><p><img src="'+E+'" alt=""></p><p><img src="'+C+'" alt=""></p><p><img src="'+F+'" alt=""></p><p><img src="'+y+'" alt=""></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=""></p><p><img src="'+v+'" alt=""></p><p><img src="'+B+'" alt=""></p><p><img src="'+f+'" alt=""></p><p><img src="'+A+'" alt=""></p><p><img src="'+D+'" alt=""></p><p><img src="'+x+'" alt=""></p><p><img src="'+q+'" alt=""></p><p><img src="'+w+'" alt=""></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 "1.2.3.2 配置 path 环境变量""></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 "1.2.4 MSYS2(推荐)""></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 "1.2.4.1 安装""></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=""></p><ul><li>点击安装:</li></ul><p><img src="'+L+'" alt=""></p><p><img src="'+P+'" alt=""></p><p><img src="'+T+'" alt=""></p><p><img src="'+W+'" alt=""></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="'+M+'" alt=""></p><ul><li>出现命令终端:</li></ul><p><img src="'+U+`" alt=""></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;"> "s#https\\?://mirror.msys2.org/#https://mirrors.tuna.tsinghua.edu.cn/msys2/#g"</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="`+R+'" alt=""></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-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><p><img src="'+O+'" alt=""></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 "1.2.4.2 配置 path 环境变量""></a></h4><ul><li>和 <code>1.2.2.2 配置 path 环境变量</code> 步骤相同:略。</li></ul><h2 id="_1-3-什么是-ide-集成开发环境" tabindex="-1">1.3 什么是 IDE(集成开发环境)? <a class="header-anchor" href="#_1-3-什么是-ide-集成开发环境" aria-label="Permalink to "1.3 什么是 IDE(集成开发环境)?""></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 ,通常为集成开发环境(IDE,Integrated Development Environment)。</p></li></ul><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><ul><li>① IDE(集成开发环境)就是一系列开发工具的组合套装。</li><li>② 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-43-ide-的安装和配置" tabindex="-1">1.43 IDE 的安装和配置 <a class="header-anchor" href="#_1-43-ide-的安装和配置" aria-label="Permalink to "1.43 IDE 的安装和配置""></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 "1.4.1 CLion""></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 "1.4.1.1 概述""></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><p>本次,演示的 CLion 的安装版本是 2024.1.4 ,后续版本可能会更新,但是操作几乎不会发生太多变化!!!</p></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 "1.4.1.2 安装""></a></h4><ul><li>鼠标双击,进入安装:</li></ul><p><img src="'+N+'" alt=""></p><ul><li>下一步:</li></ul><p><img src="'+I+'" alt=""></p><ul><li>下一步:</li></ul><p><img src="'+V+'" alt=""></p><ul><li>下一步:</li></ul><p><img src="'+G+'" alt=""></p><ul><li>安装:</li></ul><p><img src="'+X+'" alt=""></p><ul><li>安装完成:</li></ul><p><img src="'+$+'" alt=""></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 "1.4.1.3 配置""></a></h4><ul><li>打开 CLion :</li></ul><p><img src="'+H+'" alt=""></p><ul><li>安装中文插件(可选):</li></ul><p><img src="'+K+'" alt=""></p><p><img src="'+Q+'" alt=""></p><ul><li>配置新 UI :</li></ul><p><img src="'+j+'" alt=""></p><p><img src="'+Y+'" alt=""></p><ul><li>新 UI 配置自定义字体(可选):</li></ul><p><img src="'+J+'" alt=""></p><ul><li>配置<code>自动保存</code>功能:</li></ul><p><img src="'+Z+'" alt=""></p><ul><li>配置<code>文件编码</code> 为 UTF-8:</li></ul><p><img src="'+z+'" alt=""></p><ul><li>配置<code>控制台编码</code>为 UTF-8:</li></ul><p><img src="'+ss+'" alt=""></p><ul><li>配置<code>显示方法分隔符</code>功能:</li></ul><p><img src="'+as+'" alt=""></p><ul><li>配置<code>编辑器</code>的字体(可选):</li></ul><p><img src="'+is+'" alt=""></p><ul><li>检测 GCC 工具链是否安装成功:</li></ul><p><img src="'+ns+'" alt=""></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 "1.4.2 VS Code""></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 "1.4.2.1 概述""></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 "1.4.2.2 安装""></a></h4><ul><li>鼠标双击,进入安装:</li></ul><p><img src="'+ls+'" alt=""></p><ul><li>同意协议:</li></ul><p><img src="'+es+'" alt=""></p><ul><li>下一步:</li></ul><p><img src="'+ps+'" alt=""></p><ul><li>下一步:</li></ul><p><img src="'+ts+'" alt=""></p><ul><li>下一步:</li></ul><p><img src="'+rs+'" alt=""></p><ul><li>安装:</li></ul><p><img src="'+cs+'" alt=""></p><ul><li>安装过程:</li></ul><p><img src="'+hs+'" alt=""></p><ul><li>安装完成:</li></ul><p><img src="'+ds+'" alt=""></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 "1.4.2.3 配置""></a></h4><ul><li>安装<code>中文</code>插件:</li></ul><p><img src="'+os+'" alt=""></p><p><img src="'+ks+'" alt=""></p><ul><li>安装 <code>C/C++</code> 插件:</li></ul><p><img src="'+us+'" alt=""></p><p><img src="'+gs+'" alt=""></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 "1.4.3 Microsoft Visual Studio""></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 "1.4.3.1 概述""></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 "1.4.3.2 安装""></a></h4><ul><li>鼠标双击,进入安装:</li></ul><p><img src="'+bs+'" alt=""></p><ul><li>继续:</li></ul><p><img src="'+ms+'" alt=""></p><ul><li>等待:</li></ul><p><img src="'+Es+'" alt=""></p><ul><li>工作负荷(使用 C++ 的桌面开发):</li></ul><p><img src="'+Cs+'" alt=""></p><ul><li>单个组件:</li></ul><p><img src="'+Fs+'" alt=""></p><ul><li>语言包:</li></ul><p><img src="'+ys+'" alt=""></p><ul><li>安装位置(修改默认的安装位置):</li></ul><p><img src="'+_s+'" alt=""></p><ul><li>如果不是第一次安装,可能会出现<code>共享组件、工具和 SDK</code>不可以修改,即:</li></ul><p><img src="'+vs+'" alt=""></p><ul><li>此时,就需要打开<code>注册表编辑器</code>,将如下图中的除了第一个选项,全部删除,然后关闭再重新安装,即:</li></ul><p><img src="'+Bs+'" alt=""></p><ul><li>开始安装:</li></ul><p><img src="'+fs+'" alt=""></p><ul><li>安装中:</li></ul><p><img src="'+As+'" alt=""></p><ul><li>安装完成,然后关闭:</li></ul><p><img src="'+Ds+'" alt=""></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 "1.4.3.3 配置""></a></h4><ul><li>在开始菜单处,启动 VS :</li></ul><p><img src="'+xs+'" alt=""></p><ul><li>登录或跳过该选项(有微软账号就注册,没有就暂时跳过):</li></ul><p><img src="'+qs+'" alt=""></p><ul><li>继续:</li></ul><p><img src="'+ws+'" alt=""></p><ul><li>注册 VS :</li></ul><p><img src="'+Ss+'" alt=""></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=""></p><p><img src="'+Ps+'" alt=""></p><h1 id="第二章-c-语言入门-helloworld-⭐" tabindex="-1">第二章:C 语言入门(HelloWorld,⭐) <a class="header-anchor" href="#第二章-c-语言入门-helloworld-⭐" aria-label="Permalink to "第二章:C 语言入门(HelloWorld,⭐)""></a></h1><h2 id="_2-1-手动版" tabindex="-1">2.1 手动版 <a class="header-anchor" href="#_2-1-手动版" aria-label="Permalink to "2.1 手动版""></a></h2><ul><li>① 新建一个 <code>HelloWorld.c</code> 的文件:</li></ul><p><img src="'+Ts+`" alt=""></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;"> <stdio.h></span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">int</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> main</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(){</span></span>
|
||
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"Hello World"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
||
<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=""></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=""></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=""></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 "2.2 VS Code 版""></a></h2><ul><li>① 新建一个<code>空</code>文件夹(目录),用于存放代码:</li></ul><p><img src="'+Rs+'" alt=""></p><ul><li>② 通过 <code>vscode</code> 打开该目录:</li></ul><p><img src="'+Os+'" alt=""></p><ul><li>③ 在 <code>vscode</code> 中新建 <code>HelloWorld.c</code> 文件:</li></ul><p><img src="'+Ns+'" alt=""></p><ul><li>④ 设置 VSCode 中 C/C++ 的代码格式为行尾风格(可选):</li></ul><p><img src="'+Is+'" alt=""></p><p><img src="'+Vs+`" alt=""></p><ul><li>⑤ 编写如下的代码,并保存:</li></ul><div class="language-c vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">#include</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> <stdio.h></span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">int</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> main</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(){</span></span>
|
||
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"Hello World"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
||
<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=""></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=""></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=""></p><ul><li>⑧ 安装 Code Runner 插件(步骤略),实现右键直接编译执行(可选):</li></ul><p><img src="'+Hs+'" alt=""></p><h2 id="_2-3-vs-版" tabindex="-1">2.3 VS 版 <a class="header-anchor" href="#_2-3-vs-版" aria-label="Permalink to "2.3 VS 版""></a></h2><ul><li>① 新建空项目:</li></ul><p><img src="'+Ks+'" alt=""></p><p><img src="'+Qs+'" alt=""></p><p><img src="'+js+'" alt=""></p><p><img src="'+Ys+'" alt=""></p><ul><li>② 打开<code>解决方案资源管理器</code>:</li></ul><p><img src="'+Js+'" alt=""></p><p><img src="'+Zs+'" alt=""></p><ul><li>③ 新建 <code>HelloWorld.c</code> 文件:</li></ul><p><img src="'+zs+`" alt=""></p><ul><li>④ 编写如下代码,并保存:</li></ul><div class="language-c vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">#include</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> <stdio.h></span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">int</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> main</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(){</span></span>
|
||
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"Hello World"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
||
<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="`+sa+'" alt=""></p><ul><li>⑤ 编译和执行:</li></ul><p><img src="'+aa+'" alt=""></p><h2 id="_2-4-clion-版" tabindex="-1">2.4 CLion 版 <a class="header-anchor" href="#_2-4-clion-版" aria-label="Permalink to "2.4 CLion 版""></a></h2><ul><li>① 新建空项目:</li></ul><p><img src="'+ia+'" alt=""></p><p><img src="'+na+'" alt=""></p><p><img src="'+la+`" alt=""></p><ul><li>② 编写如下代码,并保存:</li></ul><div class="language-c vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">#include</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> <stdio.h></span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">int</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> main</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(){</span></span>
|
||
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"Hello World"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
||
<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="`+ea+'" alt=""></p><ul><li>③ 编译和运行:</li></ul><p><img src="'+pa+'" alt=""></p><ul><li>④ 默认情况下,一个项目只能有一个 c 源文件包含 main 函数,但是 CLion 可以有多个,如下:</li></ul><p><img src="'+ta+'" alt=""></p><ul><li>⑤ 如果之后,有中文乱码问题,那么请做如下步骤:</li></ul><p><img src="'+ra+'" alt=""></p><p><img src="'+ca+`" alt=""></p><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><p>内容如下所示:</p><div class="language-txt vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">txt</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span>-Dfile.encoding=UTF-8</span></span>
|
||
<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 "第三章:五花八门的 C 语言编译器(⭐)""></a></h1><h2 id="_3-1-概述" tabindex="-1">3.1 概述 <a class="header-anchor" href="#_3-1-概述" aria-label="Permalink to "3.1 概述""></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 "3.2 桌面操作系统""></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>MFC(Microsoft 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>③ 丰富的工具链: 和 GDB(GNU 调试器)、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 "3.3 嵌入式系统""></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 "3.4 C 语言为什么有那么多的编译器?""></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><p>各个厂商可以为了自己的利益、根据自己的喜好来开发编译器。</p></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><p>在学习的时候,无所谓使用那个 C 语言编译器了;但是,如果要开发实际项目(开源或商业),最好使用 GCC 编译器,因为其功能最强大、开源、跨平台、免费,支持最新的 C 语言标准。</p></div><h1 id="第四章-注释-⭐" tabindex="-1">第四章:注释(⭐) <a class="header-anchor" href="#第四章-注释-⭐" aria-label="Permalink to "第四章:注释(⭐)""></a></h1><h2 id="_4-1-概述" tabindex="-1">4.1 概述 <a class="header-anchor" href="#_4-1-概述" aria-label="Permalink to "4.1 概述""></a></h2><ul><li>编程语言中,<code>注释</code>是一种<code>特殊</code>的文本,它不会被编译器执行,而仅用于代码的解释和文档说明。</li></ul><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><ul><li>① 注释是一个程序员必须有具有的良好编程习惯。</li><li>② 在实际开发中,程序员可以将自己的思路通过<code>注释</code>整理出来,然后再用<code>代码</code>去实现。</li></ul></div><h2 id="_4-2-单行注释" tabindex="-1">4.2 单行注释 <a class="header-anchor" href="#_4-2-单行注释" aria-label="Permalink to "4.2 单行注释""></a></h2><ul><li>C 语言中的单行注释的格式,如下所示:</li></ul><div class="language-c vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">// 单行注释</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><p>在 CLion 中的快捷键是 <code>Ctrl + /</code> 。</p></div><ul><li>示例:</li></ul><div class="language-c vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">#include</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> <stdio.h></span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // 这是编译预处理指令</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">int</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> main</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">() {</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // 定义主函数</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"你好,世界!!!"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // 输出所指定的一行信息</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // 函数执行完毕时返回函数值0</span></span>
|
||
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><h2 id="_4-3-多行注释" tabindex="-1">4.3 多行注释 <a class="header-anchor" href="#_4-3-多行注释" aria-label="Permalink to "4.3 多行注释""></a></h2><ul><li>C 语言中的多行注释的格式,如下所示:</li></ul><div class="language-c vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">/*</span></span>
|
||
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> 这是第一行注释</span></span>
|
||
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> 这是第二行注释</span></span>
|
||
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> 这是第三行注释</span></span>
|
||
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">*/</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><ul><li>① 多行注释不能嵌套使用!!!</li><li>② 在 CLion 中的快捷键是 <code>Ctrl + Alt + /</code> 。</li></ul></div><ul><li>示例:</li></ul><div class="language-c vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">#include</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> <stdio.h></span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> </span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">int</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> main</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">() { </span></span>
|
||
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> </span></span>
|
||
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> /*</span></span>
|
||
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> printf(1);</span></span>
|
||
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> printf(2);</span></span>
|
||
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> */</span></span>
|
||
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"你好,世界!!!"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">); </span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">; </span></span>
|
||
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><h1 id="第五章-helloworld-的规范-⭐" tabindex="-1">第五章:HelloWorld 的规范(⭐) <a class="header-anchor" href="#第五章-helloworld-的规范-⭐" aria-label="Permalink to "第五章:HelloWorld 的规范(⭐)""></a></h1><h2 id="_5-1-规范的代码风格" tabindex="-1">5.1 规范的代码风格 <a class="header-anchor" href="#_5-1-规范的代码风格" aria-label="Permalink to "5.1 规范的代码风格""></a></h2><h3 id="_5-1-1-正确的缩进和空白" tabindex="-1">5.1.1 正确的缩进和空白 <a class="header-anchor" href="#_5-1-1-正确的缩进和空白" aria-label="Permalink to "5.1.1 正确的缩进和空白""></a></h3><ul><li>① 使用一次 <code>tab</code> 操作,实现缩进,默认整体向右边移动;如果使用 <code>shift + tab</code> 则整体向左移动。</li><li>② 运算符两边习惯各加一个空格,如:<code>2 + 4 = 6</code>。</li></ul><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><p>各种 IDE 都有格式化的快捷键,如:CLion 的格式化快捷键是 <code>Ctrl + Alt + L</code> 。</p></div><ul><li>示例:</li></ul><div class="language-c vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">#include</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> <stdio.h></span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">int</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> main</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">() {</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> int</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> a </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 1</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> int</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> b </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 2</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> int</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> c </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> a </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">+</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> b;</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"c = </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%d</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, c);</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
||
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><h3 id="_5-1-2-代码风格" tabindex="-1">5.1.2 代码风格 <a class="header-anchor" href="#_5-1-2-代码风格" aria-label="Permalink to "5.1.2 代码风格""></a></h3><ul><li>在 C 语言中,有两种代码风格:<code>行尾风格</code>和<code>次行风格</code>。</li></ul><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><p>看个人爱好,任选一种即可,本人喜欢<code>行尾分格</code>!!!</p></div><ul><li>示例:行尾风格</li></ul><div class="language-c vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">int</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> main</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(){ </span></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> if</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(a </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">></span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> b) {</span></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> a;</span></span>
|
||
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> } </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">else</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> b;</span></span>
|
||
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> } </span></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">; </span></span>
|
||
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><ul><li>示例:次行风格</li></ul><div class="language-c vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">int</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> main</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">()</span></span>
|
||
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">{ </span></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> if</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(a </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">></span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> b) </span></span>
|
||
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> a;</span></span>
|
||
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> } </span></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> else</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> </span></span>
|
||
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> b;</span></span>
|
||
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> } </span></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">; </span></span>
|
||
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><h2 id="_5-2-代码细节剖析" tabindex="-1">5.2 代码细节剖析 <a class="header-anchor" href="#_5-2-代码细节剖析" aria-label="Permalink to "5.2 代码细节剖析""></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 "5.2.1 main() 函数""></a></h3><ul><li>在 C 语言中,一个程序或工程可以定义很多函数,但是有且只有一个 main() 函数,作为程序执行的入口,并且在 main() 函数结尾结束整个程序的运行,即:</li></ul><div class="language-c vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">int</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> main</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(){</span></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
||
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><ul><li>如果 main() 函数是空括号,即表示 main() 函数不接收任何参数。</li><li>在 main() 函数之前的 <code>int</code> 称为关键字,代表数据类型是<code>整型</code>,它是 main() 函数的返回值的类型,即在执行 main() 函数之后一定会得到一个整数类型的值,即函数值。</li></ul><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><ul><li>① 在 C 语言中,人们约定,如果 <code>return 0</code>,就表示 main() 函数终止运行,且运行成功;如果返回其它非零整数,则表示运行失败。</li><li>② 默认情况下,如果 main() 函数中省略 <code>return 0</code> ,则编译器会自动加上;但是,为了保持统一的代码风格,不建议省略。</li></ul></div><h3 id="_5-2-2-函数体" tabindex="-1">5.2.2 函数体 <a class="header-anchor" href="#_5-2-2-函数体" aria-label="Permalink to "5.2.2 函数体""></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 "5.2.3 printf() 函数""></a></h3><ul><li>printf() 函数的格式,如下所示:</li></ul><div class="language-c vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> (</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">const</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> char</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> *</span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;">__format</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, ...)</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><ul><li>printf() 函数是产生格式化输出的函数,作用是将参数文本输出到屏幕,f 表示 format(格式化),表示可以指定输出文本的格式,即:</li></ul><div class="language-c vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> (</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"Hello World"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // 将字符串输出到控制台,行尾不换行</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><ul><li>如果想让光标移动到下一行的开头,可以在输出文本的结尾,可以添加一个换行符 <code>\\n</code>,即:</li></ul><div class="language-c vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"Hello World</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">\\n</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</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 "5.2.4 标准库和头文件""></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 "5.2.4.1 概述""></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;"> <stdio.h></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 "5.2.4.2 标准库(Standard Library)""></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 "5.2.4.3 头文件(Header Files)""></a></h4><ul><li><p><code>头文件</code>是包含函数声明、宏定义、数据类型定义等内容的文件。头文件的作用是为源代码提供必要的声明和定义,以便编译器能够正确解析和链接函数调用。头文件通常以<code>.h</code>作为文件扩展名。</p></li><li><p>换言之,头文件包含函数声明、宏定义和数据类型定义,但不包含函数的实现。头文件告知编译器如何使用标准库中的函数和定义,确保编译时的正确性。头文件需要在源代码文件中使用<code>#include</code>指令显式包含,如:<code>#include <stdio.h></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 "5.2.4.4 预处理命令""></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;"> <filename></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><div class="language-c vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">// 用于包含用户自定义的头文件。</span></span>
|
||
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">// 编译器首先在当前目录中查找文件,如果未找到,再在标准头文件目录中查找。</span></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">#include</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> "filename"</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h1 id="第六章-clion-高级配置-⭐" tabindex="-1">第六章:CLion 高级配置(⭐) <a class="header-anchor" href="#第六章-clion-高级配置-⭐" aria-label="Permalink to "第六章:CLion 高级配置(⭐)""></a></h1><h2 id="_6-1-安装和配置-wsl2" tabindex="-1">6.1 安装和配置 WSL2 <a class="header-anchor" href="#_6-1-安装和配置-wsl2" aria-label="Permalink to "6.1 安装和配置 WSL2""></a></h2><h3 id="_6-1-1-概述" tabindex="-1">6.1.1 概述 <a class="header-anchor" href="#_6-1-1-概述" aria-label="Permalink to "6.1.1 概述""></a></h3><ul><li>WSL 2,全称为 Windows Subsystem for Linux 2,是微软提供的一种技术,允许用户在 Windows 操作系统上运行 Linux 内核。WSL 2 是 WSL 的升级版,它引入了一个真正的 Linux 内核来代替 WSL 1 中使用的兼容层,从而提供更高的性能和更广泛的系统调用支持。</li><li>其架构图,如下所示:</li></ul><p><img src="`+ha+'" alt=""></p><ul><li><p>WSL2 的功能,如下所示:</p><ul><li>① <strong>真实的Linux内核</strong>:WSL 2 使用了微软开发的轻量级虚拟机,它包含了一个完整的 Linux 内核。这意味着 WSL 2 能够运行更多的 Linux 应用程序,并且支持更多的系统调用。</li><li>② <strong>文件系统性能提升</strong>:WSL 2 的文件系统性能比 WSL 1 有显著提升。对于 I/O 密集型的操作,如:编译代码或数据库操作,WSL 2 能够提供更快的速度。</li><li>③ <strong>兼容性增强</strong>:由于使用了真实的 Linux 内核,WSL 2 对 Linux 应用程序的兼容性大幅提高。许多在 WSL 1上不能运行或需要调整的应用程序,可以在 WSL 2上直接运行。</li><li>④ <strong>网络功能改进</strong>:WSL 2 提供了更好的网络集成,能够更容易地与 Windows 上的其他网络资源进行交互。</li><li>⑤ <strong>资源使用优化</strong>:WSL 2 使用轻量级虚拟机,比传统的虚拟机占用更少的资源,同时提供了类似的隔离和安全性。</li></ul></li><li><p>WSL 的用途,如下所示:</p><ul><li>① <strong>开发环境</strong>:WSL 2 为开发者提供了一个原生的Linux开发环境,而无需离开 Windows 。这对于需要在 Linux 上开发、测试或运行应用程序的开发者非常有帮助。</li><li>② <strong>学习和实验</strong>:用户可以使用 WSL 2 在Windows上学习和实验 Linux 命令行工具和应用程序,而无需设置双重引导系统或安装虚拟机。</li><li>③ <strong>多平台开发</strong>:对于跨平台开发者来说,WSL 2 允许他们在一个操作系统上同时进行 Windows 和 Linux 平台的开发和测试,提高工作效率。</li><li>④ <strong>运行 Linux 工具和应用程序</strong>:WSL 2 支持在 Windows 上直接运行各种 Linux 工具和应用程序,如:Docker、数据库、编程语言环境等。</li></ul></li></ul><h3 id="_6-1-2-wsl2-的安装" tabindex="-1">6.1.2 WSL2 的安装 <a class="header-anchor" href="#_6-1-2-wsl2-的安装" aria-label="Permalink to "6.1.2 WSL2 的安装""></a></h3><ul><li>① BIOS 或 UEFI 中,开启虚拟化:步骤略。</li></ul><p><img src="'+da+'" alt=""></p><ul><li>② 查看是否开启了虚拟化:</li></ul><p><img src="'+oa+'" alt=""></p><ul><li>③ 启用适用于 Linux 的 Windows 子系统:</li></ul><div class="language-powershell vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">powershell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span 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="'+ka+'" alt=""></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="'+ua+'" alt=""></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="'+ga+'" alt=""></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="'+ba+'" alt=""></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="'+ma+'" alt=""></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=""></p><ul><li>⑨ 在 Microsoft Store 中搜索并安装(可选):</li></ul><p><img src="'+Ca+'" alt=""></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=""></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 "6.1.3 配置 WSL2""></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;"> "Development Tools"</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -y</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 安装开发工具包</span></span>
|
||
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sudo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> dnf</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> install</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> gcc</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> gcc-c++</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -y</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 安装GCC相关工具链</span></span>
|
||
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sudo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> dnf</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> install</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> cmake</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -y</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 安装 cmake</span></span>
|
||
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sudo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> dnf</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> install</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> make</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -y</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 安装 make</span></span>
|
||
<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="`+ya+'" alt=""></p><ul><li>可以通过 CLion 测试是否安装成功:</li></ul><p><img src="'+_a+`" alt=""></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 "6.1.4 配置 WSL2""></a></h3><ul><li>本人的安装的是 Ubuntu 24.04,所以需要执行如下命令,以便安装 cmake 相关工具链:</li></ul><div class="language-shell vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sudo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> apt</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> update</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> && </span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sudo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> apt</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> upgrade</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -y</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 更新包管理器</span></span>
|
||
<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="`+va+'" alt=""></p><ul><li>可以通过 CLion 测试是否安装成功:</li></ul><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><p>默认仓库中的 GDB 的版本会很高,CLion 会不支持,那么就需要手动下载源码,自行编译,本次不再演示!!!</p></div><p><img src="'+Ba+'" alt=""></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 "6.2 切换 CLion 中的 cmake 的工具链""></a></h2><ul><li>可以在 CLoin 中切换 cmake 的工具链,以便支持不同平台的 cmake ,即:</li></ul><p><img src="'+fa+'" alt=""></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 "6.3 修改 CMakeLists.txt 文件""></a></h2><ul><li>前文也提到了,在一个 C 语言项目中,只能有一个 main() 函数;但是,我们可以修改 <code>CMakeLists.txt</code> 文件的内容,以便其支持在一个 C 语言项目中,可以有多个包含 main() 函数的文件,如下所示:</li></ul><p><img src="'+Aa+`" alt=""></p><ul><li><code>CMakeLists.txt</code> 的内容,如下所示:</li></ul><div class="language-txt vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">txt</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span>cmake_minimum_required(VERSION 3.10)</span></span>
|
||
<span class="line"><span></span></span>
|
||
<span class="line"><span># 项目名称和版本号</span></span>
|
||
<span class="line"><span>project(c-study VERSION 1.0 LANGUAGES C)</span></span>
|
||
<span class="line"><span></span></span>
|
||
<span class="line"><span># 设置 C 标准</span></span>
|
||
<span class="line"><span>set(CMAKE_C_STANDARD 23)</span></span>
|
||
<span class="line"><span>set(CMAKE_C_STANDARD_REQUIRED True)</span></span>
|
||
<span class="line"><span></span></span>
|
||
<span class="line"><span># 辅助函数,用于递归查找所有源文件</span></span>
|
||
<span class="line"><span>function(collect_sources result dir)</span></span>
|
||
<span class="line"><span> file(GLOB_RECURSE new_sources "\${dir}/*.c")</span></span>
|
||
<span class="line"><span> set(\${result} \${\${result}} \${new_sources} PARENT_SCOPE)</span></span>
|
||
<span class="line"><span>endfunction()</span></span>
|
||
<span class="line"><span></span></span>
|
||
<span class="line"><span># 查找顶层 include 目录(如果存在)</span></span>
|
||
<span class="line"><span>if (EXISTS "\${CMAKE_SOURCE_DIR}/include")</span></span>
|
||
<span class="line"><span> include_directories(\${CMAKE_SOURCE_DIR}/include)</span></span>
|
||
<span class="line"><span>endif ()</span></span>
|
||
<span class="line"><span></span></span>
|
||
<span class="line"><span># 查找所有源文件</span></span>
|
||
<span class="line"><span>set(SOURCES)</span></span>
|
||
<span class="line"><span>collect_sources(SOURCES \${CMAKE_SOURCE_DIR})</span></span>
|
||
<span class="line"><span></span></span>
|
||
<span class="line"><span># 用于存储已经处理过的可执行文件名,防止重复</span></span>
|
||
<span class="line"><span>set(EXECUTABLE_NAMES)</span></span>
|
||
<span class="line"><span></span></span>
|
||
<span class="line"><span># 创建可执行文件</span></span>
|
||
<span class="line"><span>foreach (SOURCE \${SOURCES})</span></span>
|
||
<span class="line"><span> # 获取文件的相对路径</span></span>
|
||
<span class="line"><span> file(RELATIVE_PATH REL_PATH \${CMAKE_SOURCE_DIR} \${SOURCE})</span></span>
|
||
<span class="line"><span> # 将路径中的斜杠替换为下划线,生成唯一的可执行文件名</span></span>
|
||
<span class="line"><span> string(REPLACE "/" "_" EXECUTABLE_NAME \${REL_PATH})</span></span>
|
||
<span class="line"><span> string(REPLACE "\\\\" "_" EXECUTABLE_NAME \${EXECUTABLE_NAME})</span></span>
|
||
<span class="line"><span> string(REPLACE "." "_" EXECUTABLE_NAME \${EXECUTABLE_NAME})</span></span>
|
||
<span class="line"><span></span></span>
|
||
<span class="line"><span> # 处理与 CMakeLists.txt 文件同名的问题</span></span>
|
||
<span class="line"><span> if (\${EXECUTABLE_NAME} STREQUAL "CMakeLists_txt")</span></span>
|
||
<span class="line"><span> set(EXECUTABLE_NAME "\${EXECUTABLE_NAME}_exec")</span></span>
|
||
<span class="line"><span> endif ()</span></span>
|
||
<span class="line"><span></span></span>
|
||
<span class="line"><span> # 检查是否已经创建过同名的可执行文件</span></span>
|
||
<span class="line"><span> if (NOT EXECUTABLE_NAME IN_LIST EXECUTABLE_NAMES)</span></span>
|
||
<span class="line"><span> list(APPEND EXECUTABLE_NAMES \${EXECUTABLE_NAME})</span></span>
|
||
<span class="line"><span></span></span>
|
||
<span class="line"><span> # 创建可执行文件</span></span>
|
||
<span class="line"><span> add_executable(\${EXECUTABLE_NAME} \${SOURCE})</span></span>
|
||
<span class="line"><span></span></span>
|
||
<span class="line"><span> # 查找源文件所在的目录,并添加为包含目录(头文件可能在同一目录下)</span></span>
|
||
<span class="line"><span> get_filename_component(DIR \${SOURCE} DIRECTORY)</span></span>
|
||
<span class="line"><span> target_include_directories(\${EXECUTABLE_NAME} PRIVATE \${DIR})</span></span>
|
||
<span class="line"><span></span></span>
|
||
<span class="line"><span> # 检查并添加子目录中的 include 目录(如果存在)</span></span>
|
||
<span class="line"><span> if (EXISTS "\${DIR}/include")</span></span>
|
||
<span class="line"><span> target_include_directories(\${EXECUTABLE_NAME} PRIVATE \${DIR}/include)</span></span>
|
||
<span class="line"><span> endif ()</span></span>
|
||
<span class="line"><span></span></span>
|
||
<span class="line"><span> # 检查并添加 module 目录中的所有 C 文件(如果存在)</span></span>
|
||
<span class="line"><span> if (EXISTS "\${DIR}/module")</span></span>
|
||
<span class="line"><span> file(GLOB_RECURSE MODULE_SOURCES "\${DIR}/module/*.c")</span></span>
|
||
<span class="line"><span> target_sources(\${EXECUTABLE_NAME} PRIVATE \${MODULE_SOURCES})</span></span>
|
||
<span class="line"><span> endif ()</span></span>
|
||
<span class="line"><span> endif ()</span></span>
|
||
<span class="line"><span>endforeach ()</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br><span class="line-number">57</span><br><span class="line-number">58</span><br><span class="line-number">59</span><br><span class="line-number">60</span><br><span class="line-number">61</span><br><span class="line-number">62</span><br><span class="line-number">63</span><br><span class="line-number">64</span><br></div></div><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 "6.4 配置 .clang-format 文件""></a></h2><ul><li>配置 <code>.clang-format</code> 格式化文件,以便写代码的时候,可以自动保存并格式化 C 程序代码,如下所示:</li></ul><p><img src="`+Da+`" alt=""></p><ul><li><code>.clang-format</code> 的内容,如下所示:</li></ul><div class="language-txt vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">txt</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span>BasedOnStyle: Google</span></span>
|
||
<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="`+xa+'" alt=""></p><h2 id="_6-5-配置-gitignore-文件" tabindex="-1">6.5 配置 .gitignore 文件 <a class="header-anchor" href="#_6-5-配置-gitignore-文件" aria-label="Permalink to "6.5 配置 .gitignore 文件""></a></h2><ul><li>需要在项目中,配置 <code>.gitignore</code> 文件,以便在提交代码到 Git 仓库的时候,忽略某些文件或目录,如下所示:</li></ul><p><img src="'+qa+`" alt=""></p><ul><li><code>.gitignore</code> 文件的内容,如下所示:</li></ul><div class="language-txt vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">txt</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span>.vscode</span></span>
|
||
<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 "6.6 演示""></a></h2><ul><li>我们可以在项目中,临时创建或复制一个文件,看上述配置是否生效,即:</li></ul><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><p>如果是复制并粘贴一个文件到项目中,请点击<code>重新加载 CMake 项目</code>!!!</p></div><p><img src="`+wa+'" alt=""></p><h1 id="第七章-c-语言的编译过程-⭐" tabindex="-1">第七章:C 语言的编译过程(⭐) <a class="header-anchor" href="#第七章-c-语言的编译过程-⭐" aria-label="Permalink to "第七章:C 语言的编译过程(⭐)""></a></h1><h2 id="_7-1-概述" tabindex="-1">7.1 概述 <a class="header-anchor" href="#_7-1-概述" aria-label="Permalink to "7.1 概述""></a></h2><ul><li>C 程序的编译过程,如下所示:</li></ul><p><img src="'+Sa+'" alt=""></p><ul><li>过程 ① :编写(编辑)源代码,即:编写 C 语言源程序代码,并以文件的形式存储在磁盘中。</li></ul><div class="important custom-block github-alert"><p class="custom-block-title">IMPORTANT</p><p></p><p>源程序需要以 <code>.c</code> 作为扩展名。</p></div><ul><li>过程 ② :编译,即:将 C 语言源程序转换为<code>目标程序(或目标文件)</code>。如果程序没有错误,没有任何提示,就会生成一个扩展名为 <code>.obj</code>或 <code>.o</code> 的二进制文件。C 语言中的每条可执行语句经过编译之后,最终都会转换为二进制的机器指令。</li></ul><div class="important custom-block github-alert"><p class="custom-block-title">IMPORTANT</p><p></p><ul><li><p>① 其实,<code>编译阶段</code>包含了<code>预处理</code>、<code>编译</code>和<code>汇编</code>。</p></li><li><p>② <code>预处理</code>是编译过程的第一个阶段。在这个阶段,预处理器处理源代码中的指令(例如:<code>#include</code>、<code>#define</code>等),主要任务包括:</p><ul><li><p>头文件包含:将头文件的内容插入到源文件中。例如:<code>#include <stdio.h></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><stdio.h></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 "7.2 GCC 编译器的介绍""></a></h2><ul><li>编辑器,如:vim 、vscode 等,是指我们用它来编写源程序的(编辑代码),而我们写的代码语句,电脑是不懂的,我们需要把它转成电脑能懂的语句,编译器就是这样的转化工具。换言之,我们用编辑器编写程序,由编译器编译后才可以运行!</li><li>编译器是将易于编写、阅读和维护的高级计算机语言翻译为计算机能解读、运行的低级机器语言的程序。</li><li>gcc(GNU Compiler Collection,GNU 编译器套件),是由 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 "7.3 通过 gcc 直接生成可执行文件""></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="'+La+'" alt=""></p><h2 id="_7-4-通过-gcc-分步编译" tabindex="-1">7.4 通过 gcc 分步编译 <a class="header-anchor" href="#_7-4-通过-gcc-分步编译" aria-label="Permalink to "7.4 通过 gcc 分步编译""></a></h2><h3 id="_7-3-1-概述" tabindex="-1">7.3.1 概述 <a class="header-anchor" href="#_7-3-1-概述" aria-label="Permalink to "7.3.1 概述""></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;"> 源文件.c</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -o</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 源文件.i</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 通常以 .i 结尾表示这个文件是一个中间状态</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</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:#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 style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 在 Linux 中,通常以 .s 结尾;在 Windows 中,通常以 .asm 结尾</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</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:#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 style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 在 Linux 中,通常以 .o 结尾;在 Windows 中,通常以 .obj 结尾</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</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:#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 style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 在 Linux 中,通常以 .out 结尾;在 Windows 中,通常以 .exe 结尾</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</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 "7.4.2 应用示例""></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=""></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=""></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=""></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=""></p><h1 id="第八章-附录" tabindex="-1">第八章:附录 <a class="header-anchor" href="#第八章-附录" aria-label="Permalink to "第八章:附录""></a></h1><h2 id="_8-1-wsl2-代理问题" tabindex="-1">8.1 WSL2 代理问题 <a class="header-anchor" href="#_8-1-wsl2-代理问题" aria-label="Permalink to "8.1 WSL2 代理问题""></a></h2><ul><li>在安装和配置 WSL2 之后,可能会出现如下的提示,即:</li></ul><p><img src="'+Ua+`" alt=""></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="`+Ra+'" alt=""></p><ul><li>在命令行中,执行如下的命令:</li></ul><div class="language-shell vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">wsl</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --shutdown</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><p><img src="'+Oa+'" alt=""></p><ul><li>此时,再打开终端,就没有这种提示了:</li></ul><p><img src="'+Na+'" alt=""></p><h2 id="_8-2-clion-调试问题" tabindex="-1">8.2 CLion 调试问题 <a class="header-anchor" href="#_8-2-clion-调试问题" aria-label="Permalink to "8.2 CLion 调试问题""></a></h2><ul><li>在 CLion 中进行 run(运行)程序的时候,对于 <code>printf</code> 函数或 <code>scanf</code> 函数很正常,如下所示:</li></ul><p><img src="'+Ia+'" alt=""></p><ul><li>但是,当我们 debug(调试) 的时候,对于 <code>printf</code> 函数或 <code>scanf</code> 函数会一直没有输出,如下所示:</li></ul><p><img src="'+Va+`" alt=""></p><ul><li>原因是 <code>scanf</code> 函数并不是直接让用户从键盘输入数据,而是先检查缓冲区,处理缓冲区中的数据;当遇到 <code>scanf</code> 函数时,程序会先检查输入缓冲区中是否有数据,所以解决方案就是<code>禁用缓冲区</code>,如下所示:</li></ul><div class="language-c vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">#include</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> <stdio.h></span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">int</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> main</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">() {</span></span>
|
||
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // 禁用 stdout 缓冲区</span></span>
|
||
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> setbuf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(stdout, </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">NULL</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> int</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> a, b, c;</span></span>
|
||
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"请输入整数 a 、b 和 c 的值:"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span></span>
|
||
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> scanf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%d</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> %d</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> %d</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">&</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">a, </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">&</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">b, </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">&</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">c);</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> int</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> result </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> a </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">*</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> b </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">*</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> c;</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%d</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> × </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%d</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> × </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%d</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> = </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%d</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, a, b, c, result);</span></span>
|
||
<span class="line"></span>
|
||
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
||
<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=""></p><h2 id="_8-3-内存泄露检测" tabindex="-1">8.3 内存泄露检测 <a class="header-anchor" href="#_8-3-内存泄露检测" aria-label="Permalink to "8.3 内存泄露检测""></a></h2><h3 id="_8-3-1-概述" tabindex="-1">8.3.1 概述 <a class="header-anchor" href="#_8-3-1-概述" aria-label="Permalink to "8.3.1 概述""></a></h3><ul><li>C 语言中的指针是否使用是个颇具争议的话题,现代化的高级编程语言通过各种策略和机制,在编译期就能解决指针危险的问题。但是,遗憾的是,C 语言的指针很大程度上,在运行期才会暴露问题。</li><li>幸运的是,我们可以使用 <code>Valgrind</code> 项目来进行<code>内存泄露检测</code>和<code>性能分析</code>,而 <code>Valgrind</code> 只支持 Linux 。</li></ul><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><p>win 11 中的 WSL2 就是个 Linux 环境,我们可以在上面跑各种 Linux 工具,这样我们就不需要再安装虚拟机软件了,如:VMware Workstation(它会完整的模拟一个硬件系统,并在上面跑各种 Linux ,实在是太笨重了)。</p></div><h3 id="_8-3-2-安装" tabindex="-1">8.3.2 安装 <a class="header-anchor" href="#_8-3-2-安装" aria-label="Permalink to "8.3.2 安装""></a></h3><ul><li>在 WSL2 上安装 Valgrind :</li></ul><div class="language-shell vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">dnf</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -y</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> upgrade</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> && </span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">dnf</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -y</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> install</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> valgrind</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # AlmaLinux</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><div class="language-shell vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">apt</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -y</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> update</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> && </span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">apt</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -y</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> upgrade</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> && </span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">apt</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -y</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> install</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 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=""></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=""></p><h3 id="_8-3-3-整合" tabindex="-1">8.3.3 整合 <a class="header-anchor" href="#_8-3-3-整合" aria-label="Permalink to "8.3.3 整合""></a></h3><ul><li>CLion 中将工具链设置为 WSL2 :</li></ul><p><img src="'+Ha+'" alt=""></p><ul><li>CLion 中配置 valgrind 的路径:</li></ul><p><img src="'+Ka+'" alt=""></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=""></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 "\${dir}/*.c")</span></span>
|
||
<span class="line"><span> set(\${result} \${\${result}} \${new_sources} PARENT_SCOPE)</span></span>
|
||
<span class="line"><span>endfunction()</span></span>
|
||
<span class="line"><span></span></span>
|
||
<span class="line"><span># 查找顶层 include 目录(如果存在)</span></span>
|
||
<span class="line"><span>if (EXISTS "\${CMAKE_SOURCE_DIR}/include")</span></span>
|
||
<span class="line"><span> include_directories(\${CMAKE_SOURCE_DIR}/include)</span></span>
|
||
<span class="line"><span>endif ()</span></span>
|
||
<span class="line"><span></span></span>
|
||
<span class="line"><span># 查找所有源文件</span></span>
|
||
<span class="line"><span>set(SOURCES)</span></span>
|
||
<span class="line"><span>collect_sources(SOURCES \${CMAKE_SOURCE_DIR})</span></span>
|
||
<span class="line"><span></span></span>
|
||
<span class="line"><span># 用于存储已经处理过的可执行文件名,防止重复</span></span>
|
||
<span class="line"><span>set(EXECUTABLE_NAMES)</span></span>
|
||
<span class="line"><span></span></span>
|
||
<span class="line"><span># 创建可执行文件</span></span>
|
||
<span class="line"><span>foreach (SOURCE \${SOURCES})</span></span>
|
||
<span class="line"><span> # 获取文件的相对路径</span></span>
|
||
<span class="line"><span> file(RELATIVE_PATH REL_PATH \${CMAKE_SOURCE_DIR} \${SOURCE})</span></span>
|
||
<span class="line"><span> # 将路径中的斜杠替换为下划线,生成唯一的可执行文件名</span></span>
|
||
<span class="line"><span> string(REPLACE "/" "_" EXECUTABLE_NAME \${REL_PATH})</span></span>
|
||
<span class="line"><span> string(REPLACE "\\\\" "_" EXECUTABLE_NAME \${EXECUTABLE_NAME})</span></span>
|
||
<span class="line"><span> string(REPLACE "." "_" EXECUTABLE_NAME \${EXECUTABLE_NAME})</span></span>
|
||
<span class="line"><span></span></span>
|
||
<span class="line"><span> # 处理与 CMakeLists.txt 文件同名的问题</span></span>
|
||
<span class="line"><span> if (\${EXECUTABLE_NAME} STREQUAL "CMakeLists_txt")</span></span>
|
||
<span class="line"><span> set(EXECUTABLE_NAME "\${EXECUTABLE_NAME}_exec")</span></span>
|
||
<span class="line"><span> endif ()</span></span>
|
||
<span class="line"><span></span></span>
|
||
<span class="line"><span> # 检查是否已经创建过同名的可执行文件</span></span>
|
||
<span class="line"><span> if (NOT EXECUTABLE_NAME IN_LIST EXECUTABLE_NAMES)</span></span>
|
||
<span class="line"><span> list(APPEND EXECUTABLE_NAMES \${EXECUTABLE_NAME})</span></span>
|
||
<span class="line"><span></span></span>
|
||
<span class="line"><span> # 创建可执行文件</span></span>
|
||
<span class="line"><span> add_executable(\${EXECUTABLE_NAME} \${SOURCE})</span></span>
|
||
<span class="line"><span></span></span>
|
||
<span class="line"><span> # 查找源文件所在的目录,并添加为包含目录(头文件可能在同一目录下)</span></span>
|
||
<span class="line"><span> get_filename_component(DIR \${SOURCE} DIRECTORY)</span></span>
|
||
<span class="line"><span> target_include_directories(\${EXECUTABLE_NAME} PRIVATE \${DIR})</span></span>
|
||
<span class="line"><span></span></span>
|
||
<span class="line"><span> # 检查并添加子目录中的 include 目录(如果存在)</span></span>
|
||
<span class="line"><span> if (EXISTS "\${DIR}/include")</span></span>
|
||
<span class="line"><span> target_include_directories(\${EXECUTABLE_NAME} PRIVATE \${DIR}/include)</span></span>
|
||
<span class="line"><span> endif ()</span></span>
|
||
<span class="line"><span></span></span>
|
||
<span class="line"><span> # 检查并添加 module 目录中的所有 C 文件(如果存在)</span></span>
|
||
<span class="line"><span> if (EXISTS "\${DIR}/module")</span></span>
|
||
<span class="line"><span> file(GLOB_RECURSE MODULE_SOURCES "\${DIR}/module/*.c")</span></span>
|
||
<span class="line"><span> target_sources(\${EXECUTABLE_NAME} PRIVATE \${MODULE_SOURCES})</span></span>
|
||
<span class="line"><span> endif ()</span></span>
|
||
<span class="line"><span> endif ()</span></span>
|
||
<span class="line"><span>endforeach ()</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br><span class="line-number">57</span><br><span class="line-number">58</span><br><span class="line-number">59</span><br><span class="line-number">60</span><br><span class="line-number">61</span><br><span class="line-number">62</span><br><span class="line-number">63</span><br><span class="line-number">64</span><br></div></div><ul><li>在 CLion 中正常运行代码:</li></ul><p><img src="`+ja+'" alt=""></p><ul><li>在 CLion 中通过 valgrind 运行代码:</li></ul><p><img src="'+Ya+'" alt=""></p><h2 id="_8-4-性能分析" tabindex="-1">8.4 性能分析 <a class="header-anchor" href="#_8-4-性能分析" aria-label="Permalink to "8.4 性能分析""></a></h2><h3 id="_8-4-1-概述" tabindex="-1">8.4.1 概述 <a class="header-anchor" href="#_8-4-1-概述" aria-label="Permalink to "8.4.1 概述""></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 "8.4.2 安装""></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 "8.4.2.1 AlmaLinux9""></a></h4><ul><li>在 WSL2 中的 AlmaLinux 安装 perf :</li></ul><div class="language-shell vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#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=""></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 "8.4.2.2 Ubuntu 22.04""></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;"> && </span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">apt</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -y</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> install</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> linux-tools-common</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> linux-tools-generic</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> linux-tools-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$(</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">uname</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -r</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p><img src="`+Za+'" alt=""></p><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><p>之所以报错的原因,在于 WSL2 中的 Ubuntu 的内核是定制化的,并非 Ubuntu 的母公司 Canonical 发布的标准内核,所以需要我们手动编译安装。</p></div><ul><li>查看内核版本:</li></ul><div class="language-shell vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">uname</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -sr</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><p><img src="'+za+'" alt=""></p><ul><li>设置环境变量,方便后续引用:</li></ul><div class="language-shell vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">export</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> KERNEL_VERSION</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$(</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">uname</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -r</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> |</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> cut</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -d</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'-'</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -f1</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><p><img src="'+si+`" alt=""></p><ul><li>安装依赖库:</li></ul><div class="language-shell vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">apt</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -y</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> update</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> && </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">\\</span></span>
|
||
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> apt</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -y</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> install</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> binutils-dev</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> debuginfod</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> default-jdk</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> default-jre</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> libaio-dev</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> \\</span></span>
|
||
<span class="line"><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> libbabeltrace-dev</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> libcap-dev</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> libdw-dev</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> libdwarf-dev</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> libelf-dev</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> libiberty-dev</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> \\</span></span>
|
||
<span class="line"><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> liblzma-dev</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> libnuma-dev</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> libperl-dev</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> libpfm4-dev</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> libslang2-dev</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> libssl-dev</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> \\</span></span>
|
||
<span class="line"><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> libtraceevent-dev</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> libunwind-dev</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> libzstd-dev</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> libzstd1</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> python3-setuptools</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> python3</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> \\</span></span>
|
||
<span class="line"><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> python3-dev</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> systemtap-sdt-dev</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> zlib1g-dev</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> bc</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> dwarves</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> bison</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> flex</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> libnewt-dev</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> libdwarf++0</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> \\</span></span>
|
||
<span class="line"><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> libelf++0</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> libbfb0-dev</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> python-dev-is-python3</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p><img src="`+ai+`" alt=""></p><ul><li>下载源码:</li></ul><div class="language-shell vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">git</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> clone</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> \\</span></span>
|
||
<span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --depth</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 1</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> \\</span></span>
|
||
<span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --single-branch</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --branch=linux-msft-wsl-\${</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">KERNEL_VERSION</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">}</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> \\</span></span>
|
||
<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="`+ii+'" alt=""></p><ul><li>编译内核代码:</li></ul><div class="language-shell vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">cd</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> WSL2-Linux-Kernel</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><div class="language-shell vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">make</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -j</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> $(</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">nproc</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">) </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">KCONFIG_CONFIG=Microsoft/config-wsl</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><p><img src="'+ni+'" alt=""></p><ul><li>编译 perf 工具:</li></ul><div class="language-shell vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">cd</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> tools/perf</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><div class="language-shell vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">make</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> clean</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> && </span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">make</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><p><img src="'+li+'" alt=""></p><ul><li>复制到 PATH 变量所指向的路径中:</li></ul><div class="language-shell vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">cp</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> perf</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> /usr/bin/</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><p><img src="'+ei+'" alt=""></p><h3 id="_8-4-3-整合" tabindex="-1">8.4.3 整合 <a class="header-anchor" href="#_8-4-3-整合" aria-label="Permalink to "8.4.3 整合""></a></h3><ul><li>CLion 中配置 perf 的路径:</li></ul><p><img src="'+pi+'" alt=""></p><ul><li>在 CLion 中通过 perf 运行代码:</li></ul><p><img src="'+ti+'" alt=""></p><h2 id="_8-5-win-中文乱码问题" tabindex="-1">8.5 Win 中文乱码问题 <a class="header-anchor" href="#_8-5-win-中文乱码问题" aria-label="Permalink to "8.5 Win 中文乱码问题""></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="'+ri+'" alt=""></p><p><img src="'+ci+'" alt=""></p><p><img src="'+hi+'" alt=""></p><ul><li>测试一下,是否配置成功:</li></ul><p><img src="'+di+'" alt=""></p><h2 id="_8-6-clion-中自动导入头文件" tabindex="-1">8.6 CLion 中自动导入头文件 <a class="header-anchor" href="#_8-6-clion-中自动导入头文件" aria-label="Permalink to "8.6 CLion 中自动导入头文件""></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="'+oi+'" alt=""></p><ul><li>开启自动导入头文件的步骤,如下所示:</li></ul><p><img src="'+ki+'" alt=""></p><p><img src="'+ui+'" alt=""></p>',555),mi=[bi];function Ei(Ci,Fi,yi,_i,vi,Bi){return i(),a("div",null,mi)}const Di=s(gi,[["render",Ei]]);export{Ai as __pageData,Di as default};
|