Cấu hình
Trang này cung cấp các file cấu hình mẫu cho dự án C/C++ trong Visual Studio Code, bao gồm .clang-format
, tasks.json
, launch.json
, cpp.json
, và c_cpp_properties.json
.
.clang-format
Định dạng mã nguồn tự động, giúp mã nguồn của bạn luôn gọn gàng và dễ đọc.
Nội dung file .clang-format
, đặt tại thư mục gốc của workspace.
---
Language: Cpp
DisableFormat: false
RemoveSemicolon: false
RemoveBracesLLVM: false
SpaceInEmptyBlock: false
CompactNamespaces: false
SpaceBeforeCaseColon: false
SpaceAfterLogicalNot: false
SpaceBeforeSquareBrackets: false
SpacesInContainerLiterals: false
SpaceAfterTemplateKeyword: false
KeepEmptyLinesAtTheStartOfBlocks: false
InsertBraces: true
ReflowComments: true
IndentRequires: true
IndentCaseLabels: true
BreakStringLiterals: true
FixNamespaceComments: true
IndentRequiresClause: true
SpaceAfterCStyleCast: true
IndentAccessModifiers: true
IndentWrappedFunctionNames: true
SpaceBeforeInheritanceColon: true
AllowShortLoopsOnASingleLine: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeCtorInitializerColon: true
AllowShortCaseLabelsOnASingleLine: true
SpaceBeforeRangeBasedForLoopColon: true
SortIncludes: CaseInsensitive
IncludeBlocks: Regroup
AlignOperands: AlignAfterOperator
SpaceBeforeParens: Custom
RemoveParentheses: MultipleParentheses
IndentPPDirectives: AfterHash
NamespaceIndentation: All
BreakInheritanceList: AfterComma
SortUsingDeclarations: LexicographicNumeric
SpaceAroundPointerQualifiers: After
RequiresExpressionIndentation: Keyword
EmptyLineBeforeAccessModifier: LogicalBlock
AllowShortFunctionsOnASingleLine: Inline
AllowShortIfStatementsOnASingleLine: AllIfsAndElse
AlignConsecutiveBitFields: Consecutive
AlignConsecutiveAssignments: Consecutive
AlignConsecutiveDeclarations: Consecutive
PointerAlignment: Left
QualifierAlignment: Left
ReferenceAlignment: Right
AlignArrayOfStructures: Right
UseTab: Never
SpacesInAngles: Never
BreakBeforeBinaryOperators: None
EmptyLineAfterAccessModifier: Never
SeparateDefinitionBlocks: Always
AllowShortBlocksOnASingleLine: Always
IndentWidth: 4
ShortNamespaceLines: 0
MaxEmptyLinesToKeep: 1
ContinuationIndentWidth: 0
SpacesBeforeTrailingComments: 1
ColumnLimit: 100
SpaceBeforeParensOptions:
AfterPlacementOperator: false
AfterFunctionDefinitionName: false
AfterFunctionDeclarationName: false
AfterControlStatements: true
BraceWrapping:
AfterEnum: false
AfterClass: false
AfterUnion: false
BeforeElse: false
BeforeWhile: false
AfterStruct: false
BeforeCatch: false
AfterFunction: false
AfterCaseLabel: false
AfterNamespace: false
AfterExternBlock: false
SplitEmptyRecord: false
SplitEmptyFunction: false
SplitEmptyNamespace: false
AfterControlStatement: Never
AlignConsecutiveShortCaseStatements:
Enabled: true
AcrossComments: true
AlignCaseColons: true
AcrossEmptyLines: true
AlignTrailingComments:
Kind: Always
OverEmptyLines: 2
---
tasks.json
Định nghĩa các tác vụ xây dựng (build tasks) cho dự án của bạn, giúp bạn biên dịch và xây dựng mã nguồn một cách dễ dàng.
Nội dung file tasks.json
, đặt bên trong thư mục .vscode
nằm ở thư mục gốc của workspace
.
{
"tasks": [
// {
// "type": "shell",
// "label": "Compile icon resource",
// "command": "windres",
// "args": [
// "icon.rc",
// "-O",
// "coff",
// "-o",
// "icon.o"
// ],
// "group": "build",
// "detail": "Compiles icon resource file"
// },
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file with icon",
"command": "g++",
"args": [
// "-finput-charset=UTF-8",
// "-fextended-identifiers",
// "-mwindows",
// "-municode",
"-Wall",
"-Wextra",
"-Wpedantic",
"-Werror",
"-std=c++17",
"-fdiagnostics-color=always",
"-g",
"${file}",
// "icon.o",
"-o",
"${fileDirname}/${fileBasenameNoExtension}.exe"
],
// "dependsOn": [
// "Compile icon resource"
// ],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
launch.json
Cấu hình các phiên gỡ lỗi (debug sessions), giúp bạn kiểm tra và sửa lỗi mã nguồn một cách hiệu quả.
Nội dung file launch.json
, đặt bên trong thư mục .vscode
nằm ở thư mục gốc của workspace
.
{
"version": "0.2.0",
"configurations": [
{
"name": "C/C++: g++.exe build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "gdb",
"setupCommands": [
// {
// "description": "Set charset to UTF-8 for Unicode support",
// "text": "set charset UTF-8",
// "ignoreFailures": true
// },
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe build active file with icon"
}
]
}
cpp.json
Các cài đặt và cấu hình bổ sung cho mã nguồn C++, bao gồm cả tùy chọn biên dịch và liên kết.
Snippets template cppmain
prefix, file cpp.json
đặt tại "%appdata%\Roaming\Code\User\snippets\"
.
{
"C++ Template": {
"prefix": "cppmain",
"body": [
"#include <iostream>",
"#include <vector>",
"",
"int main() {",
"\tstd::cout << \"\\\\033c\";",
"\tstd::cin.sync_with_stdio(0);",
"\tstd::cin.tie(0);",
"",
"\tint n;",
"\tstd::cin >> n;",
"",
"\treturn 0;",
"}"
],
"description": "Template for C++ main file"
}
}
c_cpp_properties.json
Cấu hình IntelliSense, giúp cung cấp gợi ý mã và kiểm tra cú pháp khi bạn lập trình bằng C/C++.
Nội dung file c_cpp_properties.json
, đặt bên trong thư mục .vscode
nằm ở thư mục gốc của workspace
.
{
"env": {
"myIncludePath": [
"${workspaceFolder}/include",
"${workspaceFolder}/src"
],
"myDefines": [
"DEBUG",
"MY_FEATURE=1"
]
},
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"${myIncludePath}"
],
"defines": [
"${myDefines}",
"_WINDOWS",
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "C:/msys64/ucrt64/bin/g++",
"cStandard": "c17",
"cppStandard": "c++20",
"intelliSenseMode": "windows-gcc-x64",
"browse": {
"path": [
"${myIncludePath}",
"${workspaceFolder}"
]
}
}
],
"version": 4,
"enableConfigurationSquiggles": true
}