如何在vscode編寫(xiě)c語(yǔ)言

如何在vscode編寫(xiě)c語(yǔ)言

1、安裝vscode(版本1.27)

https://code.visualstudio.com/ 下載安裝vscode。

2、安裝c/c++擴(kuò)展。

如何在vscode編寫(xiě)c語(yǔ)言

立即學(xué)習(xí)C語(yǔ)言免費(fèi)學(xué)習(xí)筆記(深入)”;

3、安裝編譯工具mingw-w64,http://www.mingw-w64.org/doku.php/download

配置環(huán)境變量,以WIN10為例 ,此電腦-屬性-高級(jí)系統(tǒng)設(shè)置-環(huán)境變量-系統(tǒng)變量-path-添加一條D:Program Filesmingw-w64i686-8.1.0-posix-dwarf-rt_v6-rev0mingw32in(你安裝編譯工具路徑)

配置前

如何在vscode編寫(xiě)c語(yǔ)言

配置后

如何在vscode編寫(xiě)c語(yǔ)言

-ps:如果開(kāi)著vscode配置環(huán)境變量,配置完要關(guān)掉vscode重開(kāi)一次。

4、配置文件launch.json,task.json。

新建文件hello.cpp,

如何在vscode編寫(xiě)c語(yǔ)言

按F5彈出選擇環(huán)境,配置launch.json

如何在vscode編寫(xiě)c語(yǔ)言

點(diǎn)擊進(jìn)去,configurations:里內(nèi)容如下:

{? "name":?"(gdb)?Launch",? "type":?"cppdbg", "request":?"launch",? "program":?"enter?program?name,?for?example?${workspaceFolder}/a.exe",? "args":?[],? "stopAtEntry":?false,? "cwd":?"${workspaceFolder}",? "environment":?[],? "externalConsole":?true,? "MIMode":?"gdb",? "miDebuggerPath":?"/path/to/gdb",? "setupCommands":?[? {? "description":?"Enable?pretty-printing?for?gdb",? "text":?"-enable-pretty-printing",? "ignoreFailures":?true? }? ]? }

修改成如下:

{? "name":?"(gdb)?Launch",? "type":?"cppdbg", "request":?"launch",? "program":?"${workspaceFolder}/a.exe",//這里刪除前面那里的enter?program?name,?for?example? "args":?[],? "stopAtEntry":?false,? "cwd":?"${workspaceFolder}",? "environment":?[],? "externalConsole":?true,? "MIMode":?"gdb",? "miDebuggerPath":?"D:Program?Filesmingw-w64i686-8.1.0-posix-dwarf-rt_v6-rev0mingw32bingdb.exe", //修改為你安裝mingw32的路徑? "setupCommands":?[? {? "description":?"Enable?pretty-printing?for?gdb",? "text":?"-enable-pretty-printing",? "ignoreFailures":?true? }? ],? "preLaunchTask":?"build?hello",//task.json里面的名字? }

回到hello.cpp按F5彈出報(bào)錯(cuò)框,選配置任務(wù)。

如何在vscode編寫(xiě)c語(yǔ)言

點(diǎn)擊,然后選Others,出現(xiàn)task.json,如下:

"version":?"2.0.0", ????"tasks":?[ ????????{ ????????????"label":?"echo", ????????????"type":?"shell", ????????????"command":?"echo?Hello" ????????} ????]

修改如下:

"version":?"2.0.0", ????"tasks":?[ ????????{ ????????????"label":?"build?hello", ????????????"type":?"shell", ????????????"command":?"g++", ????????????"args":?[ ????????????????"-g",?"hello.cpp", ????????????], ????????????"group":{ ????????????????"kind":?"build", ????????????????"isDefault":?true ????????????} ????????}? ????]

注意:如果是win32程序(窗口界面),args內(nèi)加上”-mwindows”。

如何在vscode編寫(xiě)c語(yǔ)言

回到hello.cpp文件,按F5。成功運(yùn)行編譯。

如何在vscode編寫(xiě)c語(yǔ)言

5、還有就是發(fā)現(xiàn)中文控制臺(tái)顯示亂碼。

如何在vscode編寫(xiě)c語(yǔ)言

只要點(diǎn)擊右下角utf-8。

如何在vscode編寫(xiě)c語(yǔ)言

點(diǎn)擊使用編碼保存,選GBK,然后按F5運(yùn)行。

如何在vscode編寫(xiě)c語(yǔ)言

PHP中文網(wǎng),有大量免費(fèi)vscode入門(mén)教程,歡迎大家學(xué)習(xí)!

? 版權(quán)聲明
THE END
喜歡就支持一下吧
點(diǎn)贊15 分享