温馨提示×

vscode如何运行多个cpp文件

小亿
106
2024-05-22 18:56:13
栏目: 编程语言

在VSCode中运行多个cpp文件的方法有两种:

  1. 使用多个终端窗口:在VSCode的顶部菜单栏选择"终端"->“新建终端”,可以打开多个终端窗口,每个窗口可以运行一个cpp文件。在每个终端窗口中使用g++编译cpp文件,然后运行生成的可执行文件。

  2. 使用任务:在VSCode中可以创建任务来编译和运行cpp文件。首先在项目根目录下创建一个名为tasks.json的文件,内容如下:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "g++",
            "args": ["-o", "${fileDirname}/${fileBasenameNoExtension}", "${file}"],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        {
            "label": "run",
            "type": "shell",
            "command": "${fileDirname}/${fileBasenameNoExtension}",
            "group": {
                "kind": "test",
                "isDefault": true
            }
        }
    ]
}

然后在VSCode中按下Ctrl+Shift+P,输入"任务"并选择"运行任务"->“运行任务”,选择"build"任务来编译cpp文件,然后再选择"run"任务来运行生成的可执行文件。这样就可以在同一个终端窗口中运行多个cpp文件了。

0