Mac VScode (写C/C++, 编译多个文件)

1,729 阅读1分钟

配置环境

  • 在Extension里面找到C/C++插件并下载

  • 找到code runner安装

  • 在终端输入clang --version 检查是否已将安装clang

  • 打开vscode, 新建一个文件夹,编写一个简单的helloworld.cpp文件保存在此文件夹下

  • shift + command + P( 输入shell, 选择install'code' command in PATH)

  • shift + command + B (编译)

  • 打开terminal, 点击 configure default build 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": [
        "-std=c++17",
        "-stdlib=libc++",
        "-g",
        "${file}",
        "-o",
        "${fileDirname}/${fileBasenameNoExtension}"
      ],
      "options": {
        "cwd": "${workspaceFolder}"
      },
      "problemMatcher": ["$gcc"],
      "group": {
        "kind": "build",
        "isDefault": true
      }
    }
  ]
}
  • You can modify your tasks.json to build multiple C++ files by using an argument like "${workspaceFolder}/*.cpp" instead of ${file}. This will build all .cpp files in your current folder. You can also modify the output filename by replacing(将${file}替换即可)