visual studio code 配置C++開發環境的教程詳解 (windows 開發環境)
0 引言
最近幫GF(不幸變成ex了)配置C++開發環境,一開始想給她裝個visual studio13完事,但是一想到自己安裝以及使用時的諸多麻煩,就有點退卻,覺得沒有這個必要。正好了解到vscode大行其道,決定按照官網指示配置一版。由於本人非計算機科班出身,對編譯原理了解不多,在配置環境的時候遇到了一些麻煩,參照網上的諸多教程,最後發現還是官網比較靠譜,所以結合自己配置的教訓,寫個帖子,希望能夠幫到大家。
1 下載安裝vscode
下載網址連結如下。
https://code.visualstudio.com/
直接下載安裝即可。
2 配置語言
1)shift + ctrl + P,開啟命令列。
2)在輸入框中輸入“Configure Display Language”,點選開啟locale.json.
3) 編輯locale.json檔案,如圖所示。“locale”: "zh-CN"儲存,然後重新開啟編輯器即可。
3 安裝C/C++相關外掛,包括以下外掛。
1)C/C++
2)C++ Intellisense
3) Chinese(Simplified)中文簡體
4 安裝C++ 編譯器
選擇安裝tdm64-gcc-5.1.0-2.exe,下載網址連結如下。
https://sourceforge.net/projects/tdm-gcc/files/TDM-GCC%20Installer/tdm64-gcc-5.1.0-2.exe/download
若上述網址失效,進入http://tdm-gcc.tdragon.net/download,選第二個。 建議直接裝在C盤,可以減少後面修改路徑的麻煩。 安裝的時候,需要手動勾選如下圖所示的選項(gdb),否則下面5中launch.json "
"miDebuggerPath": "C:/TDM-GCC-64/bin/gdb64.exe"
會出錯。
5 配置程式設計環境
配置四個.json檔案,參考官方做法
https://code.visualstudio.com/docs/languages/cpp
1)新建一個資料夾,比如myVsCodeProject,在vscode中開啟這個資料夾,新建一個.cpp檔案。
2)點選“除錯”(ctrl + shift + D)-》 “新增配置” -》 選擇C++(GDB/LLDB),系統將在myVsCodeProject
資料夾下自動生成一個launch.json檔案。
需要對miDebuggerPath
進行修改,修改為 :
"miDebuggerPath": "C:/TDM-GCC-64/bin/gdb64.exe"
修改完,launch.json長這樣,可以直接將這部分內容複製到讀者對應的檔案中。
{ // 使用 IntelliSense 瞭解相關屬性。 // 懸停以檢視現有屬性的描述。 // 欲瞭解更多資訊,請訪問: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0","configurations": [ { "name": "(gdb) Launch","type": "cppdbg","request": "launch","program": "${workspaceFolder}/${fileBasenameNoExtension}.exe","args": [],"stopAtEntry": false,"cwd": "${workspaceFolder}","environment": [],"externalConsole": true,"MIMode": "gdb","miDebuggerPath": "C:/TDM-GCC-64/bin/gdb64.exe","setupCommands": [ { "description": "Enable pretty-printing for gdb","text": "-enable-pretty-printing","ignoreFailures": true } ],"preLaunchTask": "build" } ] }
修改完,c_cpp_properties.json
的內容大概如下,裡邊可以新增自己呼叫的外部連結庫的路徑。
3)ctrl + shift + p -》 “C/Cpp: Edit configurations” ,生成c_cpp_properties.json
檔案,需要修改兩處。分別是:
“includePath”:[ "${workspaceFolder}/**","C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++","C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/x86_64-w64-mingw32","C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/backward","C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include","C:/TDM-GCC-64/x86_64-w64-mingw32/include","C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include-fixed" ]
和
"compilerPath": "C:/TDM-GCC-64/bin/g++.exe"
修改完,c_cpp_properties.json
的內容大概如下,裡邊可以新增自己呼叫的外部連結庫的路徑。
{ "configurations": [ { "name": "Win32","includePath": [ "${workspaceFolder}/**","C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include-fixed","D:/Random/include" ],"defines": [ "_DEBUG","UNICODE","_UNICODE" ],"windowsSdkVersion": "8.1","compilerPath": "C:/TDM-GCC-64/bin/g++.exe","cStandard": "c11","cppStandard": "c++11","intelliSenseMode": "msvc-x64" } ],"version": 4 }
4)Ctrl+Shift+P -》Tasks: Configure Tasks -》Create tasks.json file from templates作如下修改:
"command": "g++"
和
"args":[ "-g","${fileBasename}","-fexec-charset=GBK",//Console窗體輸出字元編碼 保證能正常顯示中文 "-finput-charset=UTF-8" //輸入編譯器文字編碼 預設為UTF-8 ]
為了保證能使用C++的新特性,新增如下語句至"args":
"-std=c++17",// 使用最新的c++17標準
為了能夠在其他機器上跑,新增如下語句至"args":
"-static-libgcc",// 靜態連結
修改完後,大概長這樣。
{ // See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.json format "version": "2.0.0","tasks": [ { "label": "build","type": "shell","command": "g++","args":[ "-g","${workspaceFolder}/${fileBasename}","-I","D:/Random/include",// 編譯時用到的外部庫的地址 "-o","${workspaceFolder}/${fileBasenameNoExtension}.exe",// 指定輸出檔名,不加該引數則預設輸出a.exe "-ggdb3",// 生成和除錯有關的資訊 "-Wall",// 開啟額外警告 "-static-libgcc",// 靜態連結 "-std=c++11",// 使用最新的c++17標準 "-Wno-format",//Console窗體輸出字元編碼 保證能正常顯示中文 "-finput-charset=UTF-8" //輸入編譯器文字編碼 預設為UTF-8 ],"group": { "kind": "build","isDefault": true } } ] }
5) debug除錯即可執行 ,enjoy!
6 配置檔案作用詳細說明
可以參考我的另一篇
https://www.jb51.net/article/183537.htm
總結
到此這篇關於visual studio code 配置C++開發環境的教程詳解 (windows 開發環境)的文章就介紹到這了,更多相關visual studio code 配置C++內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!