用CMakeList 构建Qt 项目 及 遇到的问题yuan

本博文只是介绍在Cmakelist 构建Qt项目时遇到的问题,和对应的解决方案。

构建方法请出门左转,参考博客:https://blog.csdn.net/u013243710/article/details/36005539,写的很好很清楚~

问题1.找不到QT4

CMake Error at can_ui/CMakeLists.txt:121 (find_package):
  By not providing "FindQT4.cmake" in CMAKE_MODULE_PATH this project has
  asked CMake to find a package configuration file provided by "QT4", but
  CMake did not find one.

  Could not find a package configuration file provided by "QT4" with any of
  the following names:

    QT4Config.cmake
    qt4-config.cmake

  Add the installation prefix of "QT4" to CMAKE_PREFIX_PATH or set "QT4_DIR"
  to a directory containing one of the above files.  If "QT4" provides a
  separate development package or SDK, be sure it has been installed.

解决方法:

修改

find_package(QT4 REQUIRED)

find_package(Qt4 REQUIRED)

大写Q,小写t。看清楚哦!!!!!!!

问题2:编译时 报错: error: undefined reference to ‘vtable for Widget'或‘vtable for XXX’

错误原因:没有正确的使用 moc。是CMakeLists.txt的问题

我的CMakeLists.txt是这么写的

find_package(Qt4 REQUIRED)
include(${QT_USE_FILE})
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR})
add_definitions(${QT_DEFINITIONS})

SET(can_ui_SOURCES src/can_ui.cpp src/widget.cpp)
SET(can_ui_HEADERS src/widget.h)
SET(can_ui_FORMS src/widget.ui)
#SET(CAN_UI_QRCS can_ui.qrc)
#SET(CAN_UI_SRCS can_ui.cpp widget.cpp ${CAN_UI_QRCS})


QT4_WRAP_CPP(can_ui_HEADERS_MOC ${can_ui_HEADERS})
QT4_WRAP_UI(can_ui_FORMS_HEADERS ${can_ui_FORMS})
#QT4_ADD_RESOURCES (CAN_UI_QRC_RCC     ${CAN_UI_QRCS})

add_executable(can_ui ${can_ui_SOURCES}
                      ${can_ui_HEADERS}
                      ${can_ui_FORMS})
#                      ${CAN_UI_QRC_RCC})

target_link_libraries(can_ui ${QT_LIBRARIES}
                             ${catkin_LIBRARIES})

add_dependencies(can_ui can_ui_generate_messages_cpp)

第17-19行 生成可执行文件时add_executable(),依赖的是自己写的源文件,并没有用到qt的moc。这里是有问题的。CMakelists并不能根据这些文件生成正常的可执行文件。

正确的做法是:依赖通过QT宏(QT4_WRAP_CPP,QT4_WRAP_UI等)生成的文件。

修改如下

find_package(Qt4 REQUIRED)
include(${QT_USE_FILE})
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR})
add_definitions(${QT_DEFINITIONS})

SET(can_ui_SOURCES src/can_ui.cpp src/widget.cpp)
SET(can_ui_HEADERS src/widget.h)
SET(can_ui_FORMS src/widget.ui)
#SET(CAN_UI_QRCS can_ui.qrc)
#SET(CAN_UI_SRCS can_ui.cpp widget.cpp ${CAN_UI_QRCS})


QT4_WRAP_CPP(can_ui_HEADERS_MOC ${can_ui_HEADERS})
QT4_WRAP_UI(can_ui_FORMS_HEADERS ${can_ui_FORMS})
#QT4_ADD_RESOURCES (CAN_UI_QRC_RCC     ${CAN_UI_QRCS})
add_executable(can_ui ${can_ui_SOURCES}
                      ${can_ui_HEADERS_MOC}
                      ${can_ui_FORMS_HEADERS})
#                      ${CAN_UI_QRC_RCC})
target_link_libraries(can_ui ${QT_LIBRARIES}
                             ${catkin_LIBRARIES})

add_dependencies(can_ui can_ui_generate_messages_cpp)

编译成功!

问题3 CMakeLists工程 不能使用debug调试

解决方法:在工程CMakeLists.txt 中添加以下内容

#如果要生成debug版本
SET(CMAKE_BUILD_TYPE "Debug")
#如果要生成release版本
#SET(CMAKE_BUILD_TYPE "Release")
#添加的支持debug和release支持
SET(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g -ggdb")
SET(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")

删除原来的.user文件,并重新编译。恭喜你可以debug啦~

问题4 已经定义的SLOT无法识别

Object::connect: No such slot QWidget::cansendSendButtonSlot()

原因:

           在Qt中,如果一个类要使用信号/槽功能,就必须在其中声明Q_OBJECT

           所以Q_OBJECT不可以去掉

解决方法:

           在.h文件的class类中添加

    Q_OBJECT

你可能感兴趣的:(Qt,CMakeList)