为什么cmake不移动我的Q_OBJECT标头?

时间:2018-09-19 19:56:14

标签: qt cmake qt5 qtquick2 moc

我目前有一个这样的项目:

- project/
--- include/
--- src/
--- qml/
--- CMakeLists.txt
--- qml.qrc

还有一个CMakeLists.txt

cmake_minimum_required(VERSION 3.1)

cmake_policy(SET CMP0071 NEW)

include_directories(${Qt5Widgets_INCLUDE_DIRS} ${QtQml_INCLUDE_DIRS})
add_definitions(${Qt5Widgets_DEFINITIONS} ${QtQml_DEFINITIONS} ${${Qt5Quick_DEFINITIONS}})

set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

project(wormhole_qt)

find_package(Qt5 COMPONENTS Core Quick Qml OpenGL REQUIRED)
find_package(Qt5QuickCompiler)

find_package(VTK REQUIRED)
include(${VTK_USE_FILE})

include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})

MACRO(HEADER_DIRECTORIES sub_dir return_list)
    FILE(GLOB_RECURSE new_list ${sub_dir}/*.h)
    SET(dir_list "")
    FOREACH(file_path ${new_list})
        GET_FILENAME_COMPONENT(dir_path ${file_path} PATH)
        SET(dir_list ${dir_list} ${dir_path})
    ENDFOREACH()
    LIST(REMOVE_DUPLICATES dir_list)
    SET(${return_list} ${dir_list})
ENDMACRO()

HEADER_DIRECTORIES(include mylist)
include_directories(include)
include_directories(lib)
include_directories(${mylist})

file(GLOB INCLUDES *.h)
file(GLOB_RECURSE CPP_FILES src/*.cpp)

qtquick_compiler_add_resources(QT_RESOURCES qml.qrc)
qt5_wrap_cpp(MOC_SOURCE ${INCLUDES})
add_executable(${PROJECT_NAME} MACOSX_BUNDLE ${MOC_SOURCE} ${CPP_FILES} ${QT_RESOURCES})

target_link_libraries(${PROJECT_NAME} ${VTK_LIBRARIES})
qt5_use_modules(${PROJECT_NAME} Quick Core Qml OpenGL)

问题在于,在实际的构建步骤中,moc找不到我的任何带有Q_OBJECT贴花的标头。我可以知道,因为这就是我的构建:

14:18:45: Running steps for project wormhole_qt...
14:18:45: Starting: "/usr/local/bin/cmake" --build . --target all
[  1%] Generating qmlcache_loader.cpp
[  2%] Generating qml_Empty_qml.cpp
[  4%] Generating qml_Style_qml.cpp
[  5%] Generating qml_main_qml.cpp
[  7%] Generating qml_setup_NewProject_qml.cpp
[  8%] Generating qml_setup_Setup_qml.cpp
[  9%] Generating qml_setup_Startup_qml.cpp
[ 11%] Generating qml_workspace_AnimationWindow_qml.cpp
[ 12%] Generating qml_workspace_DepthChartControls_qml.cpp
[ 14%] Generating qml_workspace_NewAnimationWindow_qml.cpp
[ 15%] Generating qml_workspace_TraceWindow_qml.cpp
[ 16%] Generating qml_workspace_WorkspaceMenuBar_qml.cpp
[ 18%] Generating qml_workspace_Workspace_qml.cpp
[ 19%] Automatic MOC for target wormhole_qt
[ 19%] Built target wormhole_qt_autogen
[ 21%] Generating qrc_qml_qmlcache.cpp
Scanning dependencies of target wormhole_qt
[ 22%] Building CXX object CMakeFiles/wormhole_qt.dir/src/controllers/AnimationsController.cpp.o
[ 23%] Building CXX object CMakeFiles/wormhole_qt.dir/src/controllers/ApplicationController.cpp.o

如果您看到19%的位置,则表明它正在自动移动,但是它没有列出任何文件,这意味着它找不到任何文件。此外,由于未发生移动,因此存在连接错误,因为信号不存在。我在CMakeLists.txt中是否缺少某些内容,还是需要更改项目的结构?如何使MOC找到我的头文件?

1 个答案:

答案 0 :(得分:0)

AUTOMOC属性告诉CMake扫描头文件并在必要时生成moc命令。如果CMake不知道头文件,则不会扫描它。因此,您要做的就是将标头以及源传递给add_executable函数。

此后,删除qt5_wrap_cpp函数。只要使用AUTOMOC,就不需要。

并删除qt5_use_modules函数。现代CMake可以使用简单的target_link_libraries(${PROJECT_NAME} Qt5Quick Qt5Core Qt5Qml Qt5OpenGL)

为您完成所有这些工作