C/C++ Development using Visual Studio Code, CMake and LLDB

时间:2022-05-07
本文章向大家介绍C/C++ Development using Visual Studio Code, CMake and LLDB,主要内容包括概述、开发环境搭建、效果、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

概述

由于我工作环境是Linux和Mac,个人的工作目录和开发环境一直来回切换,之前一直使用emacs。不可否认,emacs非常强大和可定制化。昨天由于个人电脑系统损坏,重装了osx。在好基友的推荐下,试用vscode,发现非常不错。于是记录和分享到博客中。今天主要给大家讲解的vscode配置c/c++ ide开发环境,当然官网支持很多种可定制化的配置。

开发环境搭建

我们首先安装vscode,官网是:vscode。我们下面来安装支持c/c++开发环境的安装包。

  • cpptools
  • cmake
  • C/C++ Clang

我们在工作目录依次安装如下,c_cpp_properties.json(指定c/c++包和平台相关的配置文件),launch.json(debug 调试),tasks.json(你的编译等命令)

c_cpp_propertie.json

{
  "configurations": [
    {
      "name": "Mac",
      "includePath": [
        "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1",
        "/usr/local/include",
        "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/9.0.0/include",
        "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include",
        "/usr/include",
        "${workspaceRoot}"
      ],
      "defines": [],
      "intelliSenseMode": "clang-x64",
      "browse": {
        "path": [
          "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1",
          "/usr/local/include",
          "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/9.0.0/include",
          "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include",
          "/usr/include",
          "${workspaceRoot}"
        ],
        "limitSymbolsToIncludedHeaders": true,
        "databaseFilename": ""
      },
      "macFrameworkPath": [
        "/System/Library/Frameworks",
        "/Library/Frameworks"
      ]
    },
    {
      "name": "Linux",
      "includePath": [
        "/usr/include",
        "/usr/local/include",
        "${workspaceRoot}"
      ],
      "defines": [],
      "intelliSenseMode": "clang-x64",
      "browse": {
        "path": [
          "/usr/include",
          "/usr/local/include",
          "${workspaceRoot}"
        ],
        "limitSymbolsToIncludedHeaders": true,
        "databaseFilename": ""
      }
    },
    {
      "name": "Win32",
      "includePath": [
        "C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/include",
        "${workspaceRoot}"
      ],
      "defines": [
        "_DEBUG",
        "UNICODE",
        "_UNICODE"
      ],
      "intelliSenseMode": "msvc-x64",
      "browse": {
        "path": [
          "C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/include/*",
          "${workspaceRoot}"
        ],
        "limitSymbolsToIncludedHeaders": true,
        "databaseFilename": ""
      }
    }
  ],
  "version": 3
}

我们再看一下下面的debug下的json数据。

{
  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [
    {
      "name": "(lldb) Launch",
      "type": "cppdbg",
      "request": "launch",
      "program": "${workspaceFolder}/build/DemoProject",
      "args": [],
      "stopAtEntry": false,
      "cwd": "${workspaceFolder}/build",
      "environment": [],
      "externalConsole": false,
      "MIMode": "lldb"
    }
  ]
}

最后一个是tasks.json

{
  // See https://go.microsoft.com/fwlink/?LinkId=733558
  // for the documentation about the tasks.json format
  "version": "2.0.0",
  "echoCommand": true,
  "options": {
    "cwd": "${workspaceRoot}/build"
  },
  "tasks": [
    {
      "label": "cmake",
      "type": "shell",
      "command": "cmake -G 'Unix Makefiles' -DCMAKE_BUILD_TYPE=Debug ..",
      "group": {
        "kind": "build",
        "isDefault": true
      }
    },
    {
      "label": "make",
      "type": "shell",
      "command": "make -j 8",
      "group": {
        "kind": "build",
        "isDefault": true
      }
    }
  ]
}

效果