Skip to content

门泊吴船亦已谋

VS Code 写一切 C 与 C++ 单文件

配置很简单,刷题贼方便
这里以 C++为例,C 同理

C/C++环境配置

跟 VS Code 无关,但还是提一下

Win 下可以装 MinGW 或者 Clang Max 下装 Xcode 自带 Clang 只要环境变量中有 C/C++的编译器就行了

需要安装的 VS Code 插件

  • C/C++

工作区配置

打开一个空文件夹,在其中新建文件 hello.cpp,编辑内容如下

#include <cstdio>

using namespace std;

int main() {
    printf("Hello world");
    return 0;
}

按 F5 直接运行
VS Code 提示选择环境,这里选择 C++

Select env

选择配置,随便一个 C++编译器都行,我选了第一个 clang++

Select config

接下来 VS Code 自动生成了一个 launch.json 文件,我贴一下 Mac 下的

{
    // 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": "clang++ build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "lldb",
            "preLaunchTask": "clang++ build active file"
        }
    ]
}

解释:启动类型为 cppdbg,有各种参数,其中
externalConsole 表示是否使用外部终端,如果需要用标准输入,这里要改成 true
MIMode 为调试器,Mac 下用 lldb,Win 下用 gdb
preLaunchTask 表示 launch 之前执行的 task

这个 launch 其实只配置了调试器,它需要先执行 preLaunchTask 中配置的 task 用于编译,而这个 task 我们现在还没有请往后看

切回之前的 hello.cpp,再次按下 F5
提示我们还没有配置 task,于是点击 Configure Task

No such task warning

选与之前 launch 一样的,所以我选择 clang++

Create task

他生成了一个 tasks.json 文件,可以看到里面有个任务配置了编译器与编译命令等

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558 
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "clang++ build active file",
            "command": "/usr/bin/clang++",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "/usr/bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build"
        }
    ]
}

切回 hello.cpp,F5,这次可以运行了

Result

CodeLLDB 插件

也可以使用这个插件调试 C/C++程序,它能在集成终端中进行输入
但调试控制台使用的是 lldb 命令就比较蛋疼

另外升级 macOS Catalina 之后似乎 C/C++ 插件出了点问题暂时不能调试,换成这个插件就行

MSVC

Windows 下也可以使用微软提供的编译器。 去 VS 的官网找到 Build Tools,然后安装的时候勾选 MSVC 的 Buildtools 的某一个比较新的版本就行了。

这个时候我们就可以使用 cl.exe 来编译源文件。 但是注意到 cl.exe 以及标准库啥的都不会被自动添加到环境变量中,所以直接在终端输入 cl 会发现找不到。 我们需要使用微软提供的 Developer Command Prompt for VS 或者 Developer PowerShell for VS。 它们会自动配好环境变量,这个时候我们在里面输入 cl 就能看到版本号了。 因此我们需要在这玩意里面输入 code 打开 VS Code 才能找到 cl.exe 以及相关的库。

那么 tasks.json 就大概长这样。

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "C/C++: cl.exe build active file",
            "command": "cl.exe",
            "args": [
                "/Zi",
                "/EHsc",
                "/Fe:",
                "${fileDirname}\\build\\${fileBasenameNoExtension}.exe",
                "${file}"
            ]
        }
    ]
}

于是 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": "(Windows) 启动",
            "type": "cppvsdbg",
            "request": "launch",
            "program": "${workspaceFolder}/build/${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "console": "integratedTerminal",
            "preLaunchTask": "C/C++: cl.exe build active file"
        }
    ]
}