Vscode 編譯c++配置(含Code Runner)

優點:編譯exe保存在build文件夾裏

缺點:coderunner不是c++11

修正缺點(編譯速度起飛,就是萬能頭文件不能用):

https://blog.csdn.net/zcpvn/article/details/86542216

https://www.jianshu.com/p/22134abb2c90

即:

"cpp": "clang++ --target=x86_64-w64-mingw32 $fileName -o build/$fileNameWithoutExt.exe -std=c++14 && .\\build\\$fileNameWithoutExt.exe",

acmtemp.code-snippets

{
	// 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": "acm",
		"body": [
			"#include <bits/stdc++.h>"
			"using namespace std;",
			"typedef long long ll;",
			"typedef unsigned long long ull;",
			"#define mem(s) memset(s, 0, sizeof(s))",
			"const int INF = 0x3f3f3f3f;",
			"const double eps = 1e-8;",
			"const int maxn = 1e6+5;",
			"const int mod = 998244353;",
			"int main() ",
			"{",
				
			"	return 0;",
			"}",
		],
		"description": "Log output to console"
	}
}

c_cpp_properties.json

{
    "configurations": [
        {
            "name": "MinGW",
            "intelliSenseMode": "gcc-x64",
            "compilerPath": "C:\\MinGW\\bin\\gcc.exe",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "cStandard": "c11",
            "cppStandard": "c++17"
        }
    ],
    "version": 4
}

launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/build/${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            // 這裏填你 MinGW-w64 安裝目錄下的 gdb 路徑
            "miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            // 這裏要與你在 tasks.json 中配置的 label 一致
            "preLaunchTask": "compile",
        }
    ]
}

settings.json

{
    // 在終端中運行編譯命令,否則我們無法與程序通過標準輸入交互
    "code-runner.runInTerminal": true,
    // 如果你全局設置中的默認終端是 WSL 之類的,那麼可以在工作區設置中改回 PowerShell
    "terminal.integrated.shell.windows": "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
    //"terminal.external.windowsExec": "C:\\WINDOWS\\System32\\bash.exe",
    //"terminal.integrated.shell.windows": "C:\\WINDOWS\\System32\\bash.exe",
    // 運行代碼之前清除之前的輸出
    "code-runner.clearPreviousOutput": true,
    // 開啓這個後在運行編譯命令之前會自動 cd 至文件所在目錄
    "code-runner.fileDirectoryAsCwd": true,
    // 因爲上面那個選項會自動 cd,所以我刪除了默認編譯命令中的 cd 語句
    // 同時我將編譯結果的輸出目錄修改爲了同目錄下的 build 文件夾
    // 不然源碼文件和編譯結果混雜在一個目錄中非常雜亂(尤其是刷題時)
    // 這裏只保留了 C 和 C++ 的編譯命令,有需要其他語言的請自行添加
    "code-runner.executorMap": {
        "c": "gcc $fileName -o build/$fileNameWithoutExt && .\\build\\$fileNameWithoutExt",
        "cpp": "g++ $fileName -o build/$fileNameWithoutExt && .\\build\\$fileNameWithoutExt",
    },
    // 運行代碼後切換焦點至終端,方便直接輸入測試數據
    "code-runner.preserveFocus": false,
    // 在運行代碼之前保存文件
    "code-runner.saveFileBeforeRun": true,
    "python.pythonPath": "C:\\Program Files (x86)\\python\\python.exe",
    "files.exclude": {
        "**/.classpath": true,
        "**/.project": true,
        "**/.settings": true,
        "**/.factorypath": true
    },
    "files.associations": {
        "iostream": "cpp",
        "ostream": "cpp",
        "vector": "cpp",
        "chrono": "cpp",
        "limits": "cpp",
        "valarray": "cpp",
        "unordered_map": "cpp",
        "array": "cpp",
        "atomic": "cpp",
        "hash_map": "cpp",
        "*.tcc": "cpp",
        "bitset": "cpp",
        "cctype": "cpp",
        "cfenv": "cpp",
        "charconv": "cpp",
        "cinttypes": "cpp",
        "clocale": "cpp",
        "cmath": "cpp",
        "codecvt": "cpp",
        "complex": "cpp",
        "condition_variable": "cpp",
        "csetjmp": "cpp",
        "csignal": "cpp",
        "cstdarg": "cpp",
        "cstddef": "cpp",
        "cstdint": "cpp",
        "cstdio": "cpp",
        "cstdlib": "cpp",
        "cstring": "cpp",
        "ctime": "cpp",
        "cuchar": "cpp",
        "cwchar": "cpp",
        "cwctype": "cpp",
        "deque": "cpp",
        "forward_list": "cpp",
        "list": "cpp",
        "unordered_set": "cpp",
        "exception": "cpp",
        "fstream": "cpp",
        "functional": "cpp",
        "future": "cpp",
        "initializer_list": "cpp",
        "iomanip": "cpp",
        "iosfwd": "cpp",
        "istream": "cpp",
        "memory": "cpp",
        "mutex": "cpp",
        "new": "cpp",
        "numeric": "cpp",
        "optional": "cpp",
        "ratio": "cpp",
        "scoped_allocator": "cpp",
        "shared_mutex": "cpp",
        "sstream": "cpp",
        "stdexcept": "cpp",
        "streambuf": "cpp",
        "string_view": "cpp",
        "system_error": "cpp",
        "thread": "cpp",
        "type_traits": "cpp",
        "tuple": "cpp",
        "typeindex": "cpp",
        "typeinfo": "cpp",
        "utility": "cpp"
    },
}

tasks.json

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "compile",
      "type": "shell",
      "command": "g++",
      "args": [
        "-g",
        "\"${file}\"",
        "-o",
        "\"${fileDirname}\\build\\${fileBasenameNoExtension}\""
      ],
      "presentation": {
        "reveal": "always",
        "panel": "shared",
        "focus": false,
        "echo": true
      },
      "group": {
        "kind": "build",
        "isDefault": true
      },
      "problemMatcher": {
        "owner": "cpp",
        "fileLocation": "absolute",
        "pattern": {
          "regexp": "^(.*):(\\d+):(\\d+):\\s+(error):\\s+(.*)$",
          "file": 1,
          "line": 2,
          "column": 3,
          "severity": 4,
          "message": 5
        }
      }
    },
    {
      "type": "shell",
      "label": "g++.exe build active file",
      "command": "C:\\MinGW\\bin\\g++.exe",
      "args": [
        "-g",
        "${file}",
        "-o",
        "${fileDirname}\\${fileBasenameNoExtension}.exe"
      ],
      "options": {
        "cwd": "C:\\MinGW\\bin"
      }
    }
  ]
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章