找回密码
 立即注册
首页 业界区 安全 VsCode C++ 首次运行代码编译

VsCode C++ 首次运行代码编译

站竣凰 6 天前
VsCode C++环境的搭建,此处不叙述。本文是在环境配置后,实现代码编译,控制台输出。
1.区分gcc和g++
之前使用没有区分两者区分,使用默认gcc导致,在编译c++的时候,报错运行失败,提示json扩展等问题。后来查找后,才清楚gcc主要是处理c代码,g++才是c++.
特性gccg++全称GNU Compiler Collection (C 编译器驱动程序)GNU C++ Compiler (C++ 编译器驱动程序)主要用途默认处理 C 代码默认处理 C++ 代码链接行为默认不自动链接 C++ 标准库自动链接 C++ 标准库 (如 libstdc++)文件后缀识别优先识别 .c 为 C 代码优先识别 .cpp/.cc 为 C++ 代码2.相关json配置

  • c_cpp_properties.json
    按下 Ctrl+Shift+P,输入 C/C++: Edit Configurations (UI)。
    自动生成 .vscode/c_cpp_properties.json
  1. {
  2.     "configurations": [
  3.         {
  4.             "name": "Win32",
  5.             "includePath": [
  6.                 "${workspaceFolder}/**"
  7.             ],
  8.             "defines": [
  9.                 "_DEBUG",
  10.                 "UNICODE",
  11.                 "_UNICODE"
  12.             ],
  13.             "compilerPath": "C:/Program Files/mingw64/bin/g++.exe",
  14.             "cStandard": "c17",
  15.             "cppStandard": "c++17",
  16.             "intelliSenseMode": "windows-gcc-x64"
  17.         }
  18.     ],
  19.     "version": 4
  20. }
复制代码

  • tasks.json(编译配置)
    按下 Ctrl+Shift+P,输入 Tasks: Configure Default Build Task → 选择 g++.exe。
    修改生成的 .vscode/tasks.json:
  1.   {
  2.     "version": "2.0.0",
  3.     "tasks": [
  4.         {
  5.             "label": "Build HelloWorld", // 必须与 launch.json 中的 preLaunchTask 一致
  6.             "type": "shell",
  7.             "command": "C:/Program Files/mingw64/bin/g++.exe",
  8.             "args": [
  9.                 "-fdiagnostics-color=always",
  10.                 "-g",
  11.                 "${file}",
  12.                 "-o",
  13.                 "${fileDirname}/${fileBasenameNoExtension}.exe" // 修复1:路径分隔符改为 `/`
  14.             ],
  15.             "options": {
  16.                 "cwd": "${workspaceFolder}" // 修复2:工作目录设为项目根目录
  17.             },
  18.             "problemMatcher": [
  19.                 "$gcc"
  20.             ],
  21.             "group": {
  22.                 "kind": "build",
  23.                 "isDefault": true
  24.             },
  25.             "detail": "调试器生成的任务。"
  26.         }
  27.     ]
  28.   }
复制代码

  • launch.json(调试配置)
    点击左侧边栏的 Run and Debug 图标(或按 Ctrl+Shift+D)。
    点击顶部下拉菜单中的 create a launch.json file(若首次配置)
    在弹出的选项中选择 C++ (GDB/LLDB)。
  1.   {
  2.       // 使用 IntelliSense 了解相关属性。
  3.       // 悬停以查看现有属性的描述。
  4.       // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
  5.       "version": "0.2.0",
  6.       "configurations": [
  7.           {
  8.               "name": "(gdb) 启动",// 自定义调试名称
  9.               "type": "cppdbg",
  10.               "request": "launch",
  11.               "program": "${fileDirname}/${fileBasenameNoExtension}.exe",// 路径分隔符改为 `/`
  12.               "args": [],
  13.               "stopAtEntry": false,
  14.               "cwd": "${workspaceFolder}",
  15.               "environment": [],
  16.               "externalConsole": true, // Windows 推荐 true(显示控制台输出)
  17.               "MIMode": "gdb",
  18.               "miDebuggerPath": "C:/Program Files/mingw64/bin/gdb.exe",  // 检查路径是否正确
  19.               "preLaunchTask": "Build HelloWorld" // 必须与 tasks.json 中的 label 一致
  20.           }
  21.       ]
  22.   }
复制代码
注意:vscode自动生成的模板后,有些配置需要手动修改
3.执行运行调试
1.png


来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册