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 终端
  • 执行以下命令
bash 复制代码
# 更新软件库
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中增加对第三方库的引用

json 复制代码
{
    "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是必须的, 路径中的/可以替换为\\

json 复制代码
{
    "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文件

json 复制代码
    "-g",
    "${fileDirname}/line_detection.cpp",
    "${fileDirname}/utils.cpp",
    //"${fileDirname}/*.cpp",
    "-o",
    "${fileDirname}/build/${fileBasenameNoExtension}.exe",

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

json 复制代码
    "-I","D:/msys64/ucrt64/include/opencv4",
    "-lopencv_imgcodecs",
    "-lopencv_core"

launch.json

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

json 复制代码
{
    "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

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

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

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",
            ]
        }
    ]
}
相关推荐
就是ping不通的蛋黄派5 小时前
Win版 Visual Studio Code配置C++环境
c++·vscode
CAU界编程小白5 小时前
数据结构系列之链表
数据结构·c++·链表
天將明°5 小时前
状态模式指南:对象状态变化的优雅管理
c语言·架构·设计规范
深思慎考6 小时前
LinuxC++项目开发日志——基于正倒排索引的boost搜索引擎(3——通过cppjieba库建立索引模块)
linux·c++·搜索引擎
卷Java6 小时前
百度智能云车牌识别API官方配置指南
java·开发语言·经验分享·vscode·学习·微信小程序·intellij idea
用户6120414922136 小时前
C语言做的迷宫生成与求解程序
c语言·敏捷开发·计算机图形学
Onesoft%J1ao6 小时前
C++ 1.STL-vector 2.STL-list 3.数组模拟单向链表 详解配例题 通俗易懂
c++·青少年编程·list
_OP_CHEN6 小时前
数据结构(C语言篇):(十六)插入排序
c语言·数据结构·排序算法·学习笔记·插入排序·希尔排序·直接插入排序
Laughtin6 小时前
dolphindb vscode更改连接配置的操作步骤
ide·vscode·编辑器