为什么 cmake 不移动我的 Q_OBJECT 标头? [英] Why is cmake not mocing my Q_OBJECT headers?
问题描述
我目前有一个这样设置的项目:
I currently have a project set up like this:
- 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
的标头.我可以说,因为这就是我的构建的样子:
The problem is that in the actual build step, moc doesn't find any of my headers that have Q_OBJECT
decalred in them. I can tell because this is what my build looks like:
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 找到我的头文件?
If you look where it says 19%, it says that it is automatically moccing, but it doesn't list any files, which means it isn't finding any. In addition, because of the moccing not occurring, there is a linking error because the signals don't exist. Am I missing something in my CMakeLists.txt
, or do I need to change up the structure of my project? How do I make MOC find my header files?
推荐答案
AUTOMOC
属性告诉 CMake 扫描头文件并在必要时生成 moc 命令.如果 CMake 不知道头文件,它就不会扫描它.因此,您需要做的就是将标头和源代码一起传递给 add_executable
函数.
然后删除 qt5_wrap_cpp
函数.只要您使用 AUTOMOC
,就不需要.
After that remove the qt5_wrap_cpp
function. You don't need as long as you're using AUTOMOC
.
并删除 qt5_use_modules
功能.现代 CMake 可以通过简单的 target_link_libraries(${PROJECT_NAME} Qt5Quick Qt5Core Qt5Qml Qt5OpenGL)
And remove the qt5_use_modules
function. Modern CMake can do all of this for you with plain target_link_libraries(${PROJECT_NAME} Qt5Quick Qt5Core Qt5Qml Qt5OpenGL)
这篇关于为什么 cmake 不移动我的 Q_OBJECT 标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!