Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Installation and Deployment
  4. Set include path for Qt moc files using CMake
Forum Update on Monday, May 27th 2025

Set include path for Qt moc files using CMake

Scheduled Pinned Locked Moved Solved Installation and Deployment
3 Posts 2 Posters 7.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sugiuro
    wrote on last edited by
    #1

    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/
    
    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      https://stackoverflow.com/questions/13703647/how-to-properly-add-include-directories-with-cmake

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      2
      • S Offline
        S Offline
        Sugiuro
        wrote on last edited by
        #3

        Thanks for your answer, it worked nicely !

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved