解決調試報錯
前面win11+vscode搭建的c/c++環境,ctrl+shift+B生成正常,cttl+F5運行正常。今天打斷點逐步調試時報錯,提示找不到庫文件。解決方案如下:
- 下載mingw-w64源碼庫:(兩種途徑)
- 通過MSYS2 UCRT64終端下載
pacman -S git # 安裝git
git clone https://git.code.sf.net/p/mingw-w64/mingw-w64 # 下載mingw-w64
下載好的文件在C:\msys64\home\你的用戶名\)目錄下。 - 手動下載源碼(如果不想安裝 Git)
如果不想安裝 Git,可以直接從瀏覽器下載源碼:訪問 MinGW-w64 的官方源碼倉庫:
https://sourceforge.net/p/mingw-w64/mingw-w64/
點擊 “Download Snapshot” 下載壓縮包(如 .zip 或 .tar.gz)
- 通過MSYS2 UCRT64終端下載
- 得到mingw-w64源碼包后,在MSYS2安裝目錄下新建src文件夾,C:\msys64\ucrt64\src,把mingw-w64放到src文件夾中,最終目錄C:\msys64\ucrt64\src\mingw-w64
- 修改lauch.json
{
“version”: “0.2.0”,
“configurations”: [
{
“name”: “gcc.exe - Build and debug active file”,
“type”: “cppdbg”,
“request”: “launch”,
“program”: “${fileDirname}\ f i l e B a s e n a m e N o E x t e n s i o n . e x e " , " a r g s " : [ ] , " s t o p A t E n t r y " : f a l s e , " c w d " : " {fileBasenameNoExtension}.exe", "args": [], "stopAtEntry": false, "cwd": " fileBasenameNoExtension.exe","args":[],"stopAtEntry":false,"cwd":"{fileDirname}”,
“environment”: [],
“externalConsole”: true,
“MIMode”: “gdb”,
“miDebuggerPath”: “C:\msys64\ucrt64\bin\gdb.exe”,
“setupCommands”: [
{
“description”: “Enable pretty-printing for gdb”,
“text”: “-enable-pretty-printing”,
“ignoreFailures”: true
},
{
“description”: “Redirect C:/W/B to MSYS2 UCRT64 source path”,
“text”: “set substitute-path C:/W/B C:/msys64/ucrt64”
},
{
“description”: “Skip system libraries if needed”,
“text”: “set sysroot /”,
“ignoreFailures”: true
}
],
“preLaunchTask”: “C/C++: gcc.exe build active file”
}
]
}
因為調試時默認會指向C:/W/B路徑查找庫文件,所以將該目錄重定向到C:/msys64/ucrt64,這樣就成功解決了調試時找不到庫文件報錯的問題。