vs code 配置c/c++環(huán)境的詳細(xì)教程(推薦)
寫在前面的一段話
我這個配置方法反正在win10上是可以用的,我自己的筆記本就是win10的系統(tǒng)。但是在實驗室蹭的學(xué)長的主機卻是win7的系統(tǒng),按道理來說這個配置應(yīng)該是不分系統(tǒng)的?但是我確實是折騰了好幾天,看了很多博客的配置方法,也問過稍微會一些的人,沒有一個能解決我問題的,也希望有人能評論解決我的問題,我的vscode問題是c++只能編譯運行無法debug。每次按F5都是一閃而過,無法調(diào)試,再按就顯示調(diào)試程序正在運行。-------2019.5.3
實驗室我把系統(tǒng)換了,用了自己喜歡很久的ubuntu 18.04
vs code 配置c/c++環(huán)境
雖然平時比賽要求用的是code blocks,但是很尷尬的是我不喜歡code blocks的那個界面,平時編程感覺太白很傷眼睛。再加上最近一直在折騰個人博客,對于前端,雖然沒基礎(chǔ),但是還是知道用vs code還不錯。因此,我又開始了瞎折騰的精神。希望平時編程也能通過vs code編譯c++,但是正常情況下vs code對c++編譯是很不友好的。所以想要成功編譯c++程序,要通過自己配置。但是我在網(wǎng)上搜到的教程很少有能一次解決我的問題的。所以參考了幾篇博客后,總結(jié)了一下。
2019.8.6 更新文章(更改生成文件位置)
不需要更改,或者只是為了編譯運行C/C++的可以直接往下看。
這個是Linux的,如果需要windows,可以自己試一試,具體應(yīng)該不會差太多
因為文件分配不夠美觀(我用的文件比較多,導(dǎo)致生成的.o文件都堆疊在一起,很丑),所以我動了在編譯文件時將生成的a/a.o文件更換位置再運行,一開始打算通過命令行命令直接在其他位置生成生成文件,但是我并沒有找到此類命令,然后發(fā)現(xiàn)編譯時運行的命令是這樣的cd "/home/acm/Documents/acm/" && g++ A.cpp -o A && "/home/acm/Documents/acm/"A
。所以我決定在編譯后的命令間加上移動文件命令mv A /home/acm/Documents/acm/Documents/
這樣的話就將生成文件放在了acm目錄下的Documents文件夾下,不會導(dǎo)致多個a/a.o文件堆積在acm目錄下。而task.json文件我沒有找的修改方法,應(yīng)該可以修改"command"實現(xiàn),它可以運行腳本,但是我寫不來。。。所以我是通過修改了code-runner的命令來實現(xiàn)的。下圖為我修改后的vs code
實現(xiàn)方法
安裝code-runner后在settings.json中找到code-runner.executorMap,將其中的cpp
修改為"cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt && mv $fileNameWithoutExt $dir/Documents/$fileNameWithoutExt && $dir/Documents/$fileNameWithoutExt",
其中的Documents是我自己的目錄,請自己修改,其他編譯命令也可以修改
我整個文件的結(jié)構(gòu)如圖
上面是正常的編譯運行,還要一處就是調(diào)試的生成文件,這里修改launch.json即可
更改位置的task.json
{ // See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.json format "version": "2.0.0", "command": "g++", "args": [ "-g", "-std=c++11", // "${workspaceFolder}/${file}", // "${file}", "${workspaceFolder}/${fileBasenameNoExtension}.cpp", "-o", "${workspaceFolder}/Documents/${fileBasenameNoExtension}.o", ],// 編譯命令參數(shù) "problemMatcher":{ "owner": "cpp", "fileLocation":[ "relative", "${workspaceFolder}/Documents" ], "pattern":[ { "regexp": "^([^\\\\s].*)\\\\((\\\\d+,\\\\d+)\\\\):\\\\s*(.*)$", "file": 1, "location": 2, //"message": 3 } ] }, "group": { "kind": "build", "isDefault": true } } //"command": "g++",
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}/Documents/${fileBasenameNoExtension}.o", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}/Documents", "environment": [], "externalConsole": false, "MIMode": "gdb", "miDebuggerPath": "/usr/bin/gdb", "preLaunchTask": "g++", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ] }, ] } //"program": "${workspaceFolder}/${fileBasenameNoExtension}.o", //"cwd": "${workspaceFolder}",
2019.4.3 更新文章
因為最近有人問我三個文件里的c_cpp_properties.json會報錯,我也不知道報錯的具體原因,可能是因為版本更新?幾個月前vscode就可以配置時不需要在加c_cpp_properties.json這個文件了。對其余兩個文件單獨更新一下。也因為最近重裝過電腦,自己重新找教程配置了一遍。
tasks.json
{ "version": "2.0.0", "tasks": [ { "label": "Build", "command": "g++", "args": [ "-g", "-Wall", "-std=c++11", "-lm", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}.exe" ], "presentation": { "reveal": "always", "echo": false, "focus": true }, "problemMatcher": { "owner": "cpp", "fileLocation": "absolute", "pattern": { "regexp": "^(.*):(\\d+):(\\d+):\\s+(error):\\s+(.*)$", "file": 1, "line": 2, "column": 3, "severity": 4, "message": 5 } } }, { "label": "Run", "type": "shell", "dependsOn": "Build", "command": "${fileDirname}/${fileBasenameNoExtension}.exe", "args": [], "presentation": { "reveal": "always", "focus": true }, "problemMatcher": [], "group": { "kind": "test", "isDefault": true } } ] }
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": "${fileDirname}/${fileBasenameNoExtension}.exe", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "externalConsole": false, //強調(diào)一下,我習(xí)慣用vscode自帶的終端,所以你不習(xí)慣可以改為true "MIMode": "gdb", "miDebuggerPath": "E:\\vs code\\MinGW\\mingw64\\bin\\gdb.exe", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ], "preLaunchTask": "Build" } ] }
參考博客
Visual Studio Code (vscode)編譯C++
整理:Visual Studio Code (vscode) 配置C、C++環(huán)境/編寫運行C、C++(Windows)
下載Visual Studio Code
安裝插件
可以選擇ctrl+shift+x在商店里搜索或者選擇ctrl+shift+p打開命令框輸入
ext install cpptools
安裝該插件
安裝MinGW-W64,配置環(huán)境
據(jù)說MinGW已經(jīng)不更新了?=.=所以安裝MinGW-W64
安裝過程請注意自己的電腦是32還是64位的,其他的默認(rèn)就可以了。還要額外注意下自己的下載地址后面要用。
然后打開我的電腦->屬性->高級系統(tǒng)設(shè)置->環(huán)境變量
選擇新建,添加剛剛記住的E:\vs code\mingw\mingw64\bin確定后退出,注意這個應(yīng)該是你自己的安裝地址
檢查一下自己是否安裝配置完成
打開命令提示符輸入
gcc -v
配置vs code
找一個文件夾作為你的工作區(qū),然后在這個工作區(qū)下建立一個新的文件夾,命名為.vscode
,注意點是不能缺少的。然后在你建立的.vscode
文件夾下建立三個.json
文件,分別為tasks.json,launch.json,c_cpp_properties.json。
配置內(nèi)容如下:
tasks.json
{ "version": "2.0.0", "command": "g++", "args": ["-g","-std=c++11","${file}","-o","${workspaceRoot}\\${fileBasename}.exe"], "problemMatcher": { "owner": "cpp", "fileLocation": ["relative", "${workspaceRoot}"], "pattern": { "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$", "file": 1, "line": 2, "column": 3, "severity": 4, "message": 5 } } }
launch.json
{ "version": "0.2.0", "configurations": [ { "name": "C++ Launch (GDB)", "type": "cppdbg", "request": "launch", "targetArchitecture": "x86", "program": "${workspaceRoot}\\${fileBasename}.exe", "miDebuggerPath":"E:/vs code/mingw/mingw64/bin/gdb.exe", "args": [], "stopAtEntry": false, "cwd": "${workspaceRoot}", "externalConsole": true, "preLaunchTask": "g++" } ] }
c_cpp_properties.json
{ "configurations": [ { "name": "Win32", "includePath": [ "${workspaceRoot}", "E:/vs code/mingw/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++", "E:/vs code/mingw/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32", "E:/vs code/mingw/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward", "E:/vs code/mingw/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include", "E:/vs code/mingw/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/tr1", "E:/vs code/mingw/mingw64/x86_64-w64-mingw32/include" ], "defines": [ "_DEBUG", "UNICODE", "__GNUC__=6", "__cdecl=__attribute__((__cdecl__))" ], "intelliSenseMode": "msvc-x64", "browse": { "path": [ "${workspaceRoot}", "E:/vs code/mingw/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++", "E:/vs code/mingw/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32", "E:/vs code/mingw/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward", "E:/vs code/mingw/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include", "E:/vs code/mingw/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/tr1", "E:/vs code/mingw/mingw64/x86_64-w64-mingw32/include" ] }, "limitSymbolsToIncludedHeaders": true, "databaseFilename": "", "compilerPath": "E:\\vs code\\mingw\\mingw64\\bin\\gcc.exe", "cStandard": "c11", "cppStandard": "c++17" } ], "version": 4 }
其中尤為需要注意的就是那些安裝位置,配置的時候請參考我的來找到你自己安裝的位置。
還有就是在一些地址輸入不能用\
分割要用/
或者\\
例如:E:/vs code/mingw/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++
E:/vs code/mingw是我安裝時自己選的文件夾,從mingw64開始均為安裝時自己出現(xiàn)的文件,所以請不要直接復(fù)制粘貼,注意自己的安裝位置。
這樣基本就配置完了,測試一下經(jīng)典的Hello World
#include<iostream> using namespace std; int main() { cout<<"Hello World!"<<endl; return 0; }
直接F6運行,或者選擇在return 0處加個斷點再F5,都能成功測試是否配置完成。不過加斷點輸出的那個會比較好看=.=
當(dāng)然估計在配置過程中問題是少不了的。如果有我未提到的問題,你可以發(fā)給我郵件tanjinxiao25@gmail.com,我推薦你直接在GitHub的倉庫里添加issue。因為我不能保證自己能夠徹底的解決,但是在issues里總有大佬會回答問題的。
到此這篇關(guān)于vs code 配置c/c++環(huán)境的詳細(xì)教程(推薦)的文章就介紹到這了,更多相關(guān)vs code 配置c/c++環(huán)境內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 最新VScode C/C++ 環(huán)境配置的詳細(xì)教程
- Linux配置C++11編譯環(huán)境的方法
- vscode 配置 C/C++編譯環(huán)境(完整教程)
- C++讀取配置文件的示例代碼
- VS Code C/C++環(huán)境配置教程(無法打開源文件“xxxxxx.h”或者檢測到 #include 錯誤,請更新includePath)(POSIX API)
- C++讀寫ini配置文件實現(xiàn)過程詳解
- Visual Studio Code配置C/C++開發(fā)環(huán)境的教程圖解
- Visual Studio Code 配置C、C++環(huán)境/編譯并運行的流程分析
- Ubuntu 20.04 下安裝配置 VScode 的 C/C++ 開發(fā)環(huán)境(圖文教程)
- Windows配置VSCode+CMake+Ninja+Boost.Test的C++開發(fā)環(huán)境(教程詳解)
- C++讀寫配置項的基本操作
相關(guān)文章
C++ 關(guān)于STL中sort()對struct排序的方法
本篇文章介紹了,關(guān)于STL中sort()對struct排序的方法。需要的朋友參考下2013-04-04vscode cmake compilers配置路徑的實現(xiàn)
本文主要介紹了vscode cmake compilers配置路徑的實現(xiàn),文中通過圖文介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-03-03Matlab實現(xiàn)數(shù)據(jù)的動態(tài)顯示方法
這篇文章主要為大家詳細(xì)介紹了Matlab使用Plot函數(shù)實現(xiàn)數(shù)據(jù)動態(tài)顯示方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-06-06C/C++?Linux?Socket網(wǎng)絡(luò)編程流程分析
這篇文章主要介紹了C/C++?Linux?Socket網(wǎng)絡(luò)編程,Linux環(huán)境中的C/C++?socket?與Window環(huán)境中的C/C++?socket類似,本文所記錄的是TCP協(xié)議的socket編程,圖文實例相結(jié)合給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02