我正在尝试使用cmake构建一个简单的QT5应用程序QT5项目是使用小部件创建新项目时生成的基本项目。该项目与QTCreator成功建立并成功运行widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
cmakelists文件是根据QT5文档中给出的示例编写的。QT5目录的路径在缓存中给出。http://doc.qt.io/qt-5/cmake-manual.html
cmakelists.txt
cmake_minimum_required(VERSION 2.8.11)
project(test0)
# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)
# Find the QtWidgets library
find_package(Qt5Widgets)
# Tell CMake to create the executable
add_executable(test0 WIN32 main.cpp)
# Use the Widgets module from Qt 5.
target_link_libraries(test0 Qt5::Widgets)
Cmake Generation效果很好。
使用CMAKE生成的Makfile构建应用程序时,我会收到链接错误(对属于窗口小部件类的方法的不确定引用)。(这是错误的捕获)http://s31.postimg.org/edefl1m6j/capturetest0.png
有什么提示?
系统:Windows 7
编译器:mingw32
版本:QT 5.6.1(mingw49_32)cmake 3.6.0
cmakelists中的两个错误-Widget.cpp必须在add_executable中提及,如Tsyvarev所述 - 使用autouic来创建与widget.ui和widget.cpp
关联的ui_widget.h工作cmakelists是以下内容:
cmake_minimum_required(VERSION 2.8.11)
project(test0)
# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
# Find the QtWidgets library
find_package(Qt5Widgets)
# Tell CMake to create the executable
add_executable(test0 WIN32 main.cpp widget.cpp widget.ui)
# Use the Widgets module from Qt 5.
target_link_libraries(test0 Qt5::Widgets)