windows 10環境下 使用 msys2 + vs code 配置 c++ 的編譯環境(轉)
阿新 • • 發佈:2019-01-02
不太多描述 msys2 與 vs code ,既然你需要安裝 一種語言的編譯環境了 ,你肯定對這兩個不陌生;
1. 先安裝msys2; (下載多少位的msys2就安裝多少位的 mingw,本人安裝 32位的)
按照 官網文件,一步一步安裝。
2.修改msys 源(推薦修改)
pacman 的配置 編輯 /etc/pacman.d/mirrorlist.mingw32 ,在檔案開頭新增: Server = https://mirrors.tuna.tsinghua.edu.cn/msys2/mingw/i686 編輯 /etc/pacman.d/mirrorlist.mingw64 ,在檔案開頭新增: Server= https://mirrors.tuna.tsinghua.edu.cn/msys2/mingw/x86_64 編輯 /etc/pacman.d/mirrorlist.msys ,在檔案開頭新增: Server = https://mirrors.tuna.tsinghua.edu.cn/msys2/msys/$arch 然後執行 pacman -Sy 重新整理軟體包資料即可。
,配置好之後, 輸入 pacman -Syu更新 msys2軟體包;
更新好之後,分別安裝 gcc,gdb,make 包
注意點:當你更新msys2 之後,檢視,msys2的安裝目錄下,多出很多.exe 檔案;
最好跟你 安裝的msys2的 位數 一致 ,32位的msys2下的,你就啟動 mingw32.exe; 同理 64位。
,開始安裝 gcc,gdb,make;
在窗體中分別輸入:
pacman -S mingw32/mingw-w64-i686-gcc
pacman -S mingw32/mingw-w64-i686-gdb
pacman -S mingw32/mingw-w64-i686-make
安裝完成之後, 配置 mingw32的環境變數:
Path 新增 “C:\msys32\mingw32\bin;”
3.驗證:windows+R = cmd
在cmd 窗體中 分別輸入:
gcc -v
gdb -v
檢視gcc 與gdb 是否安裝ok 以及他們的版本資訊;
5.配置 vs code 的c++編譯環境
開啟 vs code ,
選中c++ 相關的配置,會在本地多出一個.vscode的檔案,並有 launch.json 檔案。這進行配置,
launch.json:
{ "version": "0.2.0", "configurations": [ { "name": "C++ Launch", "type": "cppdbg", "request": "launch", "targetArchitecture": "x86", "program": "${workspaceRoot}/a.exe", "args": [], "stopAtEntry": false, "cwd": "${workspaceRoot}", "environment": [], "externalConsole": true, "miDebuggerPath":"C:/msys32/mingw32/bin/gdb.exe", "preLaunchTask":"g++", "linux": { "MIMode": "gdb" }, "osx": { "MIMode": "lldb" }, "windows": { "MIMode": "gdb" } }, { "name": "C++ Attach", "type": "cppdbg", "request": "launch", "targetArchitecture": "x64", "program": "${workspaceRoot}/a.exe", "args": [], "stopAtEntry": false, "cwd": "${workspaceRoot}", "environment": [], "processId": "${command.pickProcess}", "externalConsole": false, "preLaunchTask":"g++", "linux": { "MIMode": "gdb" }, "osx": { "MIMode": "lldb" }, "windows": { "MIMode": "gdb" } } ] }
注意
miDebuggerPath 節點的配置,要與本地的路徑一直.
配置好了之後,F5啟動除錯,這個時候,會讓你配置tasks.json。 會出現,一個下拉框,你隨便選擇一個(目的,是為了出現 tasks.json 檔案),這樣,直接 配置 tasks.json 檔案即可;
5.配置 tasks.json 檔案:
{ "version": "0.1.0", "command": "g++", "args": ["-g","${file}"], "problemMatcher": { "owner": "cpp", "fileLocation": ["relative", "${workspaceRoot}"], "pattern": { "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$", "file": 1, "line": 2, "column": 3, "severity": 4, "message": 5 } } }
配置好了之後,
有一個點要注意,配置下 #include<> 標頭檔案的路徑,vs code,經常提示,沒有找到 include 的檔案路徑。
把 滑鼠放在綠色的波浪線 上,會出現一個 燈泡 ,那個是配置檔案的入口,別忘記配置了;
c_cpp_properties.json:
{ "configurations": [ { "name": "Mac", "includePath": [ "/usr/include" ], "browse": { "limitSymbolsToIncludedHeaders": true, "databaseFilename": "" } }, { "name": "Linux", "includePath": [ "/usr/include" ], "browse": { "limitSymbolsToIncludedHeaders": true, "databaseFilename": "" } }, { "name": "Win32", "includePath": [ "C:/msys32/mingw32/include/c++/6.1.0/*", "C:/msys32/mingw32/i686-w64-mingw32/include/*", "C:/msys32/mingw32/include" ], "browse": { "limitSymbolsToIncludedHeaders": true, "databaseFilename": "" } } ], "clang_format": { "style": "file", "fallback-style": "LLVM", "sort-includes": false } }