利用VS-code编写C/C++

一、准备环境

  • 操作系统:Windows
  • 安装VS-code:官网下载并安装
  • 安装编译器:
    1)下载CLang并安装:在LLVM download page,下载 Pre-Built Binaries 中的 Windows (64-bit)。安装页面中选择Add LLVM to the system PATH for all users选项。
    2)下载MinGW-w64:在Sourceforge下载x86_64-posix-seh。将.7z文件解压到系统目录下,bin目录的绝对路径添加到系统环境变量中。
    3)检查是否安装成功:打开cmd,分别输入clang和g++,如果报错信息不是“xx不是内部或外部命令,也不是可运行的程序或批处理文件”,说明安装成功。

二、配置vs-code

  • 安装插件
    需要安装的插件有:C/C++, vscode-clangd, Code Runner
    如果在编辑界面有clangd报错信息,则需要在vscode-clangd设置中需要手动输入clangd.exe的完整路径,点击齿轮->settings -> Extensions -> clangd configuration,在clangd path中写入clangd.exe的完整路径。
  • 设置工作目录(workspace)
    创建一个空文件夹作为工作目录,在该文件夹下创建.vscode目录。添加settings.json, launch.json和tasks.json三个文件。
    1) launch.json:
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch", 
            "type": "cppdbg", 
            "request": "launch", 
            "program": "${fileDirname}/${fileBasenameNoExtension}.exe", 
            "args": [], 
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}", 
            "environment": [], 
            "externalConsole": true, 
            "internalConsoleOptions": "neverOpen", 
            "MIMode": "gdb", 
            "miDebuggerPath": "gdb.exe", 
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": false
                }
            ],
            "preLaunchTask": "Compile"
        }
    ]
}
  1. tasks.json
{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Compile", 
            "command": "clang",
            "args": [
                "${file}",
                "-o", 
                "${fileDirname}/${fileBasenameNoExtension}.exe",
                "-g",
                "-Wall", 
                "-static-libgcc", 
                "--target=x86_64-w64-mingw", 
            ], 
            "type": "process", 
            "group": {
                "kind": "build",
                "isDefault": true 
            },
            "presentation": {
                "echo": true,
                "reveal": "always", 
                "focus": false,
                "panel": "shared" 
            },
            // "problemMatcher":"$gcc" 
        }
    ]
}

3)settings.json

{
    "files.defaultLanguage": "c",
    "editor.formatOnType": true, 
    "editor.suggest.snippetsPreventQuickSuggestions": false, 
    "editor.acceptSuggestionOnEnter": "off",

    "code-runner.runInTerminal": true, 
    "code-runner.executorMap": {
        "c": "cd $dir && clang '$fileName' -o '$fileNameWithoutExt.exe' -Wall -g -O2 -static-libgcc --target=x86_64-w64-mingw -std=c11 && &'$dir$fileNameWithoutExt'",
        "cpp": "cd $dir && clang++ '$fileName' -o '$fileNameWithoutExt.exe' -Wall -g -O2 -static-libgcc --target=x86_64-w64-mingw -std=c++17 && &'$dir$fileNameWithoutExt'"
    },
    "code-runner.saveFileBeforeRun": true,
    "code-runner.preserveFocus": true, 
    "code-runner.clearPreviousOutput": false,
    "code-runner.ignoreSelection": true,
    "C_Cpp.clang_format_sortIncludes": true, 
    "C_Cpp.errorSquiggles": "Disabled", 
    "C_Cpp.autocomplete": "Disabled", 
    "C_Cpp.suggestSnippets": false, 
}
  1. 在工作目录下创建compile_flags.txt
-Wall
--target=x86_64-w64-mingw
-std=c++17

三、编译和调试

在工作目录下创建helloworld.cpp:

#include 
using namespace std;
int main(){
    cout << "hello world" << endl;
    return 0;
}

保存文件后Ctrl+Shift+B编译,.\a.exe运行;或者F5编译加调试加运行。更多关于调试的信息可以参见VSCode官网。如果以上配置都没问题的话,就可以得到输出“hello world”了。

[以上内容参考谭九鼎的知乎回答:Visual Studio Code 如何编写运行 C、C++ 程序?]

你可能感兴趣的:(利用VS-code编写C/C++)