Skip to content
Published at:

VS Code C/C++ 开发环境搭建

B站视频教学地址:todo

  • Xmind
  • 通用设置
  • Snippets

通用设置

json
{
  "window.nativeTabs": true,
  "editor.cursorBlinking": "smooth",
  "editor.cursorSmoothCaretAnimation": true,
  "editor.cursorSurroundingLines": 2,
  "editor.detectIndentation": false,
  "editor.dragAndDrop": false,
  "editor.fontFamily": "Monaco",
  "editor.fontSize": 14,
  "editor.formatOnSave": true,
  "editor.formatOnType": true,
  "editor.largeFileOptimizations": false,
  "editor.minimap.enabled": false,
  "editor.multiCursorModifier": "ctrlCmd",
  "editor.renderControlCharacters": true,
  "editor.renderWhitespace": "all",
  "editor.smoothScrolling": true,
  "editor.suggest.localityBonus": true,
  "editor.suggest.shareSuggestSelections": true,
  "editor.suggest.snippetsPreventQuickSuggestions": false,
  // "editor.suggestSelection": "recentlyUsedByPrefix",
  "editor.tabCompletion": "on",
  "editor.wordWrap": "wordWrapColumn",
  "editor.wordWrapColumn": 180,
  "editor.accessibilitySupport": "off",
  "files.trimTrailingWhitespace": true,
  "files.autoSaveDelay": 100,
  "files.autoSave": "afterDelay",
  "files.autoGuessEncoding": true,
  "workbench.editor.limit.enabled": true,
  "workbench.editor.limit.perEditorGroup": true,
  "workbench.colorTheme": "GitHub Light",
  "workbench.editor.highlightModifiedTabs": true,
  "workbench.iconTheme": "vscode-icons",
  "workbench.settings.editor": "json",
  "workbench.startupEditor": "newUntitledFile",
  "workbench.colorCustomizations": {
    "editorIndentGuide.activeBackground": "#FF9249",
    "gitDecoration.ignoredResourceForeground": "#ff0000"
  },
  "debug.allowBreakpointsEverywhere": true,
  "debug.inlineValues": true,
  "debug.toolBarLocation": "docked",
  "C_Cpp.autocomplete": "Default",
  "C_Cpp.clang_format_sortIncludes": true,
  "C_Cpp.default.cStandard": "c11",
  "C_Cpp.default.cppStandard": "c++14",
  // "C_Cpp.intelliSenseCachePath": "${workspaceFolder}/.vscode",
  "C_Cpp.suggestSnippets": false,
  "C_Cpp.clang_format_style": "{ BasedOnStyle: LLVM, DerivePointerAlignment: false, PointerAlignment: Left, IndentWidth: 4, TabWidth: 4, ColumnLimit: 0, SpacesBeforeTrailingComments: 2, AccessModifierOffset: -4 }",
  "code-runner.clearPreviousOutput": true,
  "code-runner.runInTerminal": true,
  "code-runner.ignoreSelection": true,
  "code-runner.executorMap": {
    "c": "cd $dir && gcc -std=c11 $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt && rm $dir$fileNameWithoutExt",
    "cpp": "cd $dir && g++ -std=c++14 $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt && rm $dir$fileNameWithoutExt",
    "java": "cd $dir && javac $fileName && java $fileNameWithoutExt && rm $dir$fileNameWithoutExt.class",
    "python": "python3 -u",
    "rust": "cargo run",
    "html": "open"
  }
}

Snippets

c.json

json
{
  // Place your snippets for c here. Each snippet is defined under a snippet name and has a prefix, body and
  // description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
  // $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the
  // same ids are connected.
  // Example:
  // "Print to console": {
  // 	"prefix": "log",
  // 	"body": [
  // 		"console.log('$1');",
  // 		"$2"
  // 	],
  // 	"description": "Log output to console"
  // },
  "init main": {
    "prefix": "main",
    "body": [
      "#include <stdio.h>",
      "#include <stdlib.h>",
      "#include <string.h>",
      "",
      "int main(int argc, char *argv[])",
      "{",
      "    $0",
      "    return 0;",
      "}"
    ],
    "description": "Basic main function"
  },
  "Print format": {
    "prefix": "prin",
    "body": ["printf(\"$1\",$0);"],
    "description": "Quick print format"
  },
  "Print message": {
    "prefix": "pr",
    "body": ["printf(\"$0\");"],
    "description": "Quick print format"
  },
  "sizeof": {
    "prefix": "si",
    "body": ["sizeof(${1:name})"],
    "description": "sizeof(name)"
  },
  "for": {
    "prefix": "for",
    "body": ["for (${int} ${i} = ${1:0}; ${i} < ${2:length}; ${i}++)", "{", "    $0", "}"],
    "description": "Code snippet for 'for' loop"
  },
  "forj": {
    "prefix": "forj",
    "body": ["for (${int} ${j} = ${1:0}; ${j} < ${2:length}; ${j}++)", "{", "    $0", "}"],
    "description": "Code snippet for 'for' loop"
  },
  "fork": {
    "prefix": "fork",
    "body": ["for (${size_t} ${k} = ${1:0}; ${k} < ${2:length}; ${k}++)", "{", "    $0", "}"],
    "description": "Code snippet for 'for' loop"
  },
  "forr": {
    "prefix": "forr",
    "body": ["for (int ${i} = ${1:length} - 1; ${i} >= ${2:0}; ${i}--)", "{", "    $0", "}"],
    "description": "Code snippet for reverse 'for' loop"
  },
  "while": {
    "prefix": "while",
    "body": ["while ($1)", "{", "    $0", "}"],
    "description": ""
  },
  "if": {
    "prefix": "if",
    "body": ["if ($1)", "{", "    $0", "}"],
    "description": "Code snippet for if statement"
  },
  "else": {
    "prefix": "else",
    "body": ["else", "{", "    $0", "}"],
    "description": "Code snippet for else statement"
  },
  "else if": {
    "prefix": "else if",
    "body": ["else if ($1)", "{", "    $0", "}"],
    "description": "Code snippet for else-if statement"
  },
  "#ifdef": {
    "prefix": "#ifdef",
    "body": ["#ifdef ${DEBUG}", "$0", "#endif // ${DEBUG}"],
    "description": "Code snippet for #ifdef"
  },
  "#ifndef": {
    "prefix": "#ifndef",
    "body": ["#ifndef ${1:1}", "$0", "#endif // !$1"],
    "description": "Code snippet for #ifndef"
  },
  "#if": {
    "prefix": "#if",
    "body": ["#ifdef ${1:0}", "$0", "#endif // $1"],
    "description": "Code snippet for #if"
  },
  "struct": {
    "prefix": "str",
    "body": ["struct ${1:MyStruct}", "{", "    $0", "};"],
    "description": "Code snippet for struct"
  },
  "enum": {
    "prefix": "enum",
    "body": ["enum ${1:MyEnum}", "{", "    $0", "};"],
    "description": "Code snippet for enum"
  },
  "union": {
    "prefix": "union",
    "body": ["union ${MyUnion}", "{", "    $0", "};"],
    "description": "Code snippet for union"
  },
  "switc": {
    "prefix": "sw",
    "body": [
      "switch($1)",
      "{",
      "    {",
      "    case $2:",
      "        $0",
      "        break;",
      "    default:",
      "        ",
      "        break;",
      "    }",
      "}"
    ],
    "description": "switch(){}"
  },
  "switch2": {
    "prefix": "switch",
    "body": [
      "switch($1){",
      "{",
      "    case $2:",
      "        $0",
      "        break;",
      "    default:",
      "        ",
      "        break;",
      "}"
    ],
    "description": "switch(){}"
  },
  "case": {
    "prefix": "ca",
    "body": ["case $1:", "    $0", "    break;"],
    "description": "case:break;"
  },
  "case2": {
    "prefix": "case",
    "body": ["case $1:", "    $0", "    break;"],
    "description": "case:break;"
  },
  "#inc": {
    "prefix": "#in",
    "body": ["#include \"$1\""],
    "description": "Code snippet for #include \" \""
  },
  "#incl": {
    "prefix": "#inc<",
    "body": ["#include <$1>"],
    "description": "Code snippet for #include \" \""
  },
  "#def": {
    "prefix": "#def",
    "body": ["#define \"$1\" \"$2\" "],
    "description": "Code snippet for #define \" \""
  },
  "add  fun(){}": {
    "prefix": "fun",
    "body": ["${1:functionname}(${2:void})", "{", "    $0", "}"],
    "description": "add @funtionname(@arg){}"
  },
  "add fun(){}": {
    "prefix": "fu",
    "body": ["${1:functionname}(${2:args})", "{", "    $0", "}"],
    "description": "add @funtionname(@arg){}"
  }
}

cpp.json

json
{
  // Place your snippets for cpp here. Each snippet is defined under a snippet name and has a prefix, body and
  // description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
  // $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the
  // same ids are connected.
  // Example:
  // "Print to console": {
  // 	"prefix": "log",
  // 	"body": [
  // 		"console.log('$1');",
  // 		"$2"
  // 	],
  // 	"description": "Log output to console"
  // }
  "main": {
    "prefix": "main",
    "body": [
      "#include <iostream>",
      "",
      "using namespace std;",
      "",
      "int main(int argc, char *argv[]) {",
      "    $0",
      "    return 0;",
      "}"
    ]
  },
  "for": {
    "prefix": "for",
    "body": ["for (${int} ${i} = ${1:0}; ${i} < ${2:length}; ++${i}) {", "    $3", "}"],
    "description": "Code snippet for 'for' loop"
  },
  "forj": {
    "prefix": "forj",
    "body": ["for (${int} ${j} = ${1:0}; ${j} < ${2:length}; ++${j}) {", "    $3", "}"],
    "description": "Code snippet for 'for' loop"
  },
  "fork": {
    "prefix": "fork",
    "body": ["for (${size_t} ${k} = ${1:0}; ${k} < ${2:length}; ++${k}) {", "    $3", "}"],
    "description": "Code snippet for 'for' loop"
  },
  "do": {
    "prefix": "do",
    "body": ["do {", "    $1", "} while($2);"],
    "description": "Code snippet for do...while loop"
  },
  "while": {
    "prefix": "while",
    "body": ["while ($1) {", "    $2", "}"],
    "description": ""
  },
  "foreach": {
    "prefix": "foreach",
    "body": ["for(auto &${1:val} : ${2:collection_to_loop}) {", "    $0", "}"],
    "description": "Code snippet for range-based for loop (c++11) statement"
  },
  "if": {
    "prefix": "if",
    "body": [
      "if ($0) {",
      // "    $2",
      "}"
    ],
    "description": "Code snippet for if statement"
  },
  "else": {
    "prefix": "else",
    "body": ["else {", "    $1", "}"],
    "description": "Code snippet for else statement"
  },
  "else if": {
    "prefix": "else if",
    "body": ["else if ($1) {", "    $2", "}"],
    "description": "Code snippet for else-if statement"
  },
  "enum": {
    "prefix": "enum",
    "body": ["enum ${MyEnum} {", "    $1", "};"],
    "description": "Code snippet for enum"
  },
  "enum class": {
    "prefix": "enum class",
    "body": ["enum class ${MyClass} { };"],
    "description": "Code snippet for enum class (c++11)"
  },
  "class": {
    "prefix": "class",
    "body": [
      "class ${MyClass} {",
      "public:",
      "    ${MyClass}();",
      "    ${MyClass}(${MyClass} &&) = default;",
      "    ${MyClass}(const ${MyClass} &) = default;",
      "    ${MyClass} &operator=(${MyClass} &&) = default;",
      "    ${MyClass} &operator=(const ${MyClass} &) = default;",
      "    ~${MyClass}();",
      "",
      "private:",
      "    $1",
      "};",
      "",
      "${MyClass}::${MyClass}() {",
      "}",
      "",
      "${MyClass}::~${MyClass}() {",
      "}"
    ],
    "description": "Code snippet for class"
  },
  "classi": {
    "prefix": "classi",
    "body": [
      "class ${MyClass} {",
      "public:",
      "    ${MyClass}() = default;",
      "    ${MyClass}(${MyClass} &&) = default;",
      "    ${MyClass}(const ${MyClass} &) = default;",
      "    ${MyClass} &operator=(${MyClass} &&) = default;",
      "    ${MyClass} &operator=(const ${MyClass} &) = default;",
      "    ~${MyClass}() = default;",
      "",
      "private:",
      "    $1",
      "};"
    ],
    "description": "Code snippet for class with inline constructor/destructor"
  },
  "interface": {
    "prefix": "interface",
    "body": ["__interface I${Interface} {", "    $1", "};"],
    "description": "Code snippet for interface (Visual C++)"
  },
  "namespace": {
    "prefix": "names",
    "body": ["namespace $1 {", "    $2", "}"]
  },
  "#ifdef": {
    "prefix": "#ifdef",
    "body": ["#ifdef ${DEBUG}", "$1", "#endif // ${DEBUG}"],
    "description": "Code snippet for #ifdef"
  },
  "#ifndef": {
    "prefix": "#ifndef",
    "body": ["#ifndef ${1:1}", "$2", "#endif // !$1"],
    "description": "Code snippet for #ifndef"
  },
  "#if": {
    "prefix": "#if",
    "body": ["#ifdef ${1:0}", "$2", "#endif // $1"],
    "description": "Code snippet for #if"
  },
  "struct": {
    "prefix": "struct",
    "body": ["struct ${1:struct_name} {", "    $0", "};"],
    "description": "Code snippet for struct"
  },
  "switch": {
    "prefix": "switch",
    "body": ["switch (${switch_on}) {", "default:", "    break;$1", "}"],
    "description": "Code snippet for switch statement"
  },
  "try": {
    "prefix": "try",
    "body": ["try {", "    ", "} catch (const std::exception&) {", "    $1", "}"],
    "description": "Code snippet for try catch"
  },
  "union": {
    "prefix": "union",
    "body": ["union ${MyUnion} {", "    $1", "};"],
    "description": "Code snippet for union"
  },
  "cout": {
    "prefix": "cout",
    "body": ["std::cout << \"${0} << std::endl;"],
    "description": "Code snippet for printing to std::cout, provided the header is set"
  },
  "coutl": {
    "prefix": "coutl",
    "body": ["std::cout << std::endl;"],
    "description": "Code snippet for printing to std::cout, provided the header is set"
  },
  "#inc": {
    "prefix": "#in",
    "body": ["#include \"$1\""],
    "description": "Code snippet for #include \" \""
  },
  "#inc<": {
    "prefix": "#inc",
    "body": ["#include <$1>"],
    "description": "Code snippet for #include \" \""
  },
  "#def": {
    "prefix": "#def",
    "body": ["#define \"$1\" \"$2\" "],
    "description": "Code snippet for #define \" \""
  }
}

Updated at: