在windows下的VSCode編寫C++配置
阿新 • • 發佈:2019-02-04
之前在看到網上沒有相關中文的VS Code的配置教程,於是就自己寫一個。
必須安裝windows下的gcc
配置下來就是這樣,可以進行debug。
這是我的外掛:
其中如果要編寫C++最好能夠安裝所有帶C++字樣的外掛。
其中// 將設定放入此檔案中以覆蓋預設設定 { // 以畫素為單位控制字號。 "editor.fontSize": 20, // 控制字體系列。 "editor.fontFamily": "Consolas, monospace", "python.autoComplete.extraPaths": [], "python.formatting.autopep8Args": [], "python.formatting.autopep8Path": "autopep8", "python.formatting.outputWindow": "Python", "python.formatting.provider": "autopep8", "python.formatting.yapfArgs": [], "python.formatting.yapfPath": "yapf", "python.jupyter.appendResults": true, "python.jupyter.defaultKernel": "", "python.jupyter.startupCode": [ "%matplotlib inline" ], "editor.renderIndentGuides": true, "python.linter": "pyLint", "python.linting.enabled": true, "python.linting.flake8Args": [], "python.linting.flake8Enabled": false, "python.linting.flake8Path": "flake8", "python.linting.lintOnSave": true, "python.linting.lintOnTextChange": true, "C_Cpp.clang_format_formatOnSave": true, "clang-format.language.cpp.enable": true, // "C_Cpp.clang_format_style": "file", // "C_Cpp.autocomplete": "Default", "C_Cpp.clang_format_path": null, "C_Cpp.clang_format_sortIncludes": true, "clang.cflags": [], "clang.completion.completeMacros": true, "clang.completion.enable": true, "clang.completion.maxBuffer": 8388608, "clang.completion.triggerChars": [ ".", ":", ">" ], "clang.cxxflags": [], "clang.diagnostic.delay": 200, "clang.diagnostic.enable": true, "clang.diagnostic.maxBuffer": 262144, "clang.executable": "D:\\Program Files\\mingw-w64\\x86_64-6.2.0-posix-seh-rt_v5-rev1\\mingw64\\bin\\g++.exe ", "clang.objcflags": [], "editor.formatOnSave": true, "editor.tabSize": 4, "typescript.format.placeOpenBraceOnNewLineForControlBlocks": false, "typescript.format.placeOpenBraceOnNewLineForFunctions": false, "python.linting.maxNumberOfProblems": 100, "python.linting.mypyArgs": [], "python.linting.mypyEnabled": false, "python.linting.mypyPath": "mypy", "python.linting.outputWindow": "Python", "python.linting.pep8Args": [], "python.linting.pep8Enabled": true, "python.linting.pep8Path": "pep8", "python.linting.prospectorArgs": [], "python.linting.prospectorEnabled": false, "python.linting.prospectorPath": "prospector", "python.linting.pydocStyleArgs": [], }
"clang.executable":
填寫自己的g++路徑,必須為絕對路徑,而且使用雙反斜槓
"C_Cpp.clang_format_style": "file",
這裡的格式化程式碼必須自己寫,而且儲存在工程資料夾下面。
"clang-format.executable": "clang-format",
這裡使用的是名字為.clang-format的格式化配置檔案。
寫完配置檔案,先寫個程式試一下
發現縮排很怪異,而且按F5無法進行除錯
在這裡,我們把我們的原始檔改為main.cpp
選擇GDB
然後在生成的.json檔案中刪除所有程式碼,換為以下程式碼:
{ "version": "0.2.0", "configurations": [ { "name": "C++ Launch", "type": "cppdbg", "request": "launch", "program": "${workspaceRoot}/main.exe", "args": [], "stopAtEntry": false, "cwd": "${workspaceRoot}", "preLaunchTask": "g++", "miDebuggerPath": "D:\\Program Files\\mingw-w64\\x86_64-6.2.0-posix-seh-rt_v5-rev1\\mingw64\\bin\\gdb.exe", "environment": [], "externalConsole": true, "linux": { "MIMode": "gdb" }, "osx": { "MIMode": "lldb" }, "windows": { "MIMode": "gdb" } } ] }
配置執行任務程式,之後選other
繼續把程式碼替換為以下程式碼
{
"version": "0.1.0",
"command": "g++",
"args": [
"-g",
"${file}",
"-o",
"main.exe"
],
"probelmMatcher": {
"owner": "cpp",
"fileLocation": [
"relative",
"${workspaceRoot}"
],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
}
F5啟動,可以除錯了
但這意味著我們每次啟動除錯都得先寫2個配置檔案,在這裡為們可以寫一個json的程式碼片段方便除錯
檔案選單-》首選項-》使用者程式碼片段,輸入json,新增如下程式碼:
{
"TasksSetting": {
"prefix": "TaskC++",
"body": [
"{\"version\":\"0.1.0\",",
"\"command\":\"g++\",",
"\"args\":[\"-g\",\"${$}{file}\",\"-o\",\"main.exe\"],",
"\"probelmMatcher\":{",
"\"owner\":\"cpp\",",
"\"fileLocation\":[\"relative\",\"${$}{workspaceRoot}\"],",
"\"pattern\":{\"regexp\":\"^(.*):(${\\\\\\\\}d+):(${\\\\\\\\}d+):${\\\\\\\\}s+(warning|error):${\\\\\\\\}s+(.*)${$}\",",
"\"file\":1,\"line\":2,\"column\":3,\"severity\":4,\"message\":5}}}"
],
"description": "GDB的配置"
},
"LaunchSettings": {
"prefix": "LaunchC++",
"body": [
"{",
"\"version\": \"0.2.0\",",
"\"configurations\":[",
"{\n",
"\"name\": \"C++ Launch\",",
"\"type\": \"cppdbg\",",
"\"request\": \"launch\",",
"\"program\": \"${$}{workspaceRoot}/main.exe\",",
"\"args\": [],",
"\"stopAtEntry\": false,",
"\"cwd\":\"${$}{workspaceRoot}\",\n",
"\"preLaunchTask\":\"g++\",\n",
"\"miDebuggerPath\":\"D:${\\\\}${\\\\}Program Files${\\\\}${\\\\}mingw-w64${\\\\}${\\\\}x86_64-6.2.0-posix-seh-rt_v5-rev1${\\\\}${\\\\}mingw64${\\\\}${\\\\}bin${\\\\}${\\\\}gdb.exe\",",
"\"environment\":[],",
"\"externalConsole\":true,",
"\"linux\": {",
"\"MIMode\": \"gdb\"},",
"\"osx\": {",
"\"MIMode\": \"lldb\"},",
"\"windows\": {",
"\"MIMode\": \"gdb\"}}]}"
],
"description": "GDB的配置"
}
}
需要寫配置的時候,輸入
LaunchC++或TaskC++
就可以直接生成配置
還有一個問題就是格式化:
先寫一個.clang-format檔案,新增以下程式碼
# We'll use defaults from the LLVM style, but with 4 columns indentation.
# 如果想進行如下格式化,需要寫一個這樣的檔案
BasedOnStyle: LLVM
IndentWidth: 4
---
Language: Cpp
儲存,程式碼格式化恢復正常