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 如果沒有安裝,需要另行安裝 sudo apt-get update sudo apt-get install build-essential gdb
需要安裝C/C++插件。
2、創(chuà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、運行與調試
1)程序運行
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)程序調試
從播放按鈕旁邊的下拉菜單中,選擇Debug C/C++ File
4)launch.json文件模板
{ // 使用 IntelliSense 了解相關屬性。 // 懸停以查看現(xiàn)有屬性的描述。 // 欲了解更多信息,請訪問: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "(gdb) 啟動", "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": "將反匯編風格設置為 Intel", "text": "-gdb-set disassembly-flavor intel", "ignoreFailures": true } ] } ] }
5)c_cpp_properties.json文件模板
如果您想更好地控制 C/C++ 擴展,您可以創(chuàng)建一個c_cpp_properties.json文件,該文件將允許您更改設置,例如編譯器的路徑、包含路徑、C++ 標準(默認為 C++17)等等
頭文件:如果您的程序包含不在工作區(qū)或標準庫路徑中的頭文件,您只需要修改Include path Visual Studio Code 將這些設置放在.vscode/c_cpp_properties.json
二、配置文件
Vscode通常需要配置三個文件:
tasks.json
(編譯器配置文件)launch.json
(調試器配置文件)c_cpp_properties.json
(編譯器路徑和intellisense設置)
1、配置文件生成方法
① tasks.json : 編譯器構建 配置文件 ;
終端-配置任務
② launch.json : 調試器設置 配置文件 ;
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
:設置制定要運行的程序,一般為g++args
:用數(shù)組的形式,將指定的g++命令行參數(shù)傳入,必須按照編譯器順序進行制定。本task文件的含義是label
:任務列表中看到的名字,可以任意命名detail
:將在任務列表中作為任務描述的值
修改task.json文件主要修改args部分
- 使用"${workspaceFolder}/*.cpp" 代替${file} ,這將編譯文件夾中所有的CPP文件
- 使用自定義的名字如helloworld.out代替"${fileDirname}/${fileBasenameNoExtension}"
更多args配置參數(shù),可以參考:https://code.visualstudio.com/docs/editor/variables-reference
②launch.json文件解析
{ // 使用 IntelliSense 了解相關屬性。 // 懸停以查看現(xiàn)有屬性的描述。 // 欲了解更多信息,請訪問: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "(gdb) 啟動", "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": "將反匯編風格設置為 Intel", "text": "-gdb-set disassembly-flavor intel", "ignoreFailures": true } ] } ] }
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
VS2019如何創(chuàng)建C++項目的實現(xiàn)示例
這篇文章主要介紹了VS2019如何創(chuàng)建C++項目的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-08-08c語言程序設計文件操作方法示例(CreateFile和fopen)
c主要的文件操作函數(shù)有:CreateFile,CloseHandle,ReadFile,WriteFile,SetFilePointer,GetFileSize。其中的讀寫操作是以字符為單位,獲得文件大小也是以字符為單位。2013-12-12C語言實現(xiàn)學生信息管理系統(tǒng)(多文件)
這篇文章主要為大家詳細介紹了C語言實現(xiàn)學生信息管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-12-12