C++环境配置

本流程主要参考:官方文档

配置 tasks.json

task.json 的主要作用 配置 编译 的相关参数

配置方式:

  • user setting:shfit+commond+p => >Tasks:Open User Tasks
  • workspace setting:shfit+commond+p => Tasks:Configure Task 前提,没有设置 user

具体的配置文件如下,包含了第三方库的设置

  1. {
  2. // See https://go.microsoft.com/fwlink/?LinkId=733558
  3. // for the documentation about the tasks.json format
  4. "version": "2.0.0",
  5. "tasks": [
  6. {
  7. "type": "shell",
  8. "label": "clang++ build active file",
  9. "command": "/usr/bin/clang++",
  10. "args": [
  11. "-std=c++17",
  12. "-stdlib=libc++",
  13. "-g",
  14. "${workspaceFolder}/*.cpp",
  15. "-o",
  16. "${fileDirname}/${fileBasenameNoExtension}",
  17. "-I", // 设置 include 头文件路径
  18. "${workspaceFolder}/include",
  19. "-L", // 设置 lib 库路径
  20. "${workspaceFolder}/library",
  21. "-l", // 设置 liblua.a 静态库
  22. "lua"
  23. ],
  24. "options": {
  25. "cwd": "${workspaceFolder}"
  26. },
  27. "problemMatcher": [
  28. "$gcc"
  29. ],
  30. "group": {
  31. "kind": "build",
  32. "isDefault": true
  33. }
  34. }
  35. ]
  36. }

配置 launch.json

launch.json 主要作用 配置 调试的相关参数

配置方式:

  • user setting:
    shfit+commond+p => Preferences:Open User Settings 配置 launch```json “launch”: {

    1. "version": "0.2.0",
    2. "configurations": [
    3. {
    4. "name": "clang++ - user",
    5. "type": "cppdbg",
    6. "request": "launch",
    7. "program": "${fileDirname}/${fileBasenameNoExtension}",
    8. "args": [],
    9. "stopAtEntry": true,
    10. "cwd": "${workspaceFolder}",
    11. "environment": [],
    12. "externalConsole": false,
    13. "MIMode": "lldb",
    14. "preLaunchTask": "clang++ build active file" // 先通过 Task 编译成可执行二进制文件再调试
    15. },
    16. ]

    }, ```

  • workspace setting:
    直接在 debug 页签下添加单独的 launch.jsonvscode环境配置 - 图1

配置第三方库代码提示

配置方式:

  • user setting :
    shfit+commond+p => Preferences:Open User Settingsvscode环境配置 - 图2json // 第三方库头文件位置设置 "C_Cpp.default.includePath": [ "${workspaceFolder}/include" ],

  • workspace setting :
    配置文件 c_cpp_properties.json
    shfit+commond+p => C/C++: Edit Configurations (UI)
    设置 includePath 属性

  • 补充 如果安装了 插件 C Clang Commond Adapter ,需要再 setting 中配置第三方库头文件的位置json // C Clang Commond Adapter 插件设置第三方库头文件位置 "clang.cflags": [ "-I", "${workspaceRoot}/include" ], "clang.cxxflags": [ "-I", "${workspaceRoot}/include" ]

扩展

Cmake 工具可以取代 tasks.json 步骤