【问题标题】:How to get AUTOMOC to find header file in QT5如何让AUTOMOC在QT5中找到头文件
【发布时间】:2018-10-17 03:58:42
【问题描述】:

我使用的是 Qt 5,我将所有头文件放在单独的包含文件夹中,而 AUTOMOC 无法找到我的头文件。

src
 |- sim
     |- include
         |- sim
            |- client.h (contains Q_OBJECT)
     |- client.cpp

我的 CMake 代码:

set(CMAKE_AUTOMOC ON)
find_package(Qt5 COMPONENTS Core Widgets REQUIRED)
add_library(client client.cpp)
target_include_directories(client PUBLIC
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
        $<INSTALL_INTERFACE:include>)
target_link_libraries(client sim Qt5::Widgets)

这会导致对我的信号的未定义引用,因为未生成客户端中的 QObject MOC 类。我尝试添加它以强制它搜索 client.h...

#include "moc_client.cpp"

然后我得到错误

AutoMoc error
-------------
  "/home/peter/projects/smartmouse/smartmouse-simulator/src/sim/client.cpp"
The file includes the moc file "moc_client.cpp", but the header "client.{h,hh,h++,hm,hpp,hxx,in,txx}" could not be found.

我明白为什么会这样,因为文档只说它搜索当前文件夹,所以它不会在 include/sim 文件夹中查找。我的问题是如何让 AUTOMOC 找到 include/sim/client.h?

我也尝试过使用 qt5_wrap_cpp 但它似乎并没有真正调用 moc 编译器,它只是设置了一些信息。我试过了:

qt5_wrap_cpp(client include/sim/client.h)

但据我所知,它没有生成任何 cmake 目标

编辑:

我还在AutogenInfo.cmake 文件中注意到没有找到标题:set(AM_HEADERS "")

【问题讨论】:

    标签: qt cmake automoc


    【解决方案1】:

    我刚刚遇到了这个类似的问题,虽然我对这个答案不满意,但它可能会有所帮助。

    CMAKE_AUTOMOC 没有接收到client.cpp#include "sim/client.h",但您可以手动将其添加到目标源:

    add_library(client client.cpp include/sim/client.h)

    【讨论】:

      猜你喜欢
      • 2014-02-23
      • 1970-01-01
      • 1970-01-01
      • 2015-04-14
      • 2016-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多