Ubuntu vscode 配置 qt qmake开发环境

以下是在 Ubuntu 中配置 VSCode + Qt + qmake 开发环境的详细步骤:


1. 安装 Qt 开发环境

1.1 安装 Qt 库和工具

# 更新软件源
sudo apt update

# 安装 Qt5 核心库及开发工具(如需要 Qt6,需另从官网安装)
sudo apt install qtbase5-dev qt5-qmake qtchooser qtcreator

# 可选:安装其他模块(如GUI模块)
sudo apt install qtdeclarative5-dev qttools5-dev

1.2 验证 qmake

# 查看 qmake 版本及路径
qmake --version

# 若返回类似 "QMake version 3.1..." 则为成功

2. 安装 VSCode

2.1 下载并安装 VSCode

# 下载 .deb 包
wget -O vscode.deb "https://code.visualstudio.com/sha/download?build=stable&os=linux-deb-x64"

# 安装
sudo apt install ./vscode.deb

2.2 安装必备扩展

  1. C/C++(微软官方扩展):提供代码补全、调试支持
  2. Qt Tools:Qt语法高亮、QML支持
  3. Code Runner(可选):快速运行代码

3. 配置 VSCode 的 Qt 项目

3.1 创建或打开 Qt 项目

假设项目目录结构如下:

.
├── main.cpp
├── widget.cpp
├── widget.h
└── myproject.pro

3.2 配置 .vscode 文件夹

在项目根目录创建 .vscode 文件夹,并添加以下文件:

tasks.json(构建任务配置)

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "qmake build",
            "type": "shell",
            "command": "qmake",
            "args": ["${workspaceFolder}/myproject.pro"],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        {
            "label": "make",
            "type": "shell",
            "command": "make",
            "group": "build",
            "dependsOn": ["qmake build"]
        },
        {
            "label": "clean",
            "type": "shell",
            "command": "make clean",
            "group": "build"
        }
    ]
}

launch.json(调试配置)

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch Qt Program",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/myproject",  // 编译生成的可执行文件路径
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "preLaunchTask": "make",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

3.3 设置 c_cpp_properties.json(可选)

配置头文件路径和编译指令(避免代码提示报错):

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "/usr/include/x86_64-linux-gnu/qt5/**"  // 根据实际路径调整
            ],
            "defines": [],
            "compilerPath": "/usr/bin/g++",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "linux-gcc-x64"
        }
    ],
    "version": 4
}

4. 编写并运行项目

4.1 按 Ctrl+Shift+B 执行构建任务

  • 选择 make 自动执行 qmake 生成 Makefile,并编译项目。

4.2 调试

F5 启动调试,VSCode会自动编译并运行程序。


5. 常见问题解决

问题 1:无法找到 Qt 头文件

  • 解决:检查 includePath 中的 Qt 头文件路径是否正确,可通过终端查找:
    dpkg -L qtbase5-dev | grep .h
    

问题 2:qmake 未找到

  • 解决:确保已安装 qt5-qmake,或手动指定路径:
    # 查找 qmake 路径
    which qmake
    

问题 3:调试时提示权限不足

  • 解决:为生成的可执行文件添加执行权限:
    chmod +x myproject
    

6. 补充工具(可选)

  • Clang-Format:统一代码风格
  • CMake Tools:如果需要切换为 CMake 构建
  • QML Linter:QML语法检查

按上述步骤完成配置后,即可在 VSCode 中高效开发 Qt 应用!

posted @ 2025-05-12 15:46  今天昔水  阅读(771)  评论(0)    收藏  举报