如何使用gnu autotool构建qt项目

时间:2016-06-10 23:56:21

标签: c++ qt makefile

解决
我最终使用以下设置编译程序

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

项目结构
根/引导
根/ configure.ac
根/ Makefile.am
根/ SRC /
----根/ SRC / firsttry.cpp
----根/ SRC / firsttry.h
----根/ 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

我从这些参考文献中创建了这个项目。

我遇到的问题
编译程序时我得<QApplication> is undefined 任何人都可以给我一步一步的教程吗?

1 个答案:

答案 0 :(得分:0)

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