iT邦幫忙

4

[VSCode] Visual Studio Code 執行 C++ (3) - 語法檢查

繼上一篇,目前的 VSCode還沒有語法檢查的功能,程式碼自動完成也不完整。
http://ithelp.ithome.com.tw/upload/images/20171003/20106865CDZHtYQdi9.jpg
if 故意多打了一個括號沒有錯誤提示,if 內想打 printf 沒有自動帶出來。

不過 VSCode有很多好用的套件可以使用,接下來需要安裝 C/C++ Clang Command Adapter 這個套件,讓 VSCode 可以有語法錯誤提示和完整的程式碼自動完成功能。

1. 安裝套件 C/C++ Clang Command Adapter
http://ithelp.ithome.com.tw/upload/images/20171003/201068650wOZz1BinK.jpg

2. 安裝 Clang
網址: Clang
版本可自由選擇,我使用的版本是 LLVM-4.0.1-win64.exe。

3. 設定 VSCode settings.json
http://ithelp.ithome.com.tw/upload/images/20171003/20106865tdFDYpO5J9.jpg
在左下角開啟 VSCode 設定檔,加入以下內容。

{
    "[cpp]": {
        "editor.formatOnType": true,
        "editor.formatOnSave": true,
        "editor.renderIndentGuides": true,
        "editor.insertSpaces": true,
        "editor.detectIndentation": true,
        "editor.tabSize": 4
    },
    "clang.executable" : "C:\\Program Files\\LLVM\\bin\\clang.exe",
    "clang.cxxflags": [
        "-std=c++11",
        "-IC:\\MinGW\\include",
        "-IC:\\MinGW\\lib\\gcc\\mingw32\\6.3.0\\include",
        "-IC:\\MinGW\\lib\\gcc\\mingw32\\6.3.0\\include\\c++",
        "-I${workspaceRoot}\\include",
        "-I${cwd}"
    ],
    "clang.cflags":[
        "-std=c99",
        "-IC:\\MinGW\\include",
        "-IC:\\MinGW\\lib\\gcc\\mingw32\\6.3.0\\include",
        "-I${workspaceRoot}\\include",
        "-I${cwd}"
    ]
}

clang.executable Clang 的安裝路徑。
clang.cxxflags C++ 的設定。
clang.cflags C 的設定。
[cpp] .cpp檔案自動縮排和格式化相關設定,這個有點離題 XD。

http://ithelp.ithome.com.tw/upload/images/20171003/201068658Bihn58wLj.jpg
來看看結果,if 有錯誤提示,printf 也有正確帶出,太棒了跟 Visual Studio 一樣欸 XD。

結語:
介紹到這裡,已經完成所有基本功能的設定,編譯、偵錯、程式碼自動完成、語法檢查、自動縮排和格式化。經過這麼多繁瑣的設定,相信大家都煩了 XD,不過在寫程式時能有這麼多好用的功能輔助減少出錯,就會覺得這些辛苦都是值得的 :D。

相關文章:
[VSCode] Visual Studio Code 執行 C++ (1) - 安裝 VSCode + MinGW
[VSCode] Visual Studio Code 執行 C++ (2) - IntelliSense + Building + Debugging
[VSCode] Visual Studio Code 執行 C++ (3) - 語法檢查
[VSCode] Visual Studio Code 執行 C++ (4) - 範本 (Template)
[VSCode] Visual Studio Code 執行 C++ (5) - 中文亂碼
[VSCode] Visual Studio Code 執行 C++ (6) - Code Runner


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
3
海綿寶寶
iT邦大神 1 級 ‧ 2017-10-04 11:43:51

經過這麼多繁瑣的設定,相信大家都煩了 XD

你錯了

我一點都不煩
因為我根本沒有跟您做這麼多繁瑣的設定
/images/emoticon/emoticon25.gif

哈哈哈,你這樣說我要去旁邊畫圈圈了

0
crechenko_cs06
iT邦新手 5 級 ‧ 2019-04-04 14:38:05

我照您的方法試過了,但是還是不行ㄟ
您知道有可能是哪裡出錯了嗎?
以下是我的settings.json

{
    "[cpp]": {
        "editor.formatOnType": true,
        "editor.formatOnSave": true,
        "editor.renderIndentGuides": true,
        "editor.insertSpaces": true,
        "editor.detectIndentation": true,
        "editor.tabSize": 4,
    },
    "clang.executable" : "C:\\Program Files\\LLVM\\bin\\clang.exe",
    "clang.cxxflags": [
        "-std=c++11",
        "-IC:\\MinGW\\include",
        "-IC:\\MinGW\\lib\\gcc\\mingw32\\6.3.0\\include",
        "-IC:\\MinGW\\lib\\gcc\\mingw32\\6.3.0\\include\\c++",
        "-I${workspaceRoot}\\include",
        "-I${cwd}"
    ],
    "clang.cflags":[
        "-std=c99",
        "-IC:\\MinGW\\include",
        "-IC:\\MinGW\\lib\\gcc\\mingw32\\6.3.0\\include",
        "-I${workspaceRoot}\\include",
        "-I${cwd}"
    ],
    //"breadcrumbs.enabled": true,
    //"C_Cpp.default.intelliSenseMode": "msvc-x64"
}
0
2344webber
iT邦新手 5 級 ‧ 2022-05-29 14:30:27

大大您好
我的settings.json裡,editor.renderIndentGuides這一項會報問題說:這個設定不支援以語言為根據的組態。請問是為甚麼呢
https://ithelp.ithome.com.tw/upload/images/20220529/20149209SYEG90N0e0.png

不能放在 [cpp] 中,將它拉到外層試試。

我要留言

立即登入留言