路線圖
- [x] 所需MSYS包安裝
- [x] VSCode插件和環(huán)境配置
- [x] GCC平臺(tái)編譯
- [ ] MSVC CL平臺(tái)編譯
目的
- 在Windows平臺(tái)中繼續(xù)使用VS Code編寫調(diào)試C/C++程序
- 在Windows平臺(tái)中兼顧gcc的項(xiàng)目編譯,減少跨平臺(tái)錯(cuò)誤
- 避免使用Visual Studio的相應(yīng)鍵位绢彤,使用VS Code的高效編輯
準(zhǔn)備
- MSYS2(推薦) / MinGW
- VSCode
- VSCode 插件 (C/C++, Cmake, Cmake-tools)
路徑與環(huán)境變量設(shè)置
MSYS_DIR=C:\msys64
MINGW64_DIR=C:\msys64\mingw64
- 加入
Path
:%MSYS_DIR%\usr\bin
,%MINGW64_DIR%\bin
MSYS安裝配置
安裝MSYS2
-
修改MSYS的Server配置粱甫,分別在對(duì)應(yīng)文件開(kāi)頭加入如下語(yǔ)句
# in mirrorlist.msys Server = http://mirrors.ustc.edu.cn/msys2/msys/$arch # in mirrorlist.mingw64 Server = http://mirrors.ustc.edu.cn/msys2/mingw/x86_64 # in mirrorlist.mingw32 Server = http://mirrors.ustc.edu.cn/msys2/mingw/i686
從MSYS庫(kù)安裝
msys/make
安裝MinGW庫(kù)安裝
gcc
滔韵、gdb
、cmake
舅列、make
和opencv
祟峦、boost
(可選)等相關(guān)組件庫(kù)
VSCode配置
- 建立項(xiàng)目文件夾
-
Ctrl+Shift+P
->C/Cpp: Edit Configuration...
, 輸入如下配置: (若是gcc
位置不同,需要自行修改compilerPath
和includePath
){ "configurations": [ { "name": "clang", "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 }
- 編寫C/C++文件
- 配置當(dāng)前Workspace中的C/C++ IntelliSense設(shè)置:
{ "cmake.configureOnOpen": true, "cmake.generator": "MSYS Makefiles", "C_Cpp.default.configurationProvider": "vector-of-bool.cmake-tools" }
- 在項(xiàng)目根目錄下建立
CMakeLists.txt
,配置項(xiàng)目的主要編譯內(nèi)容(參見(jiàn)文件夾中的三個(gè)例子) - 在對(duì)應(yīng)的自定義包目錄下建立
CMakeLists.txt
,將對(duì)應(yīng)的子目錄聲明加入對(duì)應(yīng)的模塊 - 在根目錄
CMakeLists.txt
使用Ctrl+Shift+P
->CMake: Configure
來(lái)配置項(xiàng)目環(huán)境變量 -
Ctrl+Shift+P
->CMake: Build
- 到對(duì)應(yīng)的
build
目錄下找到對(duì)應(yīng)的可執(zhí)行文件 - 配置調(diào)試
launch.json
,參考模板如下:{ // Use IntelliSense to learn about possible attributes. // Hover to view descriptions of existing attributes. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "(gdb) Launch", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/build/cmtst.exe", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": true, "MIMode": "gdb", "miDebuggerPath": "C:/msys64/mingw64/bin/gdb.exe", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ] } ] }
代碼參考
請(qǐng)移步個(gè)人Github