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


最近在Linux上搞Qt, vim環境還用不太習慣, QtCreator之前使用時莫名其妙崩潰然后丟失代碼之后就被我徹底放棄了, 於是研究了一下用VsCode進行Qt開發.

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

Linux系統使用Ubuntu18.04 LTS.

Qt官網下載需要注冊賬號,但實際上官方有不需要注冊賬號的列表下載通道: http://download.qt.io, 在official_releases/qt/分支中找到自己想要的版本,下載 .run 文件即可, 關於Qt環境搭建, 網上有很多教程, 不再贅述.

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

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

 

接下來配置VsCode

在命令行輸入code即可打開vscode編輯器,打開編輯器后創建一個測試文件夾test, 在test目錄中創建如下結構

 

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

dialog.h

 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即可

至此開發環境搭建成功

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM