CMake和Qt5 AUTOMOC错误 [英] CMake and Qt5 AUTOMOC error

查看:170
本文介绍了CMake和Qt5 AUTOMOC错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Qt5的项目,并且有一个用于创建Visual Studio解决方案的 CMakeLists.txt 文件.

I have a project which uses Qt5 and I have a CMakeLists.txt file that I use for creating the Visual Studio Solution.

这是我的 CMakeLists.txt

cmake_policy(SET CMP0020 NEW)
set(CMAKE_AUTOMOC ON)
find_package(Qt5 REQUIRED COMPONENTS core widgets)

set(COMMON_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/src)

include_directories( ${Boost_INCLUDE_DIRS}
    ${COMMON_INCLUDE_DIR}
)


file(GLOB_RECURSE COMMON_SOURCE "*.hpp" "*.cpp")
add_library(${PROJECT_NAME} ${COMMON_SOURCE})
qt5_use_modules(${PROJECT_NAME} Widgets)

当我尝试编译代码时,它返回以下错误:

When I try to compile the code it returns the following error:

>AUTOMOC : error : C:/Users/.../Projects/MyProject/build/MyProjects_automoc.cpp The file includes the moc file "moc_MyFile.cpp", but could not find header "MyFile{.h,.hh,.h++,.hm,.hpp,.hxx,.in,.txx}" in C:/Users/.../Projects/MyProject/build/

moc文件已自动生成,并且标题不在build文件夹中,而是在src目录中的文件夹中.

The moc file have been auto-generated and the header is not in the build folder, but in a folder locate in the src directory.

如何解决此错误?

推荐答案

添加以下内容很好:

set(CMAKE_INCLUDE_CURRENT_DIR ON)

使用 AUTOMOC 功能时.此外,

include_directories(
  ...
  ${QT_USE_FILE}
  ...
)

是一个错误.它应该是:

is a mistake. It should rather be:

include(${QT_USE_FILE})

最后,您应该只显式地将源推入编译,而不能标头!如文档中所述:

Finally, you should only explicitly push sources to compilation, but not headers! As stated in the documentation:

  • 如果 Q_OBJECT foo.h 中(即在头文件中声明了 QObject ),然后在相应的 foo.cpp 中不要忘记添加 #include"moc_foo.cpp" ,最好在最后添加文件的

  • If Q_OBJECT is in the foo.h (i.e. QObject is declared in the header file), then in the corresponding foo.cpp don't forget to add #include "moc_foo.cpp", preferably at the end of the file;

如果 foo.cpp 中包含 Q_OBJECT (即在源文件中声明了 QObject ),然后再次在 foo.cpp 本身中不要忘记添加 #include"foo.moc" ,最好在最后添加文件.

If Q_OBJECT is in the foo.cpp (i.e. QObject is declared in the source file), then, again, in the foo.cpp itself don't forget to add #include "foo.moc", preferably at the end of the file.

因此,请遵循以下建议并进行更改

Therefore, follow these recommendations and change

file(GLOB_RECURSE COMMON_SOURCE "*.hpp" "*.cpp")

file(GLOB_RECURSE COMMON_SOURCE "*.cpp")

您还可以找到 我的其他答案 .您的问题非常相似,因此建议您在下一次发布之前进行更好的搜索.

You could also find my other answer helpful. Your question is very similar, so I'd recommend to search better before posting next time.

祝你好运.

这篇关于CMake和Qt5 AUTOMOC错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆