学习计算机科学的第一天,我们就需要选择一个编译器来编译自己写好的代码。在大半个学期里我一直使用Visual Studio完成作业,但是VS作为宇宙第一IDE,用来编译单文件的课后作业显得太臃肿,且VS以MSBuild为编译内核,容错性与作业评测机的GCC有些差异。如无必要,勿增实体。如果VSCode能完成这件事,何苦还要在每次启动VS时忍受漫长的等待呢?

但是别忘了VSCode仅仅是一个文本编辑器,使用它来编译C/C++代码我们还需要一些插件的加持,以及,我们的电脑里需要有一个编译器。这里梳理下如何使用Visual Studio Code连接到电脑里安装的Clang编译器,做到在VSCode里顺畅编写代码的同时能无痛编译。

环境

除了Visual Studio Code以外,你还需要一个编译器,这里我们以Clang为例。

首先在这里下载LLVM,选择 Pre-Built Binaries中的Clang for Windows (64-bit) 然后安装,记住自己的安装路径,如我的路径是 C:\Program Files\LLVM. 安装时请注意勾选 Add LLVM to the system PATH 以将其添加到环境变量。

但是此时安装好的LLVM其实不包括我们编译时需要的头文件。所以我们需要下载MinGW来获取头文件。MinGW可以随便装在哪里,比如我就装在桌面,以便到时候卸载。安装好MinGW以后将其目录下(如我的是 C:\Users\Yzstr\Desktop\mingw-w64\x86_64-7.1.0-posix-seh-rt_v5-ref1)下的所有文件复制到你的LLVM安装目录(如我的是 C:\Program Files\LLVM)下,里面的内容可以无冲突合并。

完成以上的步骤以后,在PowerShell上试着输入命令clang,如果它显示 no input files 那么就是成功了。

拓展

  • CodeRunner
  • C/C++
  • Include Autocomplete
  • C/C++ Snippets
  • C/C++ Clang Command Adapter

以及以下是一些可选插件

  • GBKtoUTF8
  • Bracket Pair Colorizer

配置

在你存放代码的文件夹里新建一个名为.vscode的文件夹,用来存放配置文件。

这里提一下在Windows资源管理器下如何以「 . 」为首字符重命名一个文件夹。其实很简单,你只要在最末尾也加一个点就行了,系统会自动抹除末尾的点。如如果你要以.vscode命名文件夹,重命名时请输入.vscode.便可以了。

这个文件夹下需要有launch.jsontasks.jsonsettings.json_cpp_properties.json四个文件,下面我直接将我电脑上的内容贴上来。

lanuch.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" 
        }
    ]
}

settings.json



{
    "files.defaultLanguage": "c", 
    "code-runner.runInTerminal": true, 
    "code-runner.executorMap": {
        "c": "cd $dir && clang $fileName -o $fileNameWithoutExt.exe -Wall -g -Og -static-libgcc -fcolor-diagnostics --target=x86_64-w64-mingw -std=c11 && $dir$fileNameWithoutExt",
        "cpp": "cd $dir && clang++ $fileName -o $fileNameWithoutExt.exe -Wall -g -Og -static-libgcc -fcolor-diagnostics --target=x86_64-w64-mingw -std=c++17 && $dir$fileNameWithoutExt"
    },
    "code-runner.saveFileBeforeRun": true, 
    "code-runner.preserveFocus": false, 
    "code-runner.clearPreviousOutput": false, 
    "C_Cpp.clang_format_sortIncludes": true, 
    "C_Cpp.intelliSenseEngine": "Default", 
    "C_Cpp.errorSquiggles": "Disabled", 
    "editor.formatOnType": true, 
    "editor.snippetSuggestions": "top",
    "clang.cflags": [ 
        "--target=x86_64-w64-mingw",
        "-std=c11",
        "-Wall"
    ],
    "clang.cxxflags": [ 
        "--target=x86_64-w64-mingw",
        "-std=c++17",
        "-Wall"
    ],
    "clang.completion.enable": false,
    "files.associations": {
        "stdio.h": "c",
        "string.h": "c"
    },
}

tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Compile", 
            "command": "clang++", 
            "args": [
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}.exe",
                "-g", 
                "-Wall", 
                "-static-libgcc", 
                "-fcolor-diagnostics",
                "--target=x86_64-w64-mingw",
                "-std=c++17" 
            ],
            "type": "shell",
            "group": {
                "kind": "build",
                "isDefault": true 
            },
            "presentation": {
                "echo": true,
                "reveal": "always", 
                "focus": false, 
                "panel": "shared" 
            }
        }
    ]
}

c_cpp_properties.json



{
    "configurations": [
        {
            "name": "Win32",
            "intelliSenseMode": "clang-x64",
            "includePath": [
                "${workspaceFolder}",
                "C:/Program Files/llvm/lib/gcc/x86_64-w64-mingw32/7.2.0/include/c++",
                "C:/Program Files/llvm/lib/gcc/x86_64-w64-mingw32/7.2.0/include/c++/x86_64-w64-mingw32",
                "C:/Program Files/llvm/lib/gcc/x86_64-w64-mingw32/7.2.0/include/c++/backward",
                "C:/Program Files/llvm/lib/gcc/x86_64-w64-mingw32/7.2.0/include",
                "C:/Program Files/llvm/include",
                "C:/Program Files/llvm/x86_64-w64-mingw32/include",
                "C:/Program Files/llvm/lib/gcc/x86_64-w64-mingw32/7.2.0/include-fixed"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "__GNUC__=7",
                "__cdecl=__attribute__((__cdecl__))"
            ],
            "browse": {
                "path": [
                    "${workspaceFolder}",
                    "C:/Program Files/llvm/lib/gcc/x86_64-w64-mingw32/7.2.0/include/c++",
                    "C:/Program Files/llvm/lib/gcc/x86_64-w64-mingw32/7.2.0/include/c++/x86_64-w64-mingw32",
                    "C:/Program Files/llvm/lib/gcc/x86_64-w64-mingw32/7.2.0/include/c++/backward",
                    "C:/Program Files/llvm/lib/gcc/x86_64-w64-mingw32/7.2.0/include",
                    "C:/Program Files/llvm/include",
                    "C:/Program Files/llvm/x86_64-w64-mingw32/include",
                    "C:/Program Files/llvm/lib/gcc/x86_64-w64-mingw32/7.2.0/include-fixed"
                ],
                "limitSymbolsToIncludedHeaders": true,
                "databaseFilename": ""
            }
        }
    ],
    "version": 3
}

请注意将配置文件中的路径改为你电脑中LLVM的路径。

以上四个文件的一些属性设置可以按照自己的需求修改。

编译

完成了上面的步骤,就可以开始编译代码了。在完成代码的时候使用快捷键Ctrl+Alt+N,编译结果/输出会在VSCode的终端显示。如果你希望能像Dev-CPP或Visual Studio一样在跳出的命令窗口显示,将settings.json"code-runner.runInTerminal"的值改为false.

参考

支付宝扫码打赏 微信打赏

若你觉得我的文章对你有帮助,欢迎点击上方按钮对我打赏

扫描二维码,分享此文章

Yzstr Andy's Picture
Yzstr Andy

School of Data and Computer Science, SUN YAT-SEN UNIVERSITY