You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu下Qt-QWT项目编译报错,请求解决方法

问题描述

在Ubuntu系统中搭建Qt-QWT项目,执行了以下操作:

  • 编译安装QWT:
    qmake qwt.pro
    make
    sudo make install
    
  • 配置环境:
    1. 在.bashrc中添加:
      export QWT_ROOT="/usr/local/qwt-6.2.0"
      QT_PLUGIN_PATH="${QWT_ROOT}/plugins:$QT_PLUGIN_PATH"
      export QT_PLUGIN_PATH
      
    2. 在/etc/ld.so.conf.d/qwt.conf中添加行:/usr/local/qwt-6.2.0/lib
  • 设置QMAKEFEATURES:
    qmake -set QMAKEFEATURES ${QWT_ROOT}/features
    
  • 创建Qt Quick项目,相关文件代码如下:

    DeleteMe.pro

    QT += quick
    
    #add QWT
    CONFIG += qwt
    
    INCLUDEPATH += /usr/local/qwt-6.2.0/include
    LIBS += -L/usr/local/qwt-6.2.0/lib -lqwt
    
    SOURCES += \
            main.cpp
    ...
    

    main.cpp

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    
    #include <QwtPlot>
    #include <QwtText>
    
    int main(int argc, char *argv[])
    {
    #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    #endif
        QGuiApplication app(argc, argv);
    
        QwtPlot plot(QwtText("Qwt example"));
        plot.setGeometry(0,0,640, 300);
        plot.show();
    
        return 0;
    }
    

编译时出现以下错误:

:-1: error: main.o: undefined reference to symbol '_ZN7QWidget4showEv@@Qt_5'
:-1: error: /lib/x86_64-linux-gnu/libQt5Widgets.so.5: error adding symbols: DSO missing from command line
解决方案

错误核心原因:QwtPlot是基于Qt Widgets模块的组件,但当前项目仅引入了quick模块,缺少Widgets模块的编译链接依赖,导致无法找到QWidget::show()这类属于Widgets的符号。

修复步骤:

  1. 修改项目配置文件
    打开DeleteMe.pro,将QT += quick修改为QT += quick widgets,添加Widgets模块的依赖。

  2. 调整主程序代码
    因为使用了QWidget类,需要用专门支持Widget应用的QApplication替代QGuiApplication:

    • 替换头文件<QGuiApplication>为<QApplication>
    • 将QGuiApplication app(argc, argv);改为QApplication app(argc, argv);
    • 最后返回app.exec()(确保事件循环正常启动)

修改后的文件示例:

修改后的DeleteMe.pro

QT += quick widgets

#add QWT
CONFIG += qwt

INCLUDEPATH += /usr/local/qwt-6.2.0/include
LIBS += -L/usr/local/qwt-6.2.0/lib -lqwt

SOURCES += \
        main.cpp
...

修改后的main.cpp

#include <QApplication>
#include <QQmlApplicationEngine>

#include <QwtPlot>
#include <QwtText>

int main(int argc, char *argv[])
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
    QApplication app(argc, argv);

    QwtPlot plot(QwtText("Qwt example"));
    plot.setGeometry(0,0,640, 300);
    plot.show();

    return app.exec();
}
  1. 重新编译项目
    执行qmake和make命令,重新编译即可正常运行。

额外说明:如果不需要Qt Quick模块,可以直接将项目改为纯Widget项目,仅保留QT += widgets,并移除QQmlApplicationEngine相关代码,项目会更轻量化。

内容的提问来源于stack exchange,提问作者Kto To

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 18:45:42