Set include path for Qt moc files using CMake



  • I'm trying to use CMake with Qt to deploy on Linux and Windows, but I can't manage to make a basic project architecture, with an src and include directory. Here's an example :

    .
    ├── CMakeLists.txt
    ├── include
    │   └── LoginUI.hpp
    └── src
         ├── login.ui
         ├── LoginUI.cpp
         └── main.cpp
    

    Here's the content of my CMakeLists.txt

    cmake_minimum_required(VERSION 2.8.11)
    
    project(testproject)
    
    set(SRCS
        src/main.cpp
        src/LoginUI.cpp)
    
    set(INCLUDE_DIR
        include
        .)
    
    set(HEADERS
        include/LoginUI.hpp)
    
    if (UNIX)
        set(CMAKE_PREFIX_PATH "/opt/Qt5.9.2/5.9.2/gcc_64/lib/cmake")
    endif (UNIX)
    
    # Find includes in corresponding build directories
    set(CMAKE_INCLUDE_CURRENT_DIR ON)
    # Instruct CMake to run moc automatically when needed.
    set(CMAKE_AUTOMOC ON)
    set(CMAKE_AUTOUIC ON)
    
    # Find the QtWidgets library
    find_package(Qt5Widgets)
    
    # Tell CMake to create the helloworld executable
    add_executable(helloworld ${SRCS})
    target_include_directories(helloworld PUBLIC ${INCLUDE_DIR})
    
    # Use the Widgets module from Qt 5.
    target_link_libraries(helloworld Qt5::Widgets)
    

    When I run cmake, it works perfectly, but when I try to run 'make', there's an error showing that it searches for a header (here, LoginUI.hpp) in the 'src/' directory. I want it to search in the 'include/' directory. Is it possible ?

    Here's the exact error message :

    [ 20%] Automatic moc and uic for target helloworld
    AUTOGEN: error: .../src/LoginUI.cpp The file includes the moc file "moc_LoginUI.cpp", but could not find header "LoginUI{.h,.hh,.h++,.hm,.hpp,.hxx,.in,.txx}" in .../src/
    




  • Thanks for your answer, it worked nicely !


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.