Linux下使用VsCode進行Qt開發環境搭建

最近在Linux上搞Qt, vim環境還用不太習慣, QtCreator以前使用時莫名其妙崩潰而後丟失代碼以後就被我完全放棄了, 因而研究了一下用VsCode進行Qt開發.c++

首先是系統環境和下載安裝包:shell

Linux系統使用Ubuntu18.04 LTS.json

Qt官網下載須要註冊帳號,但實際上官方有不須要註冊帳號的列表下載通道: http://download.qt.io, 在official_releases/qt/分支中找到本身想要的版本,下載 .run 文件便可, 關於Qt環境搭建, 網上有不少教程, 再也不贅述.vim

VsCode直接官網下載最新版的 .deb, 下載成功使用dpkg -i命令安裝便可.編輯器

安裝成功後輸入qmake --version進行檢查, 出現下圖內容說明Qt環境已經ok了, 若是沒找到qmake命令, 在系統環境變量中添加qmake路徑工具

 

接下來配置VsCode測試

在命令行輸入code便可打開vscode編輯器,打開編輯器後建立一個測試文件夾test, 在test目錄中建立以下結構優化

 

2個cpp和1個header用於測試, 代碼以下ui

dialog.hthis

 1 #ifndef _MAIN_DIALOG_
 2 #define _MAIN_DIALOG_
 3 
 4 #include <QDialog>
 5 #include <QLabel>
 6
 7
 8 
 9 class Dialog : public QDialog
10 {
11   Q_OBJECT
12   
13 public:
14   Dialog(QWidget *parent = 0);
15   ~Dialog();
16 
17 private:
18   QLabel *label_test;
19 }
20 
21 #endif

dialog.cpp

 1 #include <QLayout>
 2 #include "dialog.h"
 3 
 4 Dialog::Dialog(QWidget *parent) : QDialog(parent)
 5 {
 6   this->setWindowTitle("hello");
 7 
 8   label_test = new QLabel(this);
 9   label_test->setText("HelloWorld");
10 
11   QGridLayout *main_layout = new QGridLayout(this);
12   main_layout->addWidget(label_test, 0, 0);
13 }
14 
15 Dialog::~Dialog()
16 {
17 }

main.cpp

 1 #include <QApplication>
 2 #include "dialog.h"
 3 
 4 int main(int argc, char *argv[])
 5 {
 6   QApplication a(argc, argv);
 7   
 8   Dialog dialog;
 9   dialog.show();
10 
11   return a.exec();
12 }

接下來配置.vscode文件夾中的json

先配置tasks.json, 配置一些任務項, 並在生成項目以前先調用這些任務項

 1 {
 2     "version": "2.0.0",
 3     "tasks": [
 4         {
 5             "type": "shell",
 6             "label": "qmake build makefile",
 7             "command": "/home/tsing/Qt5/5.9/gcc_64/bin/qmake",
 8             "args": [],
 9             "options": {},
10             "problemMatcher": [],
11             "group": "build"
12         },
13         {
14             "type": "shell",
15             "label": "make build activefile",
16             "command": "/usr/bin/make",
17             "args": [],
18             "options": {},
19             "problemMatcher": [],
20             "group": "build"
21             "dependsOn": ["qmake build makefile"]
22         }
23     ]
24 }

上面一共配置了兩個任務項

第一個任務項qmake build makefile用於對當前項目生成makefile文件

第二個任務項make build activefile依賴於第一個任務項, 用於針對makefile文件生成當前項目的可執行文件

兩個任務項中"command"標識了要運行的指令的地址, 須要根據本身pc的環境進行調整

第二個任務項使用make指令, 需提早安裝

接下來配置launch.json, 配置vscode運行當前項目的可執行文件前的工做

 1 {
 2     "version": "0.2.0",
 3     "configurations": [
 4         {
 5             "name": "qt build and debug active file",
 6             "type": "cppdbg",
 7             "request": "launch",
 8             "program": "${fileDirname}/${workspaceRootFolderName}",
 9             "args": [],
10             "stopAtEntry": false,
11             "cwd": "${workspaceFolder}",
12             "environment": [],
13             "externalConsole": false,
14             "MIMode": "gdb",
15             "setupCommands": [
16                 {
17                     "description": "爲 gdb 啓用整齊打印",
18                     "text": "-enable-pretty-printing",
19                     "ignoreFailures": true
20                 }
21             ],
22             "prelaunchTask": "make build activefile",
23             "miDebuggerPath": "/usr/bin/gdb"
24         }
25     ]
26 }

launch.json中配置了調試的各項工做, "program"指定了要運行的程序, "prelaunchTask"指定了運行前要執行的task, "miDebuggerPath"指定了調試工具gdb的路徑

目前爲止VsCode還不能對代碼進行智能識別和Qt相關代碼高亮, 配置c_cpp_properties.json以下

 1 {
 2     "version": 4,
 3     "configurations": [
 4         {
 5             "name": "gcc_64",
 6             "intelliSenseMode": "gcc-x64",
 7             "includePath": [
 8                 "/home/tsing/Qt5/5.9/gcc_64/include",
 9                 "/home/tsing/Qt5/5.9/gcc_64/include/QtCore",
10                 "/home/tsing/Qt5/5.9/gcc_64/include/QtGui",
11                 "/home/tsing/Qt5/5.9/gcc_64/include/QtGui",
12                 "/home/tsing/Qt5/5.9/gcc_64/include/QtWidgets",
13                 "${workspaceRoot}"
14             ],
15             "browse": {
16                 "path": [
17                     "/home/tsing/Qt5/5.9/gcc_64/include",
18                     "/home/tsing/Qt5/5.9/gcc_64/include/QtCore",
19                     "/home/tsing/Qt5/5.9/gcc_64/include/QtGui",
20                     "/home/tsing/Qt5/5.9/gcc_64/include/QtGui",
21                     "/home/tsing/Qt5/5.9/gcc_64/include/QtWidgets",
22                     "${workspaceRoot}"
23                 ]
24             },
25             "compilerPath": "/usr/bin/gcc",
26             "cStandard": "c11",
27             "cppStandard": "c++17"
28         }
29     ]
30 }

"includePath"和"path"標定了查找的headers的位置, "compilerPath"標定了分析用的編譯器的路徑, 這裏我使用的gcc編譯器, 須要提早安裝

到目前爲止全部準備工做都已經就緒, 在啓動程序以前要先用qmake生成qt project, 這一步因爲我使用5.X版本的緣由, 須要修改 .pro文件, 因此我沒有把這一步集成到tasks.json裏

首先在命令行界面中將工做目錄切換到test

使用qmake -project命令生成 .pro文件

在 .pro 文件中增長一行內容: QT += widgets core gui

以後F5運行出現對話框

 

切回命令行 tree -a看一下文件目錄結構能看到生成了makefile和qt用的moc打頭的文件

若是須要斷點調試, 清理一下項目(moc打頭的qt宏文件, .o文件, Makefile以及生成的可執行程序), 在.pro文件中添加CONFIG += debug便可

至此開發環境搭建成功

目前只是粗略的搭建了一下環境, 還有不少能夠優化的地方讓開發過程更簡便, 有機會再研究

相關文章
相關標籤/搜索