提示信息

最新 VS Code 配置 C/C++ 环境的全指南(包含 tasks.json、launch.json、c_cpp_properties.json 文件配置)

旧夏天 已关注

要在 VS Code 中配置 C/C++ 环境,需要设置好几个不同的配置文件,包括 tasks.jsonlaunch.jsonc_cpp_properties.json。以下是最新的配置指南:

前置条件

  1. 安装 VS Code:确保你已经安装了 Visual Studio Code。
  2. 安装 C/C++ 扩展:在 VS Code 中,通过扩展市场安装官方的 C/C++ 扩展(由 Microsoft 提供)。
  3. 安装编译器:确保在你的环境中有可用的 C/C++ 编译器,如 GCC、Clang 或 MSVC。

工作空间结构

在你的项目文件夹中,通常需要以下目录结构:

  1. your_project/
  2. │ main.cpp
  3. └── .vscode/
  4. ├── tasks.json
  5. ├── launch.json
  6. └── c_cpp_properties.json

配置文件

1. tasks.json

tasks.json 用于定义编译任务。它会告诉 VS Code 如何调用编译器来编译你的代码。

  1. {
  2. "version": "2.0.0",
  3. "tasks": [
  4. {
  5. "label": "build",
  6. "type": "shell",
  7. "command": "g++",
  8. "args": [
  9. "-g",
  10. "${workspaceFolder}/*.cpp",
  11. "-o",
  12. "${workspaceFolder}/out.exe"
  13. ],
  14. "group": {
  15. "kind": "build",
  16. "isDefault": true
  17. },
  18. "problemMatcher": ["$gcc"]
  19. }
  20. ]
  21. }
  • command: 设置使用的编译器命令,例如 g++
  • args: 编译选项和输入/输出文件参数。
  • problemMatcher: 用于解析编译器输出,并在 VS Code 的问题面板中显示错误。

2. launch.json

launch.json 用于设置调试配置。

  1. {
  2. "version": "0.2.0",
  3. "configurations": [
  4. {
  5. "name": "(gdb) Launch",
  6. "type": "cppdbg",
  7. "request": "launch",
  8. "program": "${workspaceFolder}/out.exe",
  9. "args": [],
  10. "stopAtEntry": false,
  11. "cwd": "${workspaceFolder}",
  12. "environment": [],
  13. "externalConsole": true,
  14. "MIMode": "gdb",
  15. "setupCommands": [
  16. {
  17. "description": "Enable pretty-printing for gdb",
  18. "text": "-enable-pretty-printing",
  19. "ignoreFailures": true
  20. }
  21. ],
  22. "preLaunchTask": "build",
  23. "miDebuggerPath": "/usr/bin/gdb",
  24. "logging": {
  25. "trace": true,
  26. "traceResponse": true,
  27. "engineLogging": true,
  28. "programOutput": true,
  29. "exceptions": true
  30. }
  31. }
  32. ]
  33. }
  • program: 需要调试的程序路径。
  • preLaunchTask: 调试前要完成的任务,一般指编译任务。
  • miDebuggerPath: GNU Debugger 安装路径,Windows 下使用 gdb.exe,Mac和Linux 为 gdb

3. c_cpp_properties.json

c_cpp_properties.json 用于配置包括 IntelliSense 和其他相关的 C/C++ 编译环境信息。

  1. {
  2. "configurations": [
  3. {
  4. "name": "Win32",
  5. "includePath": [
  6. "${workspaceFolder}/**"
  7. ],
  8. "defines": [],
  9. "compilerPath": "C:/path/to/gcc.exe",
  10. "cStandard": "c17",
  11. "cppStandard": "c++17",
  12. "intelliSenseMode": "windows-gcc-x64"
  13. }
  14. ],
  15. "version": 4
  16. }
  • includePath: 包含路径,搜索 C/C++ 头文件时使用。
  • compilerPath: 编译器的完整路径。
  • cStandardcppStandard: 设置 C/C++ 版本标准。
  • intelliSenseMode: 配置 IntelliSense 模式。

编译和调试

  1. 编译:使用 Ctrl+Shift+B 运行已配置的编译任务。
  2. 调试:使用 F5 启动调试,会执行 launch.json 的配置。

通过这个指南,你可以在 VS Code 中设置和使用一个功能齐全的 C/C++ 开发环境。如有需要,你可以根据操作系统和自己的需求调整相应的路径及选项。

    遇到难题? "AI大模型GPT4.0、GPT" 是你的私人解答专家! 点击按钮去提问......
旧夏天 关注 已关注

最近一次登录:2024-11-20 21:28:29   

暂时还没有签名,请关注我或评论我的文章
×
免费图表工具,画流程图、架构图