1. 程式人生 > >VSCode配置C++環境

VSCode配置C++環境

VSCode配置C++環境

IDE用慣了,換編輯器寫被坑傻了。


VSCode環境的配置都由.json資料構成,全部放在workingFolder的.vscode資料夾內,預設是隱藏的,因為這是配置檔案啊。

1.配置settings

如果想改字型大小什麼的,直接搜尋命令open settings,在user.setting裡改。

2.生成c_cpp_properties.json

Command shift P + Edit configuration

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                "C:\\Program Files (x86)\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\lib\\gcc\\i686-w64-mingw32\\8.1.0\\include\\c++"
            ],
            "defines": [],
            "windowsSdkVersion": "10.0.16299.0",
            "compilerPath": "C:\\Program Files (x86)\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\bin\\g++.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "gcc-x64"
        }
    ],
    "version": 4
}

3. 生成tasks.json

這個檔案很重要,在這檔案裡要說明你想執行的任務。

"tasks": [
        {
            "label": "build", # 這是任務的標籤,也就是名字,在run task時會體現
            "type": "shell", # 通過shell執行你的命令
            "command": "g++", # 指定編譯器命令,引數作用看我上一篇部落格,gcc需要額外連結
            "args": [
            	"first.cpp",
                "test.cpp", # 引數
                "-o",
                "main.exe"
            ]
        },
        {
            "label": "build-debug",
            "type": "shell",
            "command": "g++",
            "args": [
                "-g",
                # 生成可除錯檔案
                "first.cpp",
                "-o",
                "debug.exe"
            ]
        }
        
    ]

4.生成launch.json

Configure Task Runner,

debug->open configuration,生成launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            # 呼叫生成的除錯檔名字,第二個task生成的
            "program": "${workspaceFolder}/debug.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            # gdb.exe路徑
            "miDebuggerPath": "C:\\Program Files (x86)\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            # 可以省去debug的第一步
            "preLaunchTask": "build-debug"
        }
    ]
}