【问题标题】:How to use gnu autotool to build a qt project如何使用gnu autotool构建qt项目
【发布时间】:2016-06-10 23:56:20
【问题描述】:

已解决
我终于用以下设置编译了程序。


我是 autotool 的新手,我想通过 autotool 构建一个 qt 项目。


项目结构
根/引导
根目录/configure.ac
根/Makefile.am
根/src/
----root/src/firsttry.cpp
----root/src/firsttry.h
----root/src/Makefile.am

root/src/firsttry.cpp

#include <QApplication>
#include <QLabel>

int main(int argc, char *argv[]) {
     QApplication app(argc, argv);

     QLabel *label = new QLabel("Hello!World! Orz...");
     label->setWindowTitle("First Qt!");
     label->resize(200, 50);
     label->show();

     return app.exec();
}

root/src/Makefile.am

...在底部添加

# qt project stuff
moc-%.cc: %.h
    @MOC@ -o$@ $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(MOC_CPPFLAGS) $<

ui-%.h: %.ui
    @UIC@ -o $@ $<

qrc-%.cc: %.qrc
    @RCC@ -o $@ $<

root/configure.ac
在 configure.ac 文件中添加

# Check for Qt libraries
PKG_CHECK_MODULES(QT, [QtCore, QtGui, QtNetwork], [], [AC_MSG_ERROR([Qt libraries are required.])])

# Retrieve Qt compilation and linker flags
CPPFLAGS="`$PKG_CONFIG --cflags-only-I QtCore QtGui QtNetwork` $CPPFLAGS"
LDFLAGS="`$PKG_CONFIG --libs-only-L QtCore QtGui QtNetwork` $LDFLAGS"
LIBS="`$PKG_CONFIG --libs-only-l QtCore QtGui QtNetwork` $LIBS"

if ! `$PKG_CONFIG --atleast-version=4.6.0 QtCore`; then
   AC_MSG_ERROR([Qt >= 4.6.0 is required.])
fi

AC_CHECK_PROGS(MOC, [moc-qt5 moc-qt4 moc])
AC_CHECK_PROGS(UIC, [uic-qt5 uic-qt4 uic])
AC_CHECK_PROGS(RCC, [rcc])
 if test -z "$MOC" || test -z "$UIC" || test -z "$RCC"; then
   AC_MSG_ERROR([Qt utility programs moc, uic, and rcc are required.])
fi

我根据这些参考资料制作了这个项目。

我遇到的问题
我在编译程序时得到了&lt;QApplication&gt; is undefined。
谁能给我一个分步教程?

【问题讨论】:

  • 为什么不使用一般的qt构建过程qmake-make?不要忘记添加到您的 .pro 文件中QT += core gui
  • 无论如何检查链接器可用的 qt 库的构建配置 (-L /path/ ) 并添加 ( -lQtGui ) 到编译命令。
  • 我会考虑使用 CMake 或 QMake 而不是在 2016 年学习自动工具...
  • 不抱歉。我只做了这个没有 UI 的项目(它是一个由 EXTjs 管理的 webUI)。
  • 覆盖这些标志是错误的。将QT_CPPFLAGS 与AC_SUBST 一起使用,并在Makefile.am 中使用该变量。

标签: c++ qt makefile


【解决方案1】:

pkg-config 不是最好的工具,Qt5+ 有一个 contrib M4 宏 https://www.gnu.org/software/autoconf-archive/ax_have_qt.html

然后在你的 Makefile.am 中:

QT_CXXFLAGS = @QT_CXXFLAGS@
QT_LIBS     = @QT_LIBS@
MOC         = @QT_MOC@
UIC         = @QT_UIC@
RCC         = @QT_RCC@

.hpp_moc.cpp:
    $(MOC) -o$@ $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(CPPFLAGS) $(QT_CXXFLAGS) $<

ui_qprojectm_mainwindow.h: qprojectm_mainwindow.ui
    $(UIC) -o $@ $<

ui_qpreseteditordialog.h: qpreseteditordialog.ui
    $(UIC) -o $@ $<

ui_qprojectmconfigdialog.h: qprojectmconfigdialog.ui
    $(UIC) -o $@ $<

application_qrc.cpp: application.qrc
    $(RCC) -o $@ $<

并将$(QT_CXXFLAGS) 添加到您的CFLAGS 和$(QT_LIBS) 到您的LDFLAGS

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-04
    • 2019-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-05
    相关资源
    最近更新 更多