关于c++:mac-vscode-c-环境配置编译debug

5次阅读

共计 2101 个字符,预计需要花费 6 分钟才能阅读完成。

官网中对应的页面如下

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 窗口,打好断点,按下图操作

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


References:

https://code.visualstudio.com…

正文完
 0