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",
            ]
        }
    ]
}
相关推荐
十五年专注C++开发18 小时前
C++类型转换通用接口设计实现
开发语言·c++·跨平台·类设计
胡萝卜3.018 小时前
掌握string类:从基础到实战
c++·学习·string·string的使用
江公望19 小时前
通过QQmlExtensionPlugin进行Qt QML插件开发
c++·qt·qml
Syntech_Wuz19 小时前
从 C 到 C++:容器适配器 std::stack 与 std::queue 详解
数据结构·c++·容器··队列
艾莉丝努力练剑20 小时前
【C++STL :stack && queue (一) 】STL:stack与queue全解析|深入使用(附高频算法题详解)
linux·开发语言·数据结构·c++·算法
胡萝卜3.020 小时前
深入理解string底层:手写高效字符串类
开发语言·c++·学习·学习笔记·string类·string模拟实现
kyle~20 小时前
计算机系统---CPU的进程与线程处理
linux·服务器·c语言·c++·操作系统·计算机系统
只是懒得想了21 小时前
用C++实现一个高效可扩展的行为树(Behavior Tree)框架
java·开发语言·c++·design-patterns
bkspiderx21 小时前
C++设计模式之行为型模式:模板方法模式(Template Method)
c++·设计模式·模板方法模式
我是华为OD~HR~栗栗呀21 小时前
华为OD-23届考研-Java面经
java·c++·后端·python·华为od·华为·面试