2024-08-15 08:21:13 +02:00
|
|
|
|
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",y="/c/assets/17.Cd37gMYs.png",F="/c/assets/18.CAw2z3gc.png",_="/c/assets/19.DdPThnr-.png",v="/c/assets/20.q_rvlDnY.png",B="/c/assets/21.JRHg9RZA.png",f="/c/assets/22.Baq-Hb06.png",A="/c/assets/23.c1ht53mu.png",D="/c/assets/24.CAZmH5Py.png",x="/c/assets/25.DWeCYBe9.png",q="/c/assets/26.2XM8pYni.png",w="/c/assets/27.CN0t-fuo.png",S="/c/assets/28.AcDh3LXQ.png",L="/c/assets/29.CUkDFAc6.png",z="/c/assets/30.picNTNaJ.png",P="/c/assets/31.DKX92RgJ.png",T="/c/assets/32.Bxr7QKI3.png",W="/c/assets/33.DuFWsOgW.png",M="/c/assets/34.Da9qUcfr.png",O="/c/assets/35.7_gEABSX.gif",U="/c/assets/36.C2KQYoVC.gif",R="/c/assets/37.RTUP8Vtx.png",N="/c/assets/38.Dq21DxwL.png",I="/c/assets/39.QmpUaNxU.png",V="/c/assets/40.DP0C29gZ.png",G="/c/assets/41.DwzHokd4.png",X="/c/assets/42.Bi6K26yA.png",$="/c/assets/43.Bw8DcEQ_.png",H="/c/assets/44.DPc8Z9rS.png",K="/c/assets/45.S84xHMTl.png",Q="/c/assets/46.iw3JQUkn.png",Y="/c/assets/47.ILeJt-HK.png",j="/c/assets/48.CHgm7khh.png",J="/c/assets/49.N1nqJaEg.png",Z="/c/assets/50.DHsnibMi.png",ss="/c/assets/51.BQr6Q0uV.png",as="/c/assets/52.BVemi2Lh.png",is="/c/assets/53.BHR69UXx.png",ns="/c/assets/54.CPSU5SJI.png",ls="/c/assets/55.CaQLoDlw.png",es="/c/assets/56.aRGy4XbL.png",ps="/c/assets/57.kNO9fmPR.png",ts="/c/assets/58.D4EkoC4o.png",rs="/c/assets/59.V23Lo9e_.png",cs="/c/assets/60.Du9LVeMB.png",hs="/c/assets/61.DdFg75U9.png",ds="/c/assets/62.C6bz6KtT.png",os="/c/assets/63.CB6LAhB5.png",ks="/c/assets/64.Bip8bkua.png",us="/c/assets/65.DRNHRduW.png",gs="/c/assets/66.Cth4qqEn.png",bs="/c/assets/67.DWGWZ64P.png",ms="/c/assets/68.3hDIDdvx.png",Es="/c/assets/69.Bw_dQ4ap.png",Cs="/c/assets/70.Cn_ubTyA.png",ys="/c/assets/71.C84EBqwB.png",Fs="/c/assets/72.dZfJY4eR.png",_s="/c/assets/73.B0zm0Z79.png",vs="/c/assets/74.CaTcEy1Q.png",Bs="/c/assets/75.iEc8mo9g.png",fs="/c/assets/76.Co2KxM6t.png",As="/c/assets/77.CaUm8Qhc.png",Ds="/c/assets/78.kOkqIKG6.png",xs="/c/assets/79.CyeASH-c.png",qs="/c/assets/80.CFdk60La.png",ws="/c/assets/81.Bhl-0uK_.png",Ss="/c/assets/82.Dni9KbXV.png",Ls="/c/assets/83.BXx10MCV.png",zs="/c/assets/84.CXcIKX1T.png",Ps="/c/assets/85.CQ3_QCQT.png",Ts="/c/assets/86.CQoDRJM5.gif",Ws="/c/assets/87.CPfwZvD9.gif",Ms="/c/assets/88.bxEomEjU.gif",Os="/c/assets/89.C_onhf2k.png",Us="/c/assets/90.Budj3s38.gif",Rs="/c/assets/91.CH8koQdu.gif",Ns="/c/assets/92.kUJhxMtg.png",Is="/c/assets/93.BJeJJW4_.png",Vs="/c/assets/94.BCuK6eJL.gif",Gs="/c/assets/95.rKC-z4z-.gif",Xs="/c/assets/96.BJE3xxuT.gif",$s="/c/assets/97.DJOq__O0.gif",Hs="/c/assets/98.BDt_0fRR.png",Ks="/c/assets/99.Bo5OawW8.png",Qs="/c/assets/100.sUWUfl5e.png",Ys="/c/assets/101.BK4jpnB8.png",js="/c/assets/102.EF8vdbSq.png",Js="/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.DmiFIqcy.svg",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",ya="/c/assets/125.DpakQFWz.gif",Fa="/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/
|
2024-08-11 07:41:10 +02:00
|
|
|
|
<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>
|
2024-08-15 08:21:13 +02:00
|
|
|
|
<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="`+O+'" alt="" loading="lazy"></p><ul><li>安装 gcc 等相关开发包:</li></ul><div class="language-shell vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">pacman</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -Syu</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --noconfirm</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 更新包管理器</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><div class="language-shell vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">pacman</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -Sy</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> base-devel</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --noconfirm</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 安装开发工具包</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><div class="language-shell vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">pacman</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -Sy</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> mingw-w64-ucrt-x86_64-toolchain</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --noconfirm</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 安装开发 gcc 相关工具链</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><p>也许,你会看到其他人的安装命令是 <code>pacman -Sy mingw-w64-x86_64-toolchain --noconfirm</code>,此处解释下两者的区别:</p><ul><li>① <code>mingw-w64-x86_64-toolchain</code> 使用更传统的 MSVCRT,适合需要经典 MinGW 环境的项目或依赖较老 C 运行时的应用。</li><li>② <code>mingw-w64-ucrt-x86_64-toolchain</code> 使用 Microsoft 的 UCRT,更适合现代 Windows 开发,提供更好的兼容性和性能。</li></ul></div><p><img src="'+U+'" alt="" loading="lazy"></p><h4 id="_1-2-4-2-配置-path-环境变量" tabindex="-1">1.2.4.2 配置 path 环境变量 <a class="header-anchor" href="#_1-2-4-2-配置-path-环境变量" aria-label="Permalink to "1.2.4.2 配置 path 环境变量""></a></h4><ul><li>和 <code>1.2.2.2 配置 path 环境变量</code> 步骤相同:略。</li></ul><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><p>本人需要配置的 path 环境变量是 <code>C:\\msys64\\ucrt64\\bin</code> 。</p></div><h2 id="_1-3-什么是-ide-集成开发环境" tabindex="-1">1.3 什么是 IDE(集成开发环境)? <a class="header-anchor" href="#_1-3-什么是-ide-集成开发环境" aria-label="Permalink to "1.3 什么是 IDE(集成开发环境)?""></a></h2><ul><li><p
|
2024-07-11 10:07:44 +02:00
|
|
|
|
<span class="line"></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">int</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> main</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(){</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"Hello World"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
2024-08-15 05:48:49 +02:00
|
|
|
|
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p><img src="`+Ts+'" alt="" loading="lazy"></p><ul><li>③ 通过 <code>gcc</code> 命令编译该文件:</li></ul><div class="language-shell vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">gcc</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> HelloWorld.c</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -o</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> HelloWorld.exe</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><p><img src="'+Ws+'" alt="" loading="lazy"></p><ul><li>④ 执行:</li></ul><div class="language-shell vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">./HelloWorld.exe</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><p><img src="'+Ms+'" alt="" loading="lazy"></p><h2 id="_2-2-vs-code-版" tabindex="-1">2.2 VS Code 版 <a class="header-anchor" href="#_2-2-vs-code-版" aria-label="Permalink to "2.2 VS Code 版""></a></h2><ul><li>① 新建一个<code>空</code>文件夹(目录),用于存放代码:</li></ul><p><img src="'+Os+'" alt="" loading="lazy"></p><ul><li>② 通过 <code>vscode</code> 打开该目录:</li></ul><p><img src="'+Us+'" alt="" loading="lazy"></p><ul><li>③ 在 <code>vscode</code> 中新建 <code>HelloWorld.c</code> 文件:</li></ul><p><img src="'+Rs+'" alt="" loading="lazy"></p><ul><li>④ 设置 VSCode 中 C/C++ 的代码格式为行尾风格(可选):</li></ul><p><img src="'+Ns+'" alt="" loading="lazy"></p><p><img src="'+Is+`" alt="" loading="lazy"></p><ul><li>⑤ 编写如下的代码,并保存:</li></ul><div class="language-c vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">#include</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> <stdio.h></span></span>
|
2024-07-11 10:07:44 +02:00
|
|
|
|
<span class="line"></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">int</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> main</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(){</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"Hello World"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
2024-08-15 07:26:13 +02:00
|
|
|
|
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p><img src="`+Vs+'" alt="" loading="lazy"></p><ul><li>⑥ 通过 <code>gcc</code> 命令编译该文件:</li></ul><div class="language-shell vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">gcc</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> HelloWorld.c</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -o</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> HelloWorld.exe</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><p><img src="'+Gs+'" alt="" loading="lazy"></p><ul><li>⑦ 执行:</li></ul><div class="language-shell vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">./HelloWorld.exe</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><p><img src="'+Xs+'" alt="" loading="lazy"></p><ul><li>⑧ 安装 Code Runner 插件(步骤略),实现右键直接编译执行(可选):</li></ul><p><img src="'+$s+'" alt="" loading="lazy"></p><h2 id="_2-3-vs-版" tabindex="-1">2.3 VS 版 <a class="header-anchor" href="#_2-3-vs-版" aria-label="Permalink to "2.3 VS 版""></a></h2><ul><li>① 新建空项目:</li></ul><p><img src="'+Hs+'" alt="" loading="lazy"></p><p><img src="'+Ks+'" alt="" loading="lazy"></p><p><img src="'+Qs+'" alt="" loading="lazy"></p><p><img src="'+Ys+'" alt="" loading="lazy"></p><ul><li>② 打开<code>解决方案资源管理器</code>:</li></ul><p><img src="'+js+'" alt="" loading="lazy"></p><p><img src="'+Js+'" alt="" loading="lazy"></p><ul><li>③ 新建 <code>HelloWorld.c</code> 文件:</li></ul><p><img src="'+Zs+`" alt="" loading="lazy"></p><ul><li>④ 编写如下代码,并保存:</li></ul><div class="language-c vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">#include</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> <stdio.h></span></span>
|
2024-07-11 10:07:44 +02:00
|
|
|
|
<span class="line"></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">int</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> main</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(){</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"Hello World"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
2024-08-15 05:48:49 +02:00
|
|
|
|
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p><img src="`+sa+'" alt="" loading="lazy"></p><ul><li>⑤ 编译和执行:</li></ul><p><img src="'+aa+'" alt="" loading="lazy"></p><h2 id="_2-4-clion-版" tabindex="-1">2.4 CLion 版 <a class="header-anchor" href="#_2-4-clion-版" aria-label="Permalink to "2.4 CLion 版""></a></h2><ul><li>① 新建空项目:</li></ul><p><img src="'+ia+'" alt="" loading="lazy"></p><p><img src="'+na+'" alt="" loading="lazy"></p><p><img src="'+la+`" alt="" loading="lazy"></p><ul><li>② 编写如下代码,并保存:</li></ul><div class="language-c vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">#include</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> <stdio.h></span></span>
|
2024-07-11 10:07:44 +02:00
|
|
|
|
<span class="line"></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">int</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> main</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(){</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"Hello World"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
2024-08-15 05:48:49 +02:00
|
|
|
|
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p><img src="`+ea+'" alt="" loading="lazy"></p><ul><li>③ 编译和运行:</li></ul><p><img src="'+pa+'" alt="" loading="lazy"></p><ul><li>④ 默认情况下,一个项目只能有一个 c 源文件包含 main 函数,但是 CLion 可以有多个,如下:</li></ul><p><img src="'+ta+'" alt="" loading="lazy"></p><ul><li>⑤ 如果之后,有中文乱码问题,那么请做如下步骤:</li></ul><p><img src="'+ra+'" alt="" loading="lazy"></p><p><img src="'+ca+`" alt="" loading="lazy"></p><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><p>内容如下所示:</p><div class="language-txt vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">txt</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span>-Dfile.encoding=UTF-8</span></span>
|
2024-08-14 11:26:35 +02:00
|
|
|
|
<span class="line"><span>-Dconsole.encoding=UTF-8</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div></div><h1 id="第三章-五花八门的-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><
|
2024-07-11 10:07:44 +02:00
|
|
|
|
<span class="line"></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">int</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> main</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">() {</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // 定义主函数</span></span>
|
|
|
|
|
<span class="line"></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"你好,世界!!!"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // 输出所指定的一行信息</span></span>
|
|
|
|
|
<span class="line"></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // 函数执行完毕时返回函数值0</span></span>
|
2024-08-09 11:10:03 +02:00
|
|
|
|
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><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>
|
2024-07-11 10:07:44 +02:00
|
|
|
|
<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>
|
2024-08-09 11:10:03 +02:00
|
|
|
|
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></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>
|
2024-07-11 10:07:44 +02:00
|
|
|
|
<span class="line"></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">int</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> main</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">() {</span></span>
|
|
|
|
|
<span class="line"></span>
|
|
|
|
|
<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>
|
2024-08-09 11:10:03 +02:00
|
|
|
|
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></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>
|
2024-07-11 10:07:44 +02:00
|
|
|
|
<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>
|
2024-08-09 11:10:03 +02:00
|
|
|
|
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></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>
|
2024-07-11 10:07:44 +02:00
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
2024-08-09 11:10:03 +02:00
|
|
|
|
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></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 clas
|
2024-07-11 10:07:44 +02:00
|
|
|
|
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">// 编译器将在系统的标准头文件目录中查找文件。</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">#include</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> <filename></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><div class="language-c vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">// 用于包含用户自定义的头文件。</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">// 编译器首先在当前目录中查找文件,如果未找到,再在标准头文件目录中查找。</span></span>
|
2024-08-15 05:48:49 +02:00
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">#include</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> "filename"</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h1 id="第六章-clion-高级配置-⭐" tabindex="-1">第六章:CLion 高级配置(⭐) <a class="header-anchor" href="#第六章-clion-高级配置-⭐" aria-label="Permalink to "第六章:CLion 高级配置(⭐)""></a></h1><h2 id="_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>WSL2,全称为 Windows Subsystem for Linux 2,是微软提供的一种技术,允许用户在 Windows 操作系统上运行 Linux 内核。WSL2 是 WSL1 的升级版,它引入了一个真正的 Linux 内核来代替 WSL1 中使用的兼容层,从而提供更高的性能和更广泛的系统调用支持。</li><li>和传统的虚拟化技术的对比,如下所示:</li></ul><p><img src="`+ha+'" alt="" loading="lazy"></p><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><p>WSL2 的功能,如下所示:</p><ul><li>① <strong>真实的 Linux 内核</strong>:WSL2 使用了微软开发的轻量级虚拟机,它包含了一个完整的 Linux 内核。这意味着 WSL2 能够运行更多的 Linux 应用程序,并且支持更多的系统调用。</li><li>② <strong>文件系统性能提升</strong>:WSL2 的文件系统性能比 WSL1 有显著提升。对于 I/O 密集型的操作,如:编译代码或数据库操作,WSL2 能够提供更快的速度。</li><li>③ <strong>兼容性增强</strong>:由于使用了真实的 Linux 内核,WSL2 对 Linux 应用程序的兼容性大幅提高。许多在 WSL1 上不能运行或需要调整的应用程序,可以在 WSL2 上直接运行。</li><li>④ <strong>网络功能改进</strong>:WSL2 提供了更好的网络集成,能够更容易地与 Windows 上的其他网络资源进行交互。</li><li>⑤ <strong>资源使用优化</strong>:WSL2 使用轻量级虚拟机,比传统的虚拟机占用更少的资源,同时提供了类似的隔离和安全性。</li></ul></div><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><p>WSL2 的用途,如下所示:</p><ul><li>① <strong>开发环境</strong>:WSL2 为开发者提供了一个原生的 Linux 开发环境,而无需离开 Windows 。这对于需要在 Linux 上开发、测试或运行应用程序的开发者非常有帮助。</li><li>② <strong>学习和实验</strong>:用户可以使用 WSL2 在 Windows 上学习和实验 Linux 命令行工具和应用程序,而无需设置双重引导系统或安装虚拟机。</li><li>③ <strong>多平台开发</strong>:对于跨平台开发者来说,WSL2 允许他们在一个操作系统上同时进行 Windows 和 Linux 平台的开发和测试,提高工作效率。</li><li>④ <strong>运行 Linux 工具和应用程序</strong>:WSL2 支持在 Windows 上直接运行各种 Linux 工具和应用程序,如:Docker、数据库、编程语言环境等。</li></ul></div><h3 id="_6-1-2-wsl2-的安装" tabindex="-1">6.1.2 WSL2 的安装 <a class="header-anchor" href="#_6-1-2-wsl2-的安装" aria-label="Permalink to "6.1.2 WSL2 的安装""></a></h3><ul><li>① BIOS 或 UEFI 中,开启虚拟化:步骤略。</li></ul><p><img src="'+da+'" alt="" loading="lazy"></p><ul><li>② 查看是否开启了虚拟化:</li></ul><p><img src="'+oa+'" alt="" loading="lazy"></p><ul><li>③ 启用适用于 Linux 的 Windows 子系统:</li></ul><div class="language-powershell vp-adapt
|
2024-07-11 10:07:44 +02:00
|
|
|
|
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sudo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> dnf</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> groupinstall</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> "Development Tools"</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -y</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 安装开发工具包</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sudo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> dnf</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> install</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> gcc</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> gcc-c++</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -y</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 安装GCC相关工具链</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sudo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> dnf</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> install</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> cmake</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -y</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 安装 cmake</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sudo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> dnf</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> install</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> make</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -y</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 安装 make</span></span>
|
2024-08-15 05:48:49 +02:00
|
|
|
|
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sudo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> dnf</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> install</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> gdb</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -y</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 安装 gdb</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p><img src="`+Fa+'" alt="" loading="lazy"></p><ul><li>可以通过 CLion 测试是否安装成功:</li></ul><p><img src="'+_a+`" alt="" loading="lazy"></p><h3 id="_6-1-4-配置-wsl2" tabindex="-1">6.1.4 配置 WSL2 <a class="header-anchor" href="#_6-1-4-配置-wsl2" aria-label="Permalink to "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>
|
2024-07-11 10:07:44 +02:00
|
|
|
|
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sudo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> apt</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> install</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> build-essential</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -y</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 安装开发工具包</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sudo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> apt</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> install</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> gcc</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> g++</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -y</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 安装 GCC 相关工具链</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sudo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> apt</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> install</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> cmake</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -y</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 安装 cmake</span></span>
|
2024-08-15 05:48:49 +02:00
|
|
|
|
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sudo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> apt</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> install</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> gdb</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -y</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 安装 gdb</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p><img src="`+va+'" alt="" loading="lazy"></p><ul><li>可以通过 CLion 测试是否安装成功:</li></ul><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><p>默认仓库中的 GDB 的版本会很高,CLion 会不支持,那么就需要手动下载源码,自行编译,本次不再演示!!!</p></div><p><img src="'+Ba+'" alt="" loading="lazy"></p><h2 id="_6-2-切换-clion-中的-cmake-的工具链" tabindex="-1">6.2 切换 CLion 中的 cmake 的工具链 <a class="header-anchor" href="#_6-2-切换-clion-中的-cmake-的工具链" aria-label="Permalink to "6.2 切换 CLion 中的 cmake 的工具链""></a></h2><ul><li>可以在 CLoin 中切换 cmake 的工具链,以便支持不同平台的 cmake ,即:</li></ul><p><img src="'+fa+'" alt="" loading="lazy"></p><h2 id="_6-3-修改-cmakelists-txt-文件" tabindex="-1">6.3 修改 CMakeLists.txt 文件 <a class="header-anchor" href="#_6-3-修改-cmakelists-txt-文件" aria-label="Permalink to "6.3 修改 CMakeLists.txt 文件""></a></h2><ul><li>前文也提到了,在一个 C 语言项目中,只能有一个 main() 函数;但是,我们可以修改 <code>CMakeLists.txt</code> 文件的内容,以便其支持在一个 C 语言项目中,可以有多个包含 main() 函数的文件,如下所示:</li></ul><p><img src="'+Aa+`" alt="" loading="lazy"></p><ul><li><code>CMakeLists.txt</code> 的内容,如下所示:</li></ul><div class="language-txt vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">txt</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span>cmake_minimum_required(VERSION 3.10)</span></span>
|
2024-07-11 10:07:44 +02:00
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span># 项目名称和版本号</span></span>
|
|
|
|
|
<span class="line"><span>project(c-study VERSION 1.0 LANGUAGES C)</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span># 设置 C 标准</span></span>
|
2024-07-26 01:28:10 +02:00
|
|
|
|
<span class="line"><span>set(CMAKE_C_STANDARD 23)</span></span>
|
2024-07-11 10:07:44 +02:00
|
|
|
|
<span class="line"><span>set(CMAKE_C_STANDARD_REQUIRED True)</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span># 辅助函数,用于递归查找所有源文件</span></span>
|
|
|
|
|
<span class="line"><span>function(collect_sources result dir)</span></span>
|
|
|
|
|
<span class="line"><span> file(GLOB_RECURSE new_sources "\${dir}/*.c")</span></span>
|
|
|
|
|
<span class="line"><span> set(\${result} \${\${result}} \${new_sources} PARENT_SCOPE)</span></span>
|
|
|
|
|
<span class="line"><span>endfunction()</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span># 查找顶层 include 目录(如果存在)</span></span>
|
|
|
|
|
<span class="line"><span>if (EXISTS "\${CMAKE_SOURCE_DIR}/include")</span></span>
|
|
|
|
|
<span class="line"><span> include_directories(\${CMAKE_SOURCE_DIR}/include)</span></span>
|
|
|
|
|
<span class="line"><span>endif ()</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span># 查找所有源文件</span></span>
|
|
|
|
|
<span class="line"><span>set(SOURCES)</span></span>
|
|
|
|
|
<span class="line"><span>collect_sources(SOURCES \${CMAKE_SOURCE_DIR})</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span># 用于存储已经处理过的可执行文件名,防止重复</span></span>
|
|
|
|
|
<span class="line"><span>set(EXECUTABLE_NAMES)</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span># 创建可执行文件</span></span>
|
|
|
|
|
<span class="line"><span>foreach (SOURCE \${SOURCES})</span></span>
|
|
|
|
|
<span class="line"><span> # 获取文件的相对路径</span></span>
|
|
|
|
|
<span class="line"><span> file(RELATIVE_PATH REL_PATH \${CMAKE_SOURCE_DIR} \${SOURCE})</span></span>
|
|
|
|
|
<span class="line"><span> # 将路径中的斜杠替换为下划线,生成唯一的可执行文件名</span></span>
|
|
|
|
|
<span class="line"><span> string(REPLACE "/" "_" EXECUTABLE_NAME \${REL_PATH})</span></span>
|
|
|
|
|
<span class="line"><span> string(REPLACE "\\\\" "_" EXECUTABLE_NAME \${EXECUTABLE_NAME})</span></span>
|
|
|
|
|
<span class="line"><span> string(REPLACE "." "_" EXECUTABLE_NAME \${EXECUTABLE_NAME})</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span> # 处理与 CMakeLists.txt 文件同名的问题</span></span>
|
|
|
|
|
<span class="line"><span> if (\${EXECUTABLE_NAME} STREQUAL "CMakeLists_txt")</span></span>
|
|
|
|
|
<span class="line"><span> set(EXECUTABLE_NAME "\${EXECUTABLE_NAME}_exec")</span></span>
|
|
|
|
|
<span class="line"><span> endif ()</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span> # 检查是否已经创建过同名的可执行文件</span></span>
|
|
|
|
|
<span class="line"><span> if (NOT EXECUTABLE_NAME IN_LIST EXECUTABLE_NAMES)</span></span>
|
|
|
|
|
<span class="line"><span> list(APPEND EXECUTABLE_NAMES \${EXECUTABLE_NAME})</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span> # 创建可执行文件</span></span>
|
|
|
|
|
<span class="line"><span> add_executable(\${EXECUTABLE_NAME} \${SOURCE})</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span> # 查找源文件所在的目录,并添加为包含目录(头文件可能在同一目录下)</span></span>
|
|
|
|
|
<span class="line"><span> get_filename_component(DIR \${SOURCE} DIRECTORY)</span></span>
|
|
|
|
|
<span class="line"><span> target_include_directories(\${EXECUTABLE_NAME} PRIVATE \${DIR})</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span> # 检查并添加子目录中的 include 目录(如果存在)</span></span>
|
|
|
|
|
<span class="line"><span> if (EXISTS "\${DIR}/include")</span></span>
|
|
|
|
|
<span class="line"><span> target_include_directories(\${EXECUTABLE_NAME} PRIVATE \${DIR}/include)</span></span>
|
|
|
|
|
<span class="line"><span> endif ()</span></span>
|
2024-07-26 01:28:10 +02:00
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span> # 检查并添加 module 目录中的所有 C 文件(如果存在)</span></span>
|
|
|
|
|
<span class="line"><span> if (EXISTS "\${DIR}/module")</span></span>
|
|
|
|
|
<span class="line"><span> file(GLOB_RECURSE MODULE_SOURCES "\${DIR}/module/*.c")</span></span>
|
|
|
|
|
<span class="line"><span> target_sources(\${EXECUTABLE_NAME} PRIVATE \${MODULE_SOURCES})</span></span>
|
|
|
|
|
<span class="line"><span> endif ()</span></span>
|
2024-07-11 10:07:44 +02:00
|
|
|
|
<span class="line"><span> endif ()</span></span>
|
2024-08-15 05:48:49 +02:00
|
|
|
|
<span class="line"><span>endforeach ()</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br><span class="line-number">57</span><br><span class="line-number">58</span><br><span class="line-number">59</span><br><span class="line-number">60</span><br><span class="line-number">61</span><br><span class="line-number">62</span><br><span class="line-number">63</span><br><span class="line-number">64</span><br></div></div><h2 id="_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="" loading="lazy"></p><ul><li><code>.clang-format</code> 的内容,如下所示:</li></ul><div class="language-txt vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">txt</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span>BasedOnStyle: Google</span></span>
|
2024-07-11 10:07:44 +02:00
|
|
|
|
<span class="line"><span>IndentWidth: 4</span></span>
|
|
|
|
|
<span class="line"><span>UseTab: Never</span></span>
|
|
|
|
|
<span class="line"><span>ColumnLimit: 0</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span># 控制大括号的位置</span></span>
|
|
|
|
|
<span class="line"><span>BreakBeforeBraces: Attach</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span># 控制空行的使用</span></span>
|
|
|
|
|
<span class="line"><span>EmptyLineBeforeAccessModifier: Never</span></span>
|
|
|
|
|
<span class="line"><span>KeepEmptyLinesAtTheStartOfBlocks: true</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span># 控制短函数、短 if 语句和循环的格式</span></span>
|
|
|
|
|
<span class="line"><span>AllowShortFunctionsOnASingleLine: Empty</span></span>
|
|
|
|
|
<span class="line"><span>AllowShortIfStatementsOnASingleLine: false</span></span>
|
|
|
|
|
<span class="line"><span>AllowShortLoopsOnASingleLine: false</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span># 控制其他格式选项</span></span>
|
|
|
|
|
<span class="line"><span>AlignConsecutiveAssignments: true</span></span>
|
|
|
|
|
<span class="line"><span>AlignConsecutiveDeclarations: true</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span># 控制注释的格式化</span></span>
|
|
|
|
|
<span class="line"><span>ReflowComments: true</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span># 控制包含指令的格式化</span></span>
|
|
|
|
|
<span class="line"><span>IncludeBlocks: Regroup</span></span>
|
|
|
|
|
<span class="line"><span>SortIncludes: CaseInsensitive</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span>SpaceBeforeParens: ControlStatements</span></span>
|
|
|
|
|
<span class="line"><span>SpacesInParentheses: false</span></span>
|
|
|
|
|
<span class="line"><span>SpacesInAngles: false</span></span>
|
|
|
|
|
<span class="line"><span>SpacesInContainerLiterals: false</span></span>
|
2024-08-15 05:48:49 +02:00
|
|
|
|
<span class="line"><span>SpacesInCStyleCastParentheses: false</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br></div></div><ul><li>CLion 中配置<code>保存</code>的时候<code>自动格式化</code>,即:</li></ul><p><img src="`+xa+'" alt="" loading="lazy"></p><h2 id="_6-5-配置-gitignore-文件" tabindex="-1">6.5 配置 .gitignore 文件 <a class="header-anchor" href="#_6-5-配置-gitignore-文件" aria-label="Permalink to "6.5 配置 .gitignore 文件""></a></h2><ul><li>需要在项目中,配置 <code>.gitignore</code> 文件,以便在提交代码到 Git 仓库的时候,忽略某些文件或目录,如下所示:</li></ul><p><img src="'+qa+`" alt="" loading="lazy"></p><ul><li><code>.gitignore</code> 文件的内容,如下所示:</li></ul><div class="language-txt vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">txt</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span>.vscode</span></span>
|
2024-07-11 10:07:44 +02:00
|
|
|
|
<span class="line"><span>.idea</span></span>
|
|
|
|
|
<span class="line"><span>cmake-build-*</span></span>
|
2024-08-15 05:48:49 +02:00
|
|
|
|
<span class="line"><span>build</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h2 id="_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="" loading="lazy"></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="" loading="lazy"></p><ul><li>过程 ① :编写(编辑)源代码,即:编写 C 语言源程序代码,并以文件的形式存储在磁盘中。</li></ul><div class="important custom-block github-alert"><p class="custom-block-title">IMPORTANT</p><p></p><p>源程序需要以 <code>.c</code> 作为扩展名。</p></div><ul><li>过程 ② :编译,即:将 C 语言源程序转换为<code>目标程序(或目标文件)</code>。如果程序没有错误,没有任何提示,就会生成一个扩展名为 <code>.obj</code>或 <code>.o</code> 的二进制文件。C 语言中的每条可执行语句经过编译之后,最终都会转换为二进制的机器指令。</li></ul><div class="important custom-block github-alert"><p class="custom-block-title">IMPORTANT</p><p></p><ul><li><p>① 其实,<code>编译阶段</code>包含了<code>预处理</code>、<code>编译</code>和<code>汇编</code>。</p></li><li><p>② <code>预处理</code>是编译过程的第一个阶段。在这个阶段,预处理器处理源代码中的指令(例如:<code>#include</code>、<code>#define</code>等),主要任务包括:</p><ul><li><p>头文件包含:将头文件的内容插入到源文件中。例如:<code>#include <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 程序库<EFBFBD>
|
2024-07-26 01:17:43 +02:00
|
|
|
|
<span class="line"><span>networkingMode=mirrored</span></span>
|
|
|
|
|
<span class="line"><span>dnsTunneling=true</span></span>
|
|
|
|
|
<span class="line"><span>firewall=true</span></span>
|
|
|
|
|
<span class="line"><span>autoProxy=true</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span>[experimental]</span></span>
|
|
|
|
|
<span class="line"><span># requires dnsTunneling but are also OPTIONAL</span></span>
|
|
|
|
|
<span class="line"><span>bestEffortDnsParsing=true</span></span>
|
2024-08-15 05:48:49 +02:00
|
|
|
|
<span class="line"><span>useWindowsDnsCache=true</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><p><img src="`+Oa+'" alt="" loading="lazy"></p><ul><li>在命令行中,执行如下的命令:</li></ul><div class="language-shell vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">wsl</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --shutdown</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><p><img src="'+Ua+'" alt="" loading="lazy"></p><ul><li>此时,再打开终端,就没有这种提示了:</li></ul><p><img src="'+Ra+'" alt="" loading="lazy"></p><h2 id="_8-2-clion-调试问题" tabindex="-1">8.2 CLion 调试问题 <a class="header-anchor" href="#_8-2-clion-调试问题" aria-label="Permalink to "8.2 CLion 调试问题""></a></h2><ul><li>在 CLion 中进行 run(运行)程序的时候,对于 <code>printf</code> 函数或 <code>scanf</code> 函数很正常,如下所示:</li></ul><p><img src="'+Na+'" alt="" loading="lazy"></p><ul><li>但是,当我们 debug(调试) 的时候,对于 <code>printf</code> 函数或 <code>scanf</code> 函数会一直没有输出,如下所示:</li></ul><p><img src="'+Ia+`" alt="" loading="lazy"></p><ul><li>原因是 <code>scanf</code> 函数并不是直接让用户从键盘输入数据,而是先检查缓冲区,处理缓冲区中的数据;当遇到 <code>scanf</code> 函数时,程序会先检查输入缓冲区中是否有数据,所以解决方案就是<code>禁用缓冲区</code>,如下所示:</li></ul><div class="language-c vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">#include</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> <stdio.h></span></span>
|
2024-08-01 02:32:54 +02:00
|
|
|
|
<span class="line"></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">int</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> main</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">() {</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // 禁用 stdout 缓冲区</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> setbuf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(stdout, </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">NULL</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span></span>
|
|
|
|
|
<span class="line"></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> int</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> a, b, c;</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"请输入整数 a 、b 和 c 的值:"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> scanf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%d</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> %d</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> %d</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">&</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">a, </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">&</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">b, </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">&</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">c);</span></span>
|
|
|
|
|
<span class="line"></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> int</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> result </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> a </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">*</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> b </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">*</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> c;</span></span>
|
|
|
|
|
<span class="line"></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%d</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> × </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%d</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> × </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%d</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> = </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%d</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, a, b, c, result);</span></span>
|
|
|
|
|
<span class="line"></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
|
2024-08-15 05:48:49 +02:00
|
|
|
|
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><ul><li>那么,就会达到我们想要的效果了,如下所示:</li></ul><p><img src="`+Va+'" alt="" loading="lazy"></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="--shi
|
2024-08-01 07:49:31 +02:00
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span># 项目名称和版本号</span></span>
|
|
|
|
|
<span class="line"><span>project(c-study VERSION 1.0 LANGUAGES C)</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span># 设置 C 标准</span></span>
|
|
|
|
|
<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>
|
2024-08-15 07:26:13 +02:00
|
|
|
|
<span class="line"><span>endforeach ()</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br><span class="line-number">57</span><br><span class="line-number">58</span><br><span class="line-number">59</span><br><span class="line-number">60</span><br><span class="line-number">61</span><br><span class="line-number">62</span><br><span class="line-number">63</span><br><span class="line-number">64</span><br></div></div><ul><li>在 CLion 中正常运行代码:</li></ul><p><img src="`+Qa+'" alt="" loading="lazy"></p><ul><li>在 CLion 中通过 valgrind 运行代码:</li></ul><p><img src="'+Ya+'" alt="" loading="lazy"></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-li
|
2024-08-15 05:48:49 +02:00
|
|
|
|
<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="`+Ja+'" alt="" loading="lazy"></p><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><p>之所以报错的原因,在于 WSL2 中的 Ubuntu 的内核是定制化的,并非 Ubuntu 的母公司 Canonical 发布的标准内核,所以需要我们手动编译安装。</p></div><ul><li>查看内核版本:</li></ul><div class="language-shell vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">uname</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -sr</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><p><img src="'+Za+'" alt="" loading="lazy"></p><ul><li>设置环境变量,方便后续引用:</li></ul><div class="language-shell vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">export</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> KERNEL_VERSION</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">$(</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">uname</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -r</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> |</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> cut</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -d</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'-'</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="" loading="lazy"></p><ul><li>安装依赖库:</li></ul><div class="language-shell vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">apt</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -y</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> update</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> && </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">\\</span></span>
|
2024-08-01 07:49:31 +02:00
|
|
|
|
<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>
|
2024-08-15 05:48:49 +02:00
|
|
|
|
<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="" loading="lazy"></p><ul><li>下载源码:</li></ul><div class="language-shell vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">git</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> clone</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> \\</span></span>
|
2024-08-01 07:49:31 +02:00
|
|
|
|
<span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --depth</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 1</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> \\</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --single-branch</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --branch=linux-msft-wsl-\${</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">KERNEL_VERSION</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">}</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> \\</span></span>
|
2024-08-15 07:26:13 +02:00
|
|
|
|
<span class="line"><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> https://github.com/microsoft/WSL2-Linux-Kernel.git</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p><img src="`+ii+'" alt="" loading="lazy"></p><ul><li>编译内核代码:</li></ul><div class="language-shell vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#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="" loading="lazy"></p><ul><li>编译 perf 工具:</li></ul><div class="language-shell vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">cd</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> tools/perf</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><div class="language-shell vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">make</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> clean</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> && </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="" loading="lazy"></p><ul><li>复制到 PATH 变量所指向的路径中:</li></ul><div class="language-shell vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">cp</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> perf</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> /usr/bin/</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><p><img src="'+ei+'" alt="" loading="lazy"></p><h3 id="_8-4-3-整合" tabindex="-1">8.4.3 整合 <a class="header-anchor" href="#_8-4-3-整合" aria-label="Permalink to "8.4.3 整合""></a></h3><ul><li>CLion 中配置 perf 的路径:</li></ul><p><img src="'+pi+'" alt="" loading="lazy"></p><ul><li>在 CLion 中通过 perf 运行代码:</li></
|