2

官网中配置页传送门

环境配置

1 准备

  • vscode 软件
  • C/C++ Extension Pack 插件

  • clang/clang++ 编译器

    • 检查已安装clang++

      clang++ -v
    • 如果未安装,请前往 app store 下载 xcode

2 .vscode配置

在当前工作区准备以下文件(夹)

  • .vscode

    • tasks.json # 用于编译c++文件
    • launch.json # 用于使用vscode自带的debug工具(左侧的小虫图标)
    • c_cpp_properties.json # 用于使用vscode自带的代码提示工具如 IntelliSense
  • main.cpp

    #include <iostream>
    #include <vector>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};
    
        for (const string& word : msg)
        {
            cout << word << " ";
        }
        cout << endl;
    }

配置tasks.json

command + shift + b,vscode会执行tasks.json中的任务。

本文配置的是c++编译,替换tasks.json的内容如下:

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "Build with Clang", //这个任务的名字在launch.json最后一项配置
      "type": "shell",
      "command": "clang++",
      "args": [
        "-std=c++17",
        "-stdlib=libc++",
        "-g",
        // 生成调试信息,GUN可使用该参数
        "${file}",
        // file指正在打开的文件
        "-o",
        // 生成可执行文件
        "${fileDirname}/${fileBasenameNoExtension}"
        // fileDirname指正在打开的文件所在的文件夹
        // fileBasenammeNoExtension指没有扩展名的文件,unix中可执行文件属于此类
      ],
      "options": {
        "cwd": "${workspaceFolder}"
      },
      "problemMatcher": ["$gcc"],
      "group": {
        "kind": "build",
        "isDefault": true
      }
    }
  ]
}

打开main.cpp,按command + shift + b(注意,按这组快捷键时main.cpp文件在vscode中是正在打开的),如下图

工作区中没有扩展名的main就是在mac os下生成的可执行文件(同理,如果是windows,有扩展名.exe)

在vscode内置的终端中,可以运行main,如下图:

配置 c_cpp_properties.json

c_cpp_properties.json 的作用是:代码提示、代码跳转等

{
  "configurations": [
    {
      "name": "Mac",
      "includePath": [
        "${workspaceFolder}/**",
        "/Library/Developer/CommandLineTools/usr/include",
        "/Library/Developer/CommandLineTools/usr/lib/clang/11.0.3/include",
        // 请根据你的clang版本修改,我这里是11.0.3
        "/usr/local/include"
      ],
      "defines": [],
      "macFrameworkPath": [
        "/System/Library/Frameworks",
        "/Library/Frameworks"
      ],
      "compilerPath": "/usr/bin/clang++",
      "cStandard": "c11",
      "cppStandard": "c++17",
      "intelliSenseMode": "clang-x64"
      // "compileCommands": "${workspaceFolder}/build/compile_commands.json"
    }
  ],
  "version": 4
}

配置launch.json

launch.json是调用vscode debug功能的配置文件

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "lldb",
      "request": "launch",
      "name": "Debug",
      "program": "${workspaceFolder}/${fileBasenameNoExtension}",
      "args": [],
      "cwd": "${workspaceFolder}",
      "preLaunchTask": "Build with Clang"
    }
  ]
}

打开debug窗口,打好断点,按下图操作

这三个配置文件中的参数说明,在官网都有,我把一部分写在了注释里

踩坑

1. c_cpp_properties.json设置的cppstander“失效”了

结论:检查vscode是否安装了C/C++ Clang Command Adapter,有的话就删除

原因应该是这个插件不继承c_cpp_properties.json中设置的C++版本

我在翻官方文档之前,看到的配置博客说需要下载C/C++ Clang Command Adapter插件,实际上这个插件并非按C++11版本给出错误提示,出现如下图的状况

C++11标准中,列表初始化 vector对象,在这里有报错提示,而且提示中没有标明是哪个插件提供的。难道vscode自身有C++语法提示,而且版本较老?当然,这个猜想是错的。

我尝试把C++ Extension Pack卸载,还是会出现这个提示。于是搜索是否已经安装过别的C++插件,果然,找到

卸载。装回C++ Extension Pack,解决。


References:

https://code.visualstudio.com...官网中配置页传送门

环境配置

1 准备

  • vscode 软件
  • C/C++ Extension Pack 插件

  • clang/clang++ 编译器

    • 检查已安装clang++

      clang++ -v
    • 如果未安装,请前往 app store 下载 xcode

2 .vscode配置

在当前工作区准备以下文件(夹)

  • .vscode

    • tasks.json # 用于编译c++文件
    • launch.json # 用于使用vscode自带的debug工具(左侧的小虫图标)
    • c_cpp_properties.json # 用于使用vscode自带的代码提示工具如 IntelliSense
  • main.cpp

     #include <iostream>
     #include <vector>
     #include <string>
     
     using namespace std;
     
     int main()
     {
      vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};
     
      for (const string& word : msg)
      {
      cout << word << " ";
      }
      cout << endl;
     }

配置tasks.json

command + shift + b,vscode会执行tasks.json中的任务。

本文配置的是c++编译,替换tasks.json的内容如下:

 {
  "version": "2.0.0",
  "tasks": [
  {
  "label": "Build with Clang", //这个任务的名字在launch.json最后一项配置
  "type": "shell",
  "command": "clang++",
  "args": [
  "-std=c++17",
  "-stdlib=libc++",
  "-g",
  // 生成调试信息,GUN可使用该参数
  "${file}",
  // file指正在打开的文件
  "-o",
  // 生成可执行文件
  "${fileDirname}/${fileBasenameNoExtension}"
  // fileDirname指正在打开的文件所在的文件夹
  // fileBasenammeNoExtension指没有扩展名的文件,unix中可执行文件属于此类
  ],
  "options": {
  "cwd": "${workspaceFolder}"
  },
  "problemMatcher": ["$gcc"],
  "group": {
  "kind": "build",
  "isDefault": true
  }
  }
  ]
 }

打开main.cpp,按command + shift + b(注意,按这组快捷键时main.cpp文件在vscode中是正在打开的),如下图

工作区中没有扩展名的main就是在mac os下生成的可执行文件(同理,如果是windows,有扩展名.exe)

在vscode内置的终端中,可以运行main,如下图:

配置 c_cpp_properties.json

c_cpp_properties.json 的作用是:代码提示、代码跳转等

 {
  "configurations": [
  {
  "name": "Mac",
  "includePath": [
  "${workspaceFolder}/**",
  "/Library/Developer/CommandLineTools/usr/include",
  "/Library/Developer/CommandLineTools/usr/lib/clang/11.0.3/include",
  // 请根据你的clang版本修改,我这里是11.0.3
  "/usr/local/include"
  ],
  "defines": [],
  "macFrameworkPath": [
  "/System/Library/Frameworks",
  "/Library/Frameworks"
  ],
  "compilerPath": "/usr/bin/clang++",
  "cStandard": "c11",
  "cppStandard": "c++17",
  "intelliSenseMode": "clang-x64"
  // "compileCommands": "${workspaceFolder}/build/compile_commands.json"
  }
  ],
  "version": 4
 }

配置launch.json

launch.json是调用vscode debug功能的配置文件

 {
  "version": "0.2.0",
  "configurations": [
  {
  "type": "lldb",
  "request": "launch",
  "name": "Debug",
  "program": "${workspaceFolder}/${fileBasenameNoExtension}",
  "args": [],
  "cwd": "${workspaceFolder}",
  "preLaunchTask": "Build with Clang"
  }
  ]
 }

打开debug窗口,打好断点,按下图操作

这三个配置文件中的参数说明,在官网都有,我把一部分写在了注释里

踩坑

1. c_cpp_properties.json设置的cppstander“失效”了

结论:检查vscode是否安装了C/C++ Clang Command Adapter,有的话就删除

原因应该是这个插件不继承c_cpp_properties.json中设置的C++版本

我在翻官方文档之前,看到的配置博客说需要下载C/C++ Clang Command Adapter插件,实际上这个插件并非按C++11版本给出错误提示,出现如下图的状况

image-20210205195759837

C++11标准中,列表初始化 vector对象,在这里有报错提示,而且提示中没有标明是哪个插件提供的。难道vscode自身有C++语法提示,而且版本较老?当然,这个猜想是错的。

我尝试把C++ Extension Pack卸载,还是会出现这个提示。于是搜索是否已经安装过别的C++插件,果然,找到

image-20210205200107290

卸载。装回C++ Extension Pack,解决。


References:

https://code.visualstudio.com/docs/cpp/config-clang-mac#_prerequisites


CregskiN
143 声望5 粉丝

焦虑驱动型学习者,喜欢 react 和 node