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

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

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",]}]
}