多平台VS Code中CPP编译环境配置脚本

  截止到目前,我已经在使用Windows、Ubuntu、macOS三个操作系统了。其中,后两者是基于Unix的操作系统,但是又有些许不同。除了正式地使用cmake构建的c++项目以外,我也经常写一些单文件或者多文件的cpp示例程序。考虑到我会在不同的设备和平台之间共享编译我的代码,所以经常需要在三种平台上的VS Code中配置CPP的编译环境。虽然,我已经熟练掌握了VS Code中配置的方法和操作。但是,程序员天生地就讨厌重复地操作(能让电脑完成的,就不要动手)。因此,我分别编写了适用于三个平台的配置脚本,以方便自己的使用。
  本文会给出三个平台的脚本,介绍适用条件和使用方法。

前置说明

  通用的基础条件:你需要安装VS Code,并安装C/C++插件。
  本文中的脚本有两个主要的功能:
  1.将你打开的当前cpp文件编译成可执行程序,存放在工作区的bin目录中。
  2.将你打开的当前cpp文件所在的目录中所有源代码编译成可执行程序,存放在工作区的bin目录中。
  上述两个功能对应调试任务中的两个选项,请根据你要编译的文件选择对应的调试任务。将可执行程序单独存放在bin目录中是为了将可执行程序与源代码分开,方便代码的管理。Windows上的脚本执行完以后还需要修改部分配置项目为你电脑上的对应路径。

Windows

  请保证已经安装了g++可执行程序并添加到系统变量中。MinGW的安装和配置可以参考这篇博客安装和配置MinGW小节。当然,你可以通过在终端中输入g++ --version命令查看是否安装。
  脚本generate_config_win_mingw.bat

@echo off

if not exist ".vscode" (
    mkdir .vscode
)

(
    echo {
    echo     "version": "0.2.0",
    echo     "configurations": [
    echo         {
    echo             "name": "cppdbg: Single File",
    echo             "type": "cppdbg",
    echo             "request": "launch",
    echo             "program": "${workspaceFolder}/bin/${fileBasenameNoExtension}.exe",
    echo             "args": [],
    echo             "stopAtEntry": false,
    echo             "cwd": "${workspaceFolder}/bin",
    echo             "environment": [],
    echo             "externalConsole": false,
    echo             "MIMode": "gdb",
    echo             "miDebuggerPath": "path/mingw64/bin/gdb.exe",
    echo             "setupCommands": [
    echo                 {
    echo                     "description": "enable pretty printing for gdb",
    echo                     "text": "-enable-pretty-printing",
    echo                     "ignoreFailures": true
    echo                 },
    echo                 {
    echo                     "description": "set disassembly flavor to intel",
    echo                     "text": "-gdb-set disassembly-flavor intel",
    echo                     "ignoreFailures": true
    echo                 }
    echo             ],
    echo             "preLaunchTask": "C/C++: build active file"
    echo         },
    echo         {
    echo             "name": "cppdbg: Multiple Files",
    echo             "type": "cppdbg",
    echo             "request": "launch",
    echo             "program": "${workspaceFolder}/bin/${relativeFileDirname}.exe",
    echo             "args": [],
    echo             "stopAtEntry": false,
    echo             "cwd": "${workspaceFolder}/bin",
    echo             "environment": [],
    echo             "externalConsole": false,
    echo             "MIMode": "gdb",
    echo             "miDebuggerPath": "path/mingw64/bin/gdb.exe",
    echo             "setupCommands": [
    echo                 {
    echo                     "description": "enable pretty printing for gdb",
    echo                     "text": "-enable-pretty-printing",
    echo                     "ignoreFailures": true
    echo                 },
    echo                 {
    echo                     "description": "set disassembly flavor to intel",
    echo                     "text": "-gdb-set disassembly-flavor intel",
    echo                     "ignoreFailures": true
    echo                 }
    echo             ],
    echo             "preLaunchTask": "C/C++: build active folder"
    echo         }
    echo     ]
    echo }
) > .vscode\launch.json

(
    echo {
    echo      "tasks": [
    echo          {
    echo              "type": "cppbuild",
    echo              "label": "C/C++: build active file",
    echo              "command": "g++.exe",
    echo              "args": [
    echo                  "-g",
    echo                  "-std=c++17",
    echo                  "${file}",
    echo                  "-o",
    echo                  "${workspaceFolder}/bin/${fileBasenameNoExtension}.exe"
    echo              ],
    echo              "options": {
    echo                  "cwd": "${fileDirname}"
    echo              },
    echo              "problemMatcher": [
    echo                  "$gcc"
    echo              ],
    echo              "group": {
    echo                  "kind": "build",
    echo                  "isDefault": true
    echo              },
    echo              "presentation": {
    echo                  "echo": true,
    echo                  "reveal": "always",
    echo                  "focus": false,
    echo                  "panel": "shared",
    echo                  "showReuseMessage": true,
    echo                  "clear": true
    echo              }
    echo          },
    echo          {
    echo              "type": "cppbuild",
    echo              "label": "C/C++: build active folder",
    echo              "command": "g++.exe",
    echo              "args": [
    echo                  "-g",
    echo                  "-std=c++17",
    echo                  "${fileDirname}/*.cpp",
    echo                  "-o",
    echo                  "${workspaceFolder}/bin/${relativeFileDirname}.exe"
    echo              ],
    echo              "options": {
    echo                  "cwd": "${fileDirname}"
    echo              },
    echo              "problemMatcher": [
    echo                  "$gcc"
    echo              ],
    echo              "group": {
    echo                  "kind": "build",
    echo                  "isDefault": true
    echo              },
    echo              "presentation": {
    echo                  "echo": true,
    echo                  "reveal": "always",
    echo                  "focus": false,
    echo                  "panel": "shared",
    echo                  "showReuseMessage": true,
    echo                  "clear": true
    echo              }
    echo          }
    echo      ],
    echo      "version": "2.0.0"
    echo }
) > .vscode\tasks.json

(
    echo {
    echo     "configurations": [
    echo         {
    echo             "name": "Win32",
    echo             "includePath": [
    echo                 "${workspaceFolder}/**"
    echo             ],
    echo             "defines": [
    echo                 "_DEBUG",
    echo                 "UNICODE",
    echo                 "_UNICODE"
    echo             ],
    echo             "compilerPath": "path/mingw64/bin/g++.exe",
    echo             "cStandard": "c17",
    echo             "cppStandard": "c++17",
    echo             "intelliSenseMode": "gcc-x64"
    echo         }
    echo     ],
    echo     "version": 4
    echo }
) > .vscode\c_cpp_properties.json

  使用后,该脚本会在当前工作区中生成.vscode目录和其中的三个配置文件。你需要修改launch.json文件中的miDebuggerPath路径为你的mingw中gdb的路径,修改c_cpp_properties文件中的compilerPath为你的mingw中的g++路径。第一个修改项用于启动调试,第二个修改项目用于识别标准库的头文件以便vscode能够准确的提示补全。

Linux

  请保证已经安装了g++可执行程序。可以使用sudo apt install build-essential进行安装。确保该脚本有可执行权限,执行完成后无需修改即可使用。
  脚本generate_config_linux.sh

#!/bin/bash

OUTPUT_DIR=".vscode"

if [ ! -d "$OUTPUT_DIR" ]; then
    mkdir "$OUTPUT_DIR"
fi

cat <<EOL > .vscode/launch.json
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "cppdbg: Single File",
            "type": "cppdbg",
            "request": "launch",
            "program": "\${workspaceFolder}/bin/\${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "\${workspaceFolder}/bin",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "enable pretty printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "set disassembly flavor to intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: build active file"
        },
        {
            "name": "cppdbg: Multiple Files",
            "type": "cppdbg",
            "request": "launch",
            "program": "\${workspaceFolder}/bin/\${relativeFileDirname}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "\${workspaceFolder}/bin",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "enable pretty printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "set disassembly flavor to intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: build active folder"
        }
    ]
}
EOL

cat <<EOL > .vscode/tasks.json
{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: build active file",
            "command": "g++",
            "args": [
                "-g",
                "-std=c++17",
                "\${file}",
                "-o",
                "\${workspaceFolder}/bin/\${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "\${fileDirname}"
            },
            "problemMatcher": [
                "\$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "调试器生成的任务。",
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": false,
                "panel": "shared",
                "showReuseMessage": true,
                "clear": true
            }
        },
        {
            "type": "cppbuild",
            "label": "C/C++: build active folder",
            "command": "g++",
            "args": [
                "-g",
                "-std=c++17",
                "\${fileDirname}/*.cpp",
                "-o",
                "\${workspaceFolder}/bin/\${relativeFileDirname}"
            ],
            "options": {
                "cwd": "\${fileDirname}"
            },
            "problemMatcher": [
                "\$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "调试器生成的任务。",
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": false,
                "panel": "shared",
                "showReuseMessage": true,
                "clear": true
            }
        }
    ],
    "version": "2.0.0"
}
EOL

cat <<EOL > .vscode/c_cpp_properties.json
{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "\${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "/usr/bin/g++",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "linux-gcc-x64"
        }
    ],
    "version": 4
}
EOL

Macos

  请保证已经安装了clang可执行程序。可以使用xcode-select --install进行安装。确保该脚本有可执行权限,执行完成后无需修改即可使用。
  脚本generate_config_macos.sh

#!/bin/bash

OUTPUT_DIR=".vscode"

if [ ! -d "$OUTPUT_DIR" ]; then
    mkdir "$OUTPUT_DIR"
fi

cat <<EOL > .vscode/launch.json
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "cppdbg: Single File",
            "type": "cppdbg",
            "request": "launch",
            "program": "\${workspaceFolder}/bin/\${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "\${workspaceFolder}/bin",
            "environment": [],
            "externalConsole": true,
            "MIMode": "lldb",
            "setupCommands": [
                {
                    "description": "enable pretty printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "set disassembly flavor to intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: build active file"
        },
        {
            "name": "cppdbg: Multiple Files",
            "type": "cppdbg",
            "request": "launch",
            "program": "\${workspaceFolder}/bin/\${relativeFileDirname}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "\${workspaceFolder}/bin",
            "environment": [],
            "externalConsole": true,
            "MIMode": "lldb",
            "setupCommands": [
                {
                    "description": "enable pretty printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "set disassembly flavor to intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: build active folder"
        }
    ]
}
EOL

cat <<EOL > .vscode/tasks.json
{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: build active file",
            "command": "clang++",
            "args": [
                "-g",
                "-std=c++17",
                "\${file}",
                "-o",
                "\${workspaceFolder}/bin/\${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "\${fileDirname}"
            },
            "problemMatcher": [
                "\$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "调试器生成的任务。",
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": false,
                "panel": "shared",
                "showReuseMessage": true,
                "clear": true
            }
        },
        {
            "type": "cppbuild",
            "label": "C/C++: build active folder",
            "command": "clang++",
            "args": [
                "-g",
                "-std=c++17",
                "\${fileDirname}/*.cpp",
                "-o",
                "\${workspaceFolder}/bin/\${relativeFileDirname}"
            ],
            "options": {
                "cwd": "\${fileDirname}"
            },
            "problemMatcher": [
                "\$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "调试器生成的任务。",
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": false,
                "panel": "shared",
                "showReuseMessage": true,
                "clear": true
            }
        }
    ],
    "version": "2.0.0"
}
EOL

cat <<EOL > .vscode/c_cpp_properties.json
{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "\${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "/usr/bin/clang++",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "macos-clang-arm64"
        }
    ],
    "version": 4
}
EOL

写在后面的

  VS Code中配置这样的环境更偏向于简单的学习场景,仅仅是使用了vscode的功能自动执行了g++等编译器的编译命令而已。如果,你需要一个完整的工程项目,并且引用了若干第三方库,cmake功能是更好的选择。如果你在Windows平台中想使用msvc进行编译,通过类似的脚本和配置文件也是可以做到的。不过,你如果已经用msvc了,请直接使用宇宙第一IDE——Visual Studio 2022吧!
  写至深夜,改天有空的时候再出一个演示视频吧,今天就先到这里了。


当珍惜每一片时光~