使用CMake

时间:2016-03-21 21:58:19

标签: c++ qt cmake moc

我正在尝试编写一个OpenCV应用程序,用Qt创建GUI并使用CMake生成makefile。我遇到的问题是,当我尝试执行make命令时,我收到错误:

  

此文件是使用5.2.1中的moc生成的。它不能与此版本Qt的包含文件一起使用。 (摩根太过分了。)

好。所以我想我应该尝试使用qt4而不是qt5的moc(我安装了qt4和qt5)。我确实有/usr/bin/moc-qt4/usr/bin/moc,所以我认为我需要做的只是invoke sudo update-alternatives --config moc,但我得到的只是update-alternatives: error: no alternatives for moc。嗯...

所以我认为问题出在qt版本或我的CMakeLists.txt中,所以这里是我用过的代码:

cmake_minimum_required(VERSION 2.8.9)
project (Vigil)

include_directories(
    build
    src
    inc
    uis
)

######################### Configure OpenCV inclusion ######################### 

FIND_PATH(CV_INCLUDE_DIR cv.h
    /usr/include/opencv
)

MESSAGE(STATUS "OpenCV: CV Path : " ${CV_INCLUDE_DIR})

FIND_PATH(CVAUX_INCLUDE_DIR cvaux.h
    /usr/include/opencv
    /usr/local/include/opencv
    /usr/local/include/opencv/cvaux/include
    /usr/local/include/opencv/include 
)

MESSAGE(STATUS "OpenCV: CVAux Path : " ${CXCORE_INCLUDE_DIR})

FIND_PATH(CXCORE_INCLUDE_DIR cxcore.h
    /usr/include/opencv
    /usr/local/include/opencv
    /usr/local/include/opencv/cxcore/include
)

MESSAGE(STATUS "OpenCV: CXCore Path : " ${CVAUX_INCLUDE_DIR})

FIND_PATH(HIGHGUI_INCLUDE_DIR highgui.h
    /usr/include/opencv
    /usr/local/include/opencv/otherlibs/highgui
    /opt/local/include/opencv
    /usr/local/include/opencv 
)

MESSAGE(STATUS "OpenCV: HighGUI Path : " ${HIGHGUI_INCLUDE_DIR})

FIND_LIBRARY(CV_LIB NAMES opencv_imgproc PATHS
    /usr/lib
    /usr/local/lib
    /usr/lib64
    /usr/local/lib64
)

MESSAGE(STATUS "OpenCV: CV Lib: " ${CV_LIB})     

FIND_LIBRARY(CXCORE_LIB NAMES opencv_core PATHS
    /usr/lib
    /usr/local/lib
    /usr/lib64
)

MESSAGE(STATUS "OpenCV: CXCore Lib: " ${CXCORE_LIB}) 

FIND_LIBRARY(HIGHGUI_LIB NAMES opencv_highgui PATHS
    /usr/lib
    /usr/local/lib
    /usr/lib64
    /usr/local/lib64
)

MESSAGE(STATUS "OpenCV: HighGUI Lib: " ${HIGHGUI_LIB})      

INCLUDE_DIRECTORIES(
    ${HIGHGUI_INCLUDE_DIR}
    ${OPENCV_INCLUDE_DIR}
    ${CXCORE_INCLUDE_DIR}
    ${CVAUX_INCLUDE_DIR}
)

######################### Configure QT inclusion ######################### 
FIND_PACKAGE(Qt5Widgets)
FIND_PACKAGE(Qt4 REQUIRED)
include_directories( ${QT_INCLUDES} )
IF(QT4_FOUND)
    MESSAGE(STATUS "QT found : YES")
ELSE(QT4_FOUND)
    MESSAGE(FATAL_ERROR
    "Cannot build without Qt4.  Please set Qt4_DIR.")
ENDIF(QT4_FOUND)

######################### Add sources, headers and uis ######################### 

set(SOURCES ${SOURCES}
    main.cpp
    VigilWidget.cpp
)

SET( HEADERS ${HEADERS}
    VigilWidget.h
)

SET( UI_FILES
    bugView.ui
)

######################### Run UIC on .ui files #########################

QT4_WRAP_UI( UI_HDRS ${UI_FILES} )
MESSAGE(STATUS "UI_HEADERS: " ${UI_HDRS})
# and finally this will run moc:
#QT4_AUTOMOC(${HEADERS})
QT4_WRAP_CPP( MOC_HEADERS  ${HEADERS} )

#########################  ######################### 

ADD_EXECUTABLE(Vigil ${UI_HDRS} ${SOURCES} ${HEADERS} ${QtApp_RCC_SRCS} ${MOC_HEADERS})

我的源文件和头文件目前还很空,但我也会发布它们。谁知道,也许错误就在那里。

main.cpp中:

#include <iostream>
#include "VigilWidget.h"
//#include <QApplication>

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);
    std::cout << "Testing";
    VigilWidget vigil;
    vigil.show();
}

VigilWidget.cpp:

#include "VigilWidget.h"

VigilWidget::VigilWidget(QWidget *parent)
    : QWidget(parent)
{
    ui.setupUi(this);
}

VigilWidget.h:

#ifndef _BGSUBTRACT_H_
#define _BGSUBTRACT_H_

#include "ui_bugView.h"
//#include <QObject>

class VigilWidget : public QWidget{
 Q_OBJECT

 public:

 private slots:

 signals:

 private slots:

 private:

 Ui::bugView ui;

};
#endif

无论如何,我已经花了两天的时间来完成这一切,真的非常感谢一些帮助!非常感谢!

1 个答案:

答案 0 :(得分:1)

配置Qt5:

set(QT_VERSION_REQ "5.2")
find_package(Qt5Core ${QT_VERSION_REQ} REQUIRED)
find_package(Qt5Quick ${QT_VERSION_REQ} REQUIRED)
find_package(Qt5Widgets ${QT_VERSION_REQ} REQUIRED)
set(CMAKE_AUTOMOC ON)

add_executable(${PROJECT_NAME} ${SOURCES})
target_link_libraries(${PROJECT_NAME}
    Qt5::Core
    Qt5::Quick
    Qt5::Widgets     
)

如果可以,最好使用QtQuick而不是QWidgets。