Windows 下搭建 MSYS2+CMake+VSCode+gdb 编译调试环境

windows 下部署 vscode 的 c++ 调试环境

给raspberrylive项目写的,可能有多余步骤

安装msys2

官网下载安装包

中国大陆地区去清华镜像下载安装包和配置软件源:MSYS2 镜像使用帮助

环境变量

根据自己的安装路径修改

MSYS_DIR=C:\msys64
MINGW64_DIR=C:\msys64\mingw64
Path添加: %MSYS_DIR%\usr\bin;%MINGW64_DIR%\bin

源改成国内的

参考https://mirrors.tuna.tsinghua.edu.cn/help/msys2/

装点东西

打开msys2终端

pacman -Syu
pacman -S mingw-w64-x86_64-gcc mingw-w64-x86_64-gdb mingw-w64-x86_64-cmake mingw-w64-x86_64-make
pacman -S mingw-w64-x86_64-toolchain
pacman -S mingw-w64-x86_64-clang
pacman -S mingw-w64-x86_64-yasm mingw-w64-x86_64-nasm
pacman -S mingw-w64-x86_64-freetype
#下面几条命令根据需求来,装自己想要的
pacman -S mingw-w64-x86_64-opencv
pacman -S mingw-w64-x86_64-ffmpeg mingw-w64-x86_64-ffms2
pacman -S mingw-w64-x86_64-libwebsockets

vscode

右击项目文件夹,使用vscode打开,或者文件打开文件夹打开项目

CMake Tools拓展

按下Ctrl+Shift+P,输入并选择C/C++: Edit Configuration

把文件内容改为如下,路径根据你msys2安装位置来

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                "C:/msys64/mingw64/include"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "C:/msys64/mingw64/bin/gcc.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}

按下Ctrl+Shift+P,输入setting.json,选择工作区设置,把文件内容改为如下,路径根据你msys2安装位置来

{
    "cmake.cmakePath": "C:/msys64/mingw64/bin/cmake.exe",
    "cmake.configureOnOpen": true,
    "cmake.generator": "MSYS Makefiles",
    "C_Cpp.default.configurationProvider": "vector-of-bool.cmake-tools"
}

重启vscode,会自动启动cmake(如果没有,按下Ctrl+Shift+P,输入CMake: configure

编译器要选mingw64/bin/下的gcc

[variant] 已加载一组新变量
[kit] 已成功从 C:\Users\chenx\AppData\Local\CMakeTools\cmake-tools-kits.json 加载 9 工具包
[main] 正在配置文件夹: raspberrylive
[proc] 执行命令: D:/tools/msys2/mingw64/bin/cmake.exe --no-warn-unused-cli -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_C_COMPILER:FILEPATH=D:\tools\msys2\mingw64\bin\gcc.exe -DCMAKE_CXX_COMPILER:FILEPATH=D:\tools\msys2\mingw64\bin\g++.exe -Hd:/projects/git/raspberrylive -Bd:/projects/git/raspberrylive/build -G "MSYS Makefiles"
[cmake] Not searching for unused variables given on the command line.
[cmake] -- The C compiler identification is GNU 10.2.0
[cmake] -- The CXX compiler identification is GNU 10.2.0
[cmake] -- Check for working C compiler: D:/tools/msys2/mingw64/bin/gcc.exe
[cmake] -- Check for working C compiler: D:/tools/msys2/mingw64/bin/gcc.exe - works
[cmake] -- Detecting C compiler ABI info
[cmake] -- Detecting C compiler ABI info - done
[cmake] -- Detecting C compile features
[cmake] -- Detecting C compile features - done
[cmake] -- Check for working CXX compiler: D:/tools/msys2/mingw64/bin/g++.exe
[cmake] -- Check for working CXX compiler: D:/tools/msys2/mingw64/bin/g++.exe - works
[cmake] -- Detecting CXX compiler ABI info
[cmake] -- Detecting CXX compiler ABI info - done
[cmake] -- Detecting CXX compile features
[cmake] -- Detecting CXX compile features - done
[cmake] -- Found OpenCV: D:/tools/msys2/mingw64 (found version "4.3.0")
[cmake] -- Found Freetype: D:/tools/msys2/mingw64/lib/libfreetype.dll.a (found version "2.10.2")
[cmake] -- Configuring done
[cmake] -- Generating done

最后就能在底部状态栏上点击编译和debug按钮了

遇到的问题

cmake一直提示找不到库,cmake始终要用/usr/bin/gcc.exe而不用/mingw64/bin/gcc.exe

更改下%AppData%\..\Local\CMakeTools\cmake-tools-kits.json的内容,cmake会自动刷新

重新选上/mingw64/bin下面的gcc即可

生成后的结果参考这个(路径按自己的来)

[
  {
    "name": "GCC 10.2.0",
    "compilers": {
      "C": "C:\\msys64\\mingw64\\bin\\gcc.exe",
      "CXX": "C:\\msys64\\mingw64\\bin\\g++.exe"
    }
  },
  {
    "name": "GCC 9.3.0",
    "compilers": {
      "C": "C:\\msys64\\usr\\bin\\gcc.exe",
      "CXX": "C:\\msys64\\usr\\bin\\g++.exe"
    }
  },
  {
    "name": "GCC for x86_64-pc-msys 9.3.0",
    "compilers": {
      "C": "C:\\msys64\\usr\\bin\\x86_64-pc-msys-gcc.exe",
      "CXX": "C:\\msys64\\usr\\bin\\x86_64-pc-msys-g++.exe"
    }
  },
  {
    "name": "GCC for x86_64-w64-mingw32 10.2.0",
    "compilers": {
      "C": "C:\\msys64\\mingw64\\bin\\x86_64-w64-mingw32-gcc.exe",
      "CXX": "C:\\msys64\\mingw64\\bin\\x86_64-w64-mingw32-g++.exe"
    }
  }
]

运行raspberrylive.exe显示无法定位程序输入点inflateReset2于动态链接库

path环境变量里的优先级问题

先用下面的命令查一下dll:

C:\Users\chenx>where zlib1.dll
C:\MentorGraphics\9.5PADS\SDD_HOME\common\win32\lib\zlib1.dll
C:\Program Files\Intel\WiFi\bin\zlib1.dll
D:\tools\msys2\mingw64\bin\zlib1.dll

可以看到优先级顺序有问题

把另两个路径改到%MINGW64_DIR%\bin后面就行了

1 Comment

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注