ubuntu系統(tǒng)vscodeC++編譯環(huán)境配置與使用方式
本文參考官網(wǎng):https://code.visualstudio.com/docs/cpp/config-linux,主要介紹在ubuntu系統(tǒng)上vscodeC++編譯環(huán)境配置與使用方法。
一、環(huán)境配置與使用
1、軟件與插件安裝
vscode軟件,可以在官網(wǎng)下載。
檢查是否安裝g++和gcc。
確保安裝GCC gcc -v 如果沒(méi)有安裝,需要另行安裝 sudo apt-get update sudo apt-get install build-essential gdb
需要安裝C/C++插件。
2、創(chuàng)建工程項(xiàng)目
創(chuàng)建工程目錄
mkdir projects cd projects mkdir helloworld cd helloworld code .
創(chuàng)建主函數(shù)
#include <iostream> #include <vector> #include <string> using namespace std; int main() { vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"}; for (const string& word : msg) { cout << word << " "; } cout << endl; }
3、運(yùn)行與調(diào)試
1)程序運(yùn)行
2)tasks.json模板
{ "version": "2.0.0", "tasks": [ { "type": "shell", "label": "C/C++: g++ build active file", "command": "/usr/bin/g++", "args": ["-g", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}"], "options": { "cwd": "/usr/bin" }, "problemMatcher": ["$gcc"], "group": { "kind": "build", "isDefault": true }, "detail": "Task generated by Debugger." } ] }
3)程序調(diào)試
從播放按鈕旁邊的下拉菜單中,選擇Debug C/C++ File
4)launch.json文件模板
{ // 使用 IntelliSense 了解相關(guān)屬性。 // 懸停以查看現(xiàn)有屬性的描述。 // 欲了解更多信息,請(qǐng)?jiān)L問(wèn): https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "(gdb) 啟動(dòng)", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/${fileBasenameNoExtension}.out", "args": [], "stopAtEntry": false, "cwd": "${fileDirname}", "environment": [], "externalConsole": false, "MIMode": "gdb", "setupCommands": [ { "description": "為 gdb 啟用整齊打印", "text": "-enable-pretty-printing", "ignoreFailures": true }, { "description": "將反匯編風(fēng)格設(shè)置為 Intel", "text": "-gdb-set disassembly-flavor intel", "ignoreFailures": true } ] } ] }
5)c_cpp_properties.json文件模板
如果您想更好地控制 C/C++ 擴(kuò)展,您可以創(chuàng)建一個(gè)c_cpp_properties.json文件,該文件將允許您更改設(shè)置,例如編譯器的路徑、包含路徑、C++ 標(biāo)準(zhǔn)(默認(rèn)為 C++17)等等
頭文件:如果您的程序包含不在工作區(qū)或標(biāo)準(zhǔn)庫(kù)路徑中的頭文件,您只需要修改Include path Visual Studio Code 將這些設(shè)置放在.vscode/c_cpp_properties.json
二、配置文件
Vscode通常需要配置三個(gè)文件:
tasks.json
(編譯器配置文件)launch.json
(調(diào)試器配置文件)c_cpp_properties.json
(編譯器路徑和intellisense設(shè)置)
1、配置文件生成方法
① tasks.json : 編譯器構(gòu)建 配置文件 ;
終端-配置任務(wù)
② launch.json : 調(diào)試器設(shè)置 配置文件 ;
ctrl+shift+D,創(chuàng)建 launch.json文件
③ c_cpp_properties.json : 編譯器路徑和智能代碼提示 配置文件 ;
Ctrl+Shift+P,輸入C/C++:Edit Configuration
2、配置文件解析
①tasks.json解析
{ "version": "2.0.0", "tasks": [ { "type": "shell", "label": "C/C++: g++ build active file", "command": "/usr/bin/g++", "args": ["-g", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}"], "options": { "cwd": "/usr/bin" }, "problemMatcher": ["$gcc"], "group": { "kind": "build", "isDefault": true }, "detail": "Task generated by Debugger." } ] }
command
:設(shè)置制定要運(yùn)行的程序,一般為g++args
:用數(shù)組的形式,將指定的g++命令行參數(shù)傳入,必須按照編譯器順序進(jìn)行制定。本task文件的含義是label
:任務(wù)列表中看到的名字,可以任意命名detail
:將在任務(wù)列表中作為任務(wù)描述的值
修改task.json文件主要修改args部分
- 使用"${workspaceFolder}/*.cpp" 代替${file} ,這將編譯文件夾中所有的CPP文件
- 使用自定義的名字如helloworld.out代替"${fileDirname}/${fileBasenameNoExtension}"
更多args配置參數(shù),可以參考:https://code.visualstudio.com/docs/editor/variables-reference
②launch.json文件解析
{ // 使用 IntelliSense 了解相關(guān)屬性。 // 懸停以查看現(xiàn)有屬性的描述。 // 欲了解更多信息,請(qǐng)?jiān)L問(wèn): https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "(gdb) 啟動(dòng)", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/${fileBasenameNoExtension}.out", "args": [], "stopAtEntry": false, "cwd": "${fileDirname}", "environment": [], "externalConsole": false, "MIMode": "gdb", "setupCommands": [ { "description": "為 gdb 啟用整齊打印", "text": "-enable-pretty-printing", "ignoreFailures": true }, { "description": "將反匯編風(fēng)格設(shè)置為 Intel", "text": "-gdb-set disassembly-flavor intel", "ignoreFailures": true } ] } ] }
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
C語(yǔ)言實(shí)現(xiàn)BST二叉排序樹(shù)的基本操作
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)BST二叉排序樹(shù)的基本操作,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09VS2019如何創(chuàng)建C++項(xiàng)目的實(shí)現(xiàn)示例
這篇文章主要介紹了VS2019如何創(chuàng)建C++項(xiàng)目的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08C++簡(jiǎn)單QQ程序服務(wù)器端的實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了C++簡(jiǎn)單QQ程序服務(wù)器端的實(shí)現(xiàn)代碼,感興趣的朋友可以參考一下2016-05-05c語(yǔ)言程序設(shè)計(jì)文件操作方法示例(CreateFile和fopen)
c主要的文件操作函數(shù)有:CreateFile,CloseHandle,ReadFile,WriteFile,SetFilePointer,GetFileSize。其中的讀寫(xiě)操作是以字符為單位,獲得文件大小也是以字符為單位。2013-12-12OpenCV實(shí)現(xiàn)單目尺寸估計(jì)的案例詳解
這篇文章主要介紹了通過(guò)OpenCV如何實(shí)現(xiàn)單目尺寸估計(jì),文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)和工作有一定的參考價(jià)值,感興趣的可以了解一下2022-01-01C語(yǔ)言實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)(多文件)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)學(xué)生信息管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-12-12