vs code 配置c/c++環(huán)境的詳細(xì)教程(推薦)
寫在前面的一段話
我這個(gè)配置方法反正在win10上是可以用的,我自己的筆記本就是win10的系統(tǒng)。但是在實(shí)驗(yàn)室蹭的學(xué)長的主機(jī)卻是win7的系統(tǒng),按道理來說這個(gè)配置應(yīng)該是不分系統(tǒng)的?但是我確實(shí)是折騰了好幾天,看了很多博客的配置方法,也問過稍微會(huì)一些的人,沒有一個(gè)能解決我問題的,也希望有人能評論解決我的問題,我的vscode問題是c++只能編譯運(yùn)行無法debug。每次按F5都是一閃而過,無法調(diào)試,再按就顯示調(diào)試程序正在運(yùn)行。-------2019.5.3
實(shí)驗(yàn)室我把系統(tǒng)換了,用了自己喜歡很久的ubuntu 18.04
vs code 配置c/c++環(huán)境
雖然平時(shí)比賽要求用的是code blocks,但是很尷尬的是我不喜歡code blocks的那個(gè)界面,平時(shí)編程感覺太白很傷眼睛。再加上最近一直在折騰個(gè)人博客,對于前端,雖然沒基礎(chǔ),但是還是知道用vs code還不錯(cuò)。因此,我又開始了瞎折騰的精神。希望平時(shí)編程也能通過vs code編譯c++,但是正常情況下vs code對c++編譯是很不友好的。所以想要成功編譯c++程序,要通過自己配置。但是我在網(wǎng)上搜到的教程很少有能一次解決我的問題的。所以參考了幾篇博客后,總結(jié)了一下。
2019.8.6 更新文章(更改生成文件位置)
不需要更改,或者只是為了編譯運(yùn)行C/C++的可以直接往下看。
這個(gè)是Linux的,如果需要windows,可以自己試一試,具體應(yīng)該不會(huì)差太多
因?yàn)槲募峙洳粔蛎烙^(我用的文件比較多,導(dǎo)致生成的.o文件都堆疊在一起,很丑),所以我動(dòng)了在編譯文件時(shí)將生成的a/a.o文件更換位置再運(yùn)行,一開始打算通過命令行命令直接在其他位置生成生成文件,但是我并沒有找到此類命令,然后發(fā)現(xiàn)編譯時(shí)運(yùn)行的命令是這樣的cd "/home/acm/Documents/acm/" && g++ A.cpp -o A && "/home/acm/Documents/acm/"A。所以我決定在編譯后的命令間加上移動(dòng)文件命令mv A /home/acm/Documents/acm/Documents/這樣的話就將生成文件放在了acm目錄下的Documents文件夾下,不會(huì)導(dǎo)致多個(gè)a/a.o文件堆積在acm目錄下。而task.json文件我沒有找的修改方法,應(yīng)該可以修改"command"實(shí)現(xiàn),它可以運(yùn)行腳本,但是我寫不來。。。所以我是通過修改了code-runner的命令來實(shí)現(xiàn)的。下圖為我修改后的vs code

實(shí)現(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是我自己的目錄,請自己修改,其他編譯命令也可以修改
我整個(gè)文件的結(jié)構(gòu)如圖

上面是正常的編譯運(yùn)行,還要一處就是調(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 更新文章
因?yàn)樽罱腥藛栁胰齻€(gè)文件里的c_cpp_properties.json會(huì)報(bào)錯(cuò),我也不知道報(bào)錯(cuò)的具體原因,可能是因?yàn)榘姹靖??幾個(gè)月前vscode就可以配置時(shí)不需要在加c_cpp_properties.json這個(gè)文件了。對其余兩個(gè)文件單獨(dú)更新一下。也因?yàn)樽罱匮b過電腦,自己重新找教程配置了一遍。
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, //強(qiáng)調(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)境/編寫運(yù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確定后退出,注意這個(gè)應(yīng)該是你自己的安裝地址
檢查一下自己是否安裝配置完成
打開命令提示符輸入
gcc -v

配置vs code

找一個(gè)文件夾作為你的工作區(qū),然后在這個(gè)工作區(qū)下建立一個(gè)新的文件夾,命名為.vscode,注意點(diǎn)是不能缺少的。然后在你建立的.vscode文件夾下建立三個(gè).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
}
其中尤為需要注意的就是那些安裝位置,配置的時(shí)候請參考我的來找到你自己安裝的位置。
還有就是在一些地址輸入不能用\分割要用/或者\\
例如:E:/vs code/mingw/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++
E:/vs code/mingw是我安裝時(shí)自己選的文件夾,從mingw64開始均為安裝時(shí)自己出現(xiàn)的文件,所以請不要直接復(fù)制粘貼,注意自己的安裝位置。
這樣基本就配置完了,測試一下經(jīng)典的Hello World
#include<iostream>
using namespace std;
int main()
{
cout<<"Hello World!"<<endl;
return 0;
}
直接F6運(yùn)行,或者選擇在return 0處加個(gè)斷點(diǎn)再F5,都能成功測試是否配置完成。不過加斷點(diǎn)輸出的那個(gè)會(huì)比較好看=.=
當(dāng)然估計(jì)在配置過程中問題是少不了的。如果有我未提到的問題,你可以發(fā)給我郵件tanjinxiao25@gmail.com,我推薦你直接在GitHub的倉庫里添加issue。因?yàn)槲也荒鼙WC自己能夠徹底的解決,但是在issues里總有大佬會(huì)回答問題的。
到此這篇關(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 錯(cuò)誤,請更新includePath)(POSIX API)
- C++讀寫ini配置文件實(shí)現(xiàn)過程詳解
- Visual Studio Code配置C/C++開發(fā)環(huán)境的教程圖解
- Visual Studio Code 配置C、C++環(huán)境/編譯并運(yùn)行的流程分析
- Ubuntu 20.04 下安裝配置 VScode 的 C/C++ 開發(fā)環(huán)境(圖文教程)
- Windows配置VSCode+CMake+Ninja+Boost.Test的C++開發(fā)環(huán)境(教程詳解)
- C++讀寫配置項(xiàng)的基本操作
相關(guān)文章
C語言實(shí)現(xiàn)運(yùn)籌學(xué)中的馬氏決策算法實(shí)例
這篇文章主要介紹了C語言實(shí)現(xiàn)運(yùn)籌學(xué)中的馬氏決策算法,簡單介紹了馬氏決策的概念,并結(jié)合實(shí)例形式分析了C語言實(shí)現(xiàn)馬氏決策算法的具體實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-09-09
C++ string與int的相互轉(zhuǎn)換(使用C++11)
本文主要介紹了C++ string與int的相互轉(zhuǎn)換(使用C++11),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01

