当前位置: 首页 > news >正文

VSCode C/C++ 开发环境配置

VSCode 安装插件

  • C/C++ Extension Pack
    • C/C++
    • C/C++ Themes
    • CMake Tools
  • Makefile Tools
  • CMake

Windows下的环境配置

安装 MSYS2 和 GCC工具链

  • 下载MSYS2安装文件 https://www.msys2.org/
  • 安装到D盘, D:\msys64
  • 将 UCRT64 路径添加到 Windows: D:\msys64\ucrt64\bin\
  • 启动 UCRT64 终端
  • 执行以下命令
# 更新软件库
pacman -Syu
# 安装工具链, 根据提示, 安装需要的 gcc, gdb等
pacman -S --needed base-devel mingw-w64-ucrt-x86_64-toolchain
# 安装opencv
pacman -S mingw-w64-ucrt-x86_64-opencv

VSCode配置

在 Windows 路径中增加 UCRT64 路径后, VSCode 会自动检测到 gcc.exe 的存在, 此时可以

  • 编译当前编辑的C/CPP文件
    • Alt+Shift+F10: 唤出菜单中选择"C/C++ gcc.exe build active file", 注意看下面的明细显示的是否是D:\msys64\ucrt64\bin\gcc.exe, 如果是cpp文件, 选择带g++.exe的选项
    • 如果只是运行标准任务, 不会创建 tasks.json
  • 创建 tasks.json
    • Ctrl+Shift+P: 唤出菜单,输入Task, 选择Tasks: Configure Task, 然后选择"C/C++ gcc.exe build active file"
    • 通过 tasks.json, 可以定制编译选项

启动debug

  • 按 F5, 选择"C/C++ gcc.exe build and debug active file"
  • 默认debug不会创建 launch.json, 如果要创建 launch.json, 在侧栏 Run and Debug 中, 点击 create a launch.json file , 再选择 C++(GDB/LLDB), 在弹出的列表中, 选择 GDB launch, 并在生成的 launch.json 模板中, 输入gdb.exe 路径等参数

配置文件示例

c_cpp_properties.json

compilerPath指向UCRT64编译器, 在includePath中增加对第三方库的引用

{"configurations": [{"name": "Win32","includePath": ["${workspaceFolder}/**","D:/msys64/ucrt64/include/opencv4"],"defines": ["_DEBUG","UNICODE","_UNICODE"],"windowsSdkVersion": "10.0.19041.0","compilerPath": "D:/msys64/ucrt64/bin/g++.exe","cStandard": "c11","cppStandard": "c++11","intelliSenseMode": "gcc-x64"}],"version": 4
}

tasks.json

options是必须的, 路径中的/可以替换为\\

{"version": "2.0.0","tasks": [{"type": "cppbuild","label": "build basic cpp file","command": "D:/msys64/ucrt64/bin/g++.exe","args": ["-fdiagnostics-color=always","-g","${file}","-o","${fileDirname}/${fileBasenameNoExtension}.exe"],"options": {"cwd": "D:/msys64/ucrt64/bin"},"problemMatcher": ["$gcc"],"group": "build","detail": "compiler: D:/msys64/ucrt64/bin/g++.exe"},{"type": "cppbuild","label": "build basic c file","command": "D:/msys64/ucrt64/bin/gcc.exe","args": ["-fdiagnostics-color=always","-g","${file}","-o","${fileDirname}/${fileBasenameNoExtension}.exe"],"options": {"cwd": "D:/msys64/ucrt64/bin"},"problemMatcher": ["$gcc"],"group": "build","detail": "compiler: D:/msys64/ucrt64/bin/gcc.exe"}]
}

如果要编译多个文件, 可以在 args 中指定, 或者用通配符"${fileDirname}/*.cpp"指定所有cpp文件

    "-g","${fileDirname}/line_detection.cpp","${fileDirname}/utils.cpp",//"${fileDirname}/*.cpp","-o","${fileDirname}/build/${fileBasenameNoExtension}.exe",

如果要添加 opencv 库, 需要在args中增加引用

    "-I","D:/msys64/ucrt64/include/opencv4","-lopencv_imgcodecs","-lopencv_core"

launch.json

program指向编译结果, 可以用preLaunchTask指定在debug之前执行编译任务

{"version": "0.2.0","configurations": [{"name": "(gdb) Launch","type": "cppdbg","request": "launch","program": "${workspaceFolder}/${fileBasenameNoExtension}.exe","args": [],"stopAtEntry": false,"cwd": "${fileDirname}","environment": [],"externalConsole": false,"MIMode": "gdb","miDebuggerPath": "D:/msys64/ucrt64/bin/gdb.exe","setupCommands": [{"description": "Enable pretty-printing for gdb","text": "-enable-pretty-printing","ignoreFailures": true},{"description": "Set Disassembly Flavor to Intel","text": "-gdb-set disassembly-flavor intel","ignoreFailures": true}]}]
}

Ubuntu下的环境配置

Ubuntu下因为系统内建了GCC工具链, 如果对版本没有要求直接用默认的就可以

配置文件示例

c_cpp_properties.json

{"configurations": [{"name": "Linux","includePath": ["${workspaceFolder}/**","/usr/include/c++/11","/usr/include/x86_64-linux-gnu/c++/11","/usr/include/opencv4"],"defines": [],"compilerPath": "/usr/bin/gcc","cStandard": "gnu11","cppStandard": "gnu++11","intelliSenseMode": "linux-gcc-x64"}],"version": 4
}

launch.json

{"version": "0.2.0","configurations": [{"name": "C/C++ debug active file","type": "cppdbg","request": "launch","program": "${fileDirname}/build/${fileBasenameNoExtension}","stopAtEntry": true,"cwd": "${fileDirname}","externalConsole": false,"MIMode": "gdb","preLaunchTask": "build active file","miDebuggerPath": "/usr/bin/gdb"}]
}

tasks.json

{// See https://go.microsoft.com/fwlink/?LinkId=733558// for the documentation about the tasks.json format"version": "2.0.0","tasks": [{"type": "cppbuild","label": "build active file","command": "/usr/bin/g++","args": ["-g","${file}","-o","${fileDirname}/build/${fileBasenameNoExtension}","-I", "/usr/include/opencv4","-l", "opencv_core","-l", "opencv_imgcodecs","-l", "opencv_videoio","-l", "opencv_imgproc","-l", "opencv_photo","-l", "opencv_xphoto",]}]
}
http://www.dtcms.com/a/398536.html

相关文章:

  • FPGA自学笔记--VIVADO RAM IP核控制和使用
  • 电源——设计DCDC原理图与参数选型
  • 企业网站建设策划书 前言263云通信官方网站
  • pip config list输出为空?如何配置pip镜像源?不同方式配置有什么区别?
  • 表格工具怎么选,国产化替代方案测评(2025 全维度实测版)
  • 分布式 ID 生成方案实战指南:从选型到落地的全场景避坑手册(二)
  • 企业网站建设案例宝安三网合一网站建设
  • 做透水砖的网站vs2019可以做网站吗
  • 鸿蒙后台定时任务实战
  • 【win32】ffmpeg 解码器2
  • MCU知识体系
  • 【win32】ffmpeg 解码器
  • 东莞市官网网站建设公司中企动力z邮箱登录入口
  • wordpress网站seo罗夫曼三大社区模式
  • 搭建一个属于自己的mac摄像头视频流rtsp服务
  • Spring Boot 集成 RabbitMQ 实现可靠消息传递:从配置到实战
  • Linux学习记录--多线程共享变量
  • 网站格式有哪些内容私人建设手机网站
  • 【Java后端】SpringBoot 常用工具类和工具方法汇总
  • leetcode hot100 中等难度 day03-刷题
  • Android | 使用 dumpsys alarm 验证自己应用使用的 Alarm 是否正确
  • React 展示Markdown内容
  • 营销型网站标准网页源码江西旺达建设工程有限公司网站
  • 南昌网站建设公司咨询交通局网站建设方案策划书
  • 阅读:Agent AI:Surveying the Horizons of Multimodal Interaction (2.2.1-2.2.3)
  • 提升网站建设品质福建省建设厅网站林瑞良
  • 阿里云网站建设服务费会计科目农产品网站建设投标书
  • 「企业模糊查询搜索api接口」详细介绍及调用使用方法
  • 【一天一个Web3概念】深入解析Web3空投:类型、参与策略与安全指南
  • JS逆向-Sign签名绕过技术算法可逆替换库模拟发包堆栈定位特征搜索安全影响