如何在没有QtCreator的情况下编译Qt 5项目?

时间:2015-03-13 19:48:13

标签: c++ eclipse cmake qt5 kdevelop

我尝试修改KDevelop Qt 4 CMake项目,用Qt 5编译它并改变了项目,但我失败了。 我还在学习Qt,项目很简单。 替换了注释行以使其成为Qt 5项目。

main.cpp中:

//#include <QtGui/QApplication>
#include <qt5/QtWidgets/QApplication>
#include "Qt4_Training.h"

int main(int argc, char** argv)
{
    QApplication app(argc, argv);
    Qt4_Training qt4_training;
    qt4_training.show();
    return app.exec();
}

Qt4_Training.cpp:

#include "Qt4_Training.h"

/*#include <QtGui/QLabel>
#include <QtGui/QMenu>
#include <QtGui/QMenuBar>
#include <QtGui/QAction>
#include <QtGui/QPushButton>
#include <QTime>*/
#include <qt5/QtWidgets/QLabel>
#include <qt5/QtWidgets/QMenu>
#include <qt5/QtWidgets/QMenuBar>
#include <qt5/QtWidgets/QAction>
#include <qt5/QtWidgets/QPushButton>
#include <qt5/QtCore/QTime>

Qt4_Training::Qt4_Training()
{
    QLabel *label = new QLabel;
    vbox = new QVBoxLayout;

    label.setText("Hi.");
    vbox->addWidget(label);
    QWidget *a = new QWidget;
    a->setLayout(vbox);
    setCentralWidget(a);

    QAction* action = new QAction(this);
    action->setText( "Quit");
    connect(action, SIGNAL(triggered()), SLOT(close()));
    menuBar()->addMenu( "File" )->addAction(action);
}

Qt4_Training::~Qt4_Training() {}

//#include "Qt4_Training.moc"

Qt4_Training.h:

#ifndef Qt4_Training_H
#define Qt4_Training_H

/*#include <QtGui/QMainWindow>
#include <QtGui/QVBoxLayout>*/
#include <qt5/QtWidgets/QMainWindow>
#include <qt5/QtWidgets/QVBoxLayout>

class Qt4_Training : public QMainWindow
{
    //Q_OBJECT

public:
    QVBoxLayout *vbox;
    Qt4_Training();
    virtual ~Qt4_Training();
};

#endif // Qt4_Training_H

的CMakeLists.txt:

cmake_minimum_required(VERSION 3.0.2)
project(qt4_training)
#find_package(Qt4 REQUIRED)
find_package(Qt5Widgets REQUIRED)
find_package(Qt5Core REQUIRED)

#include_directories(${QT_INCLUDES} ${CMAKE_CURRENT_BINARY_DIR})
include_directories(${Qt5Widgets_INCLUDE_DIRS} ${Qt5Widgets_LIBRARIES} ${CMAKE_CURRENT_BINARY_DIR} ${Qt5Core_INCLUDE_DIRS} ${Qt5Core_LIBRARIES})

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${Qt5Widgets_EXECUTABLE_COMPILE_FLAGS}")
set(Qt4_Training_SRCS Qt4_Training.cpp main.cpp)

#qt4_automoc(${Qt4_Training_SRCS})
add_executable(qt4_training ${Qt4_Training_SRCS})
target_link_libraries(qt4_training ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY})

install(TARGETS qt4_training RUNTIME DESTINATION bin)

我也尝试使用Eclipse并使用'#include',但所有对象名称必须以'Private'结尾,如'QLabelPrivate ...;'。

KDevelop错误:

...
CMakeFiles/qt4_training.dir/main.cpp.o: In function `main':
main.cpp:(.text.startup+0x37): undefined reference to `QApplication::QApplication(int&, char**, int)'
main.cpp:(.text.startup+0x47): undefined reference to `QWidget::show()'
main.cpp:(.text.startup+0x4c): undefined reference to `QApplication::exec()'
main.cpp:(.text.startup+0x5f): undefined reference to `QApplication::~QApplication()'
main.cpp:(.text.startup+0x96): undefined reference to `QApplication::~QApplication()'
collect2: error: ld returned 1 exit status
CMakeFiles/qt4_training.dir/build.make:110: recipe for target 'qt4_training' failed
make[2]: *** [qt4_training] Error 1
CMakeFiles/Makefile2:60: recipe for target 'CMakeFiles/qt4_training.dir/all' failed
make[1]: *** [CMakeFiles/qt4_training.dir/all] Error 2
Makefile:117: recipe for target 'all' failed
make: *** [all] Error 2
*** Failure: Exit code 2 ***

Eclipse错误:

In file included from ../src/Qt 5 Training.cpp:1:0:
/usr/include/x86_64-linux-gnu/qt5/QtWidgets/qapplication.h:37:37: fatal error: QtCore/qcoreapplication.h: No such file or directory
 #include <QtCore/qcoreapplication.h>
                                     ^
compilation terminated.
src/subdir.mk:21: recipe for target 'src/Qt 5 Training.o' failed
make: *** [src/Qt 5 Training.o] Error 1

我在Kubuntu上安装了libqt5widgets5,qtdeclarative5-dev,libqt5gui5,qtbase5-dev,...我有Qt 5吗?

1 个答案:

答案 0 :(得分:0)

http://www.cmake.org/cmake/help/v3.0/manual/cmake-qt.7.html

查看cmake-qt的文档

根据那个 target_link_libraries(qt4_training Qt5::Widgets)

可能会修复您的链接器错误。

您的包含目录也看起来不对,您不应在此处添加LIBRARIES变量