为什么cmake没有调整我的Q_OBJECT标头?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了为什么cmake没有调整我的Q_OBJECT标头?相关的知识,希望对你有一定的参考价值。
我目前有一个像这样设置的项目:
- 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
decalred的标头。我可以告诉,因为这是我的构建的样子:
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%的位置,就会说它会自动调整,但它没有列出任何文件,这意味着它没有找到任何文件。另外,由于没有发生moccing,因此存在链接错误,因为信号不存在。我在CMakeLists.txt
中遗漏了什么,或者我是否需要更改项目的结构?如何让MOC找到我的头文件?
答案
AUTOMOC
属性告诉CMake扫描头文件并在必要时生成moc命令。如果CMake不知道头文件,它将不会扫描它。所以你需要做的就是将标题和源一起传递给add_executable
函数。
之后,删除qt5_wrap_cpp
功能。只要您使用AUTOMOC
,就不需要了。
并删除qt5_use_modules
功能。现代CMake可以通过简单的target_link_libraries(${PROJECT_NAME} Qt5Quick Qt5Core Qt5Qml Qt5OpenGL)
为您完成所有这些
以上是关于为什么cmake没有调整我的Q_OBJECT标头?的主要内容,如果未能解决你的问题,请参考以下文章
CMake - 同一项目中的应用程序/库之间的依赖项(标头)
cmake qt5_generate_repc() 将 rep 标头放在 CMAKE_CURRENT_BINARY_DIR