VSCode使用Clangd自用配置教程

· · 科技·工程

Visual Studio Code配置教程

using namespace std;

char command[1010]; int ret; double s, e; HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);

void SetFont(const wchar_t *s) { CONSOLE_FONT_INFOEX fontInfo; fontInfo.cbSize = sizeof(fontInfo); fontInfo.nFont = 1; fontInfo.dwFontSize.X = 0; fontInfo.dwFontSize.Y = 30; fontInfo.FontFamily = FF_DONTCARE; fontInfo.FontWeight = FW_NORMAL; wcscpy(fontInfo.FaceName, s); SetCurrentConsoleFontEx(hOut, 0, &fontInfo); }

int main(int n, char args[]) { SetFont(L"Consolas"); if (n <= 1) { cout << "Build failed. \n"; return 0; } sprintf(command, "clang++ %s.cpp -o %s ", args[1], args[1]); for (int i = 2; i < n; i++) { strcat(command, args[i]); strcat(command, " "); } cout << "Compiling. . . \n"; ret = system(command); if (ret) cout << "Compilation failed with return value " << ret << endl; else { cout << "Compilation completed. \n"; cout << "Running. . . \n"; s = 1.0 clock() / CLOCKS_PER_SEC; sprintf(command, "%s", args[1]); ret = system(command); e = 1.0 * clock() / CLOCKS_PER_SEC; cout << "\nTask exited with return value " << ret << " in " << setprecision(3) << fixed << e - s << " seconds. \n"; } system("pause"); return 0; }

- 在命令行里将build.cpp编译成build.exe
- build相当于是一个编译运行文件, 比如如果你在使用A.cpp, 你可以运行`build A`
- 在`.vscode\tasks.json`写入
```json
{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "start",
            "args": [
                "cmd",
                "/c",
                "build",
                "${fileDirname}\\${fileBasenameNoExtension}"
            ],
            "presentation": {
                "reveal": "silent"
            },
            "options": {
                "cwd": "${workspaceFolder}",
                "shell": {
                    "executable": "cmd",
                    "args": [
                        "/c"
                    ]
                }
            },
            "problemMatcher": []
        }
    ]
}

如果你想要格式化代码可以按Alt+Shift+F或者你可以在设置里面搜索Format On ...