MacOs uses VS Code to compile and debug C language programs

Referenceblog:Windows/macOSusesVSCodetobuildaC/C++development/Debugenvironment

1.InstallVSCodeonmacOS

DownloadVSCodeformacOSfromMicrosoftofficialwebsite.

2.SearchandinstallthefollowingextensionsintheextensionofVSCode

  • C/C++
  • C/C++ExtensionPack
    (TheextensionpackagecontainsC/C++,C/C++Themes,CMakeToolsandCMake,whicharealsoinstalledtogether)
  • Chinese(Simplified)
  • CodeRunner(runcode)
    AfterinstallingCodeRunner,remembertocheck”RunInTerminal”initsextendedsettings
  • CodeLLDB(Debugdebuggingenvironment)



3.Installthecompiler

Theclangcompilerisusedhere,firstopenthecommandline/terminal(terminal):searchforterminalonthedesktop,andopenthe”terminal”application.

Afteropeningtheterminalapplication,enterclang-vorclang--versiontocheckwhetheryourMachastheclangcompiler.Thefigurebelowshowsthedisplayofclanginstalled.
IfclangisnotinstalledontheMac,typeXcode-select--installintheterminalandpressEnter,andclickOKinthepop-upwindow.
Suggestion:Afterclanginstallationiscomplete,runclang-v-E-xc++-intheterminalandcopytheboxselectionpath,whichwillbeusefulinconfiguringtheincludepathintheIntelliSenseconfigurationlater.

4.ConfigureVSCodetousethecompilerinmacOSenvironment

Createanewfolderonthedesktop(don’tnameitinChinese,remember!),andopenitinVScode.

Createanew.cfileinthefolderopenedbyVScode,andthenfollowthestepsshowninthefigure.

ClickonExtendedC/C++tomakeextendedsettings

ClickIntelliSense
Set”CompilerPath”or”ComplierPath”(onlyChineseandEnglisharedifferent,nodifference).Setitto/Library/Developer/CommandLineTools/usr/bin/clangor/Library/Developer/CommandLineTools/usr/bin/clang++
Thenset”IntelliSenseMode”or”IntelliSenseMode”,selectmacos-clang-arm64(dependingonthemodelandchip,ifitisApplesilicon,selectarm/arm64).

Suggestion:IncludepathsettingsforVSCodeIntelIiSence

Continuetosettheincludepath/IncludePath,andincludeseveralpathsthatyoujustranclang-v-E-xc++-intheterminal.

5.ConfigureDebugenvironment

WriteasimplecodeintheClanguagefilejustnoworusethefollowingcodedirectly:

#include<stdio.h>
intmain()
{
for(inti=0;i<4;i++)
{
printf("Hello,World!\\
");
}
return0;
}

Putabreakpointbeforethelineofafunction,clicktheoptionbuttonnexttoRun,andselectDebugC/C++File


Configureasshownbelow.

A.vscodefolderwillbegeneratedundertheopenedfolder,andtask.jsonwillbeautomaticallygeneratedinit.

Thecontentoftask.jsonisasfollows:

{
"tasks":[
{
"type":"cppbuild",
"label":"C/C++:clanggeneratesactivefiles",
"command":"/usr/bin/clang",
"args":[
"-fcolor-diagnostics",
"-fansi-escape-codes",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options":{
"cwd":"${fileDirname}"
},
"problemMatcher":[
"$gcc"
],
"group":{
"kind":"build",
"isDefault":true
},
"detail":"Taskgeneratedbythedebugger."
}
],
"version":"2.0.0"
}

Thencreateanewlaunch.jsonunderthe.vscodefolderandwritethefollowingcode:

{
//UseIntelliSenseforrelevantproperties.
//Hovertoseedescriptionsofexistingproperties.
//Formoreinformation,pleasevisit:https://go.microsoft.com/fwlink/?linkid=830387
"version":"0.2.0",
"configurations":[
{
"type":"lldb",
"request":"launch",
"name":"Debug",
"program":"${workspaceFolder}/${fileBasenameNoExtension}",
"args":[],
"cwd":"${workspaceFolder}"
}
]
}

6.HelloWorld!

Forthecodewrittenearlier,addbreakpointstodebug.


Clickthe”Continue”button

Youcanseethedebuggingresultsinthe”DebugConsole”

Thenclick”Continue”toseethenextresult:

TheaboveisthewholecontentofMacOsusingVSCodetocompileanddebugClanguageprograms~~