標籤:建立 col 一個 arc console mes image c/c++ pre
每次換台電腦寫c++,就要找配置,很是繁瑣。這次自己寫篇部落格,記錄下相關配置過程。
安裝編譯器
開啟下面的網站http://www.msys2.org/,下載 64 位元的 MYSYS2,按照首頁上的步驟安裝軟體。完整完成後,進入 /etc/pacman.d 目錄分別修改下面 3 個檔案。
mirrorlist.mingw32 檔案內容為:
Server = http://mirrors.ustc.edu.cn/msys2/mingw/i686/
mirrorlist.mingw64 檔案內容為:
Server = http://mirrors.ustc.edu.cn/msys2/mingw/x86_64/
mirrorlist.msys 檔案內容為:
Server = http://mirrors.ustc.edu.cn/msys2/msys/$arch/
然後輸入下面命令,更新系統。
cd ~pacman -Syu
再次輸入命令,安裝 c++ 工具。
pacman -S mingw-w64-x86_64-toolchain
出現以下提示,分別選擇 3,9,13。檔案有點大,耐心等待下載。
完成後,把 e:\Softwares\msys64\mingw64\bin\加入到系統內容中。注意:e:\Softwares目錄根據實際情況調正。
安裝VSCode
這次工具我們選擇 VsCode,方便實用,雖然c++的外掛程式弱了點,但是微軟開發的,不會差到哪裡去。安裝完成後選擇 c/c++ 外掛程式。見。
開啟軟體,然後在File菜單中,點擊Add Folder to Workspace..., 選擇一個目錄作為以後寫c++代碼的檔案夾,這裡我們假設選擇的檔案夾為 cpp。在 cpp 下建立目錄 .vscode, 然後在 .vscode 目錄下分別建立 task.json,c_cpp_properties.json, launch.json 3 個檔案。見。
tasks.cpp 的內容如下:
{ "version": "2.0.0", "tasks": [ { "label": "Compile", "command": "g++", "args": [ "-g", " ${file}", //指定編譯原始碼檔案 "-o", " ${fileDirname}/${fileBasenameNoExtension}.exe", // 指定輸出檔案名,不加該參數則預設輸出a.exe "-ggdb3", // 產生和調試有關的資訊 "-Wall", // 開啟額外警告 "-static-libgcc", // 靜態連結 "-std=c++17", // 使用最新的c++17標準 "-Wno-format", "-fexec-charset=GBK", //Console表單輸出字元編碼 保證能正常顯示中文 "-finput-charset=UTF-8" //輸入編譯器文本編碼 預設為UTF-8 ], "type": "shell", "group": { "kind": "build", "isDefault": true }, "presentation": { "echo": true, "reveal": "always", // 在“終端”中顯示編譯資訊的策略,可以為always,silent,never "focus": false, "panel": "shared" // 不同的檔案的編譯資訊共用一個終端面板 }, "problemMatcher": { "owner": "cpp", "fileLocation": [ "relative", "\\" ], "pattern": { "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$", "file": 1, "line": 2, "column": 3, "severity": 4, "message": 5 } } } ]}
c_cpp_properties.json 的內容:
{ "configurations": [ { "name": "Win32", "intelliSenseMode": "clang-x64", "includePath": [ "${workspaceFolder}", "e:/Softwares/msys64/mingw64/include/c++/7.3.0/" ], "defines": [ "_DEBUG", "UNICODE", "__GNUC__=7", "__cdecl=__attribute__((__cdecl__))" ], "browse": { "path": [ "${workspaceFolder}", "e:/Softwares/msys64/mingw64/include/c++/7.3.0/" ] }, "limitSymbolsToIncludedHeaders": true, "databaseFilename": "", "cStandard": "c11", "cppStandard": "c++17" } ], "version": 3}
launch.json 用於代碼調試,目前還無法工作。
運行代碼
完成上面的工作後,在 cpp 目錄下,寫個 test.cpp 檔案,然後按 Ctrl+Shift+B,就會產生 test.exe 檔了。然後在終端執行 test.exe。
VSCode 配置 C++