4

Qt with CMake often leads to trouble during linking.

After reading several posts and tries I found out that AUTOMOC takes source files given with add_executable but ignores my header files inside the include folder given with include_directories( inc ). After adding the header filed to add_executable the build was successful.

But what is the correct way to tell AUTOMOC the header files?

In my CMake CMakeLists.txt I use

set(CMAKE_AUTOMOC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
include_directories( inc )

set(QT5_MODULES Widgets)
find_package(Qt5 REQUIRED COMPONENTS ${QT5_MODULES})

add_definitions( -DQT_NO_KEYWORDS
                 -DQT_CORE_LIB 
                 -DQT_GUI_LIB 
                 -DQT_WIDGETS_LIB
                 -DQT_NO_DEBUG 
                 -DROSCONSOLE_BACKEND_LOG4CXX
                 -fPIC)

add_executable( MyGui
                src/main.cpp 
                inc/gui.h src/gui.cpp )

ament_target_dependencies( MyGui )
qt5_use_modules(MyGui ${QT5_MODULES})
Hayt
  • 5,210
  • 30
  • 37
Alex44
  • 3,597
  • 7
  • 39
  • 56
  • 1
    if you put the header in the src directory not as a sourcefile in `add_executable` does that work? – Hayt Oct 06 '16 at 11:15
  • This may also be changed in a newer cmake version. Cmake 3+ has in the documentation that header files with the same basename are automatically parsed for the `QOBJECT` Makro. Which cmake version are you running? – Hayt Oct 06 '16 at 11:21
  • I run CMake 3.5 – Alex44 Oct 06 '16 at 12:52
  • And does it work if you have the header in the src directory? Seems like a bug then or that cmake does indeed not parse include directories. – Hayt Oct 06 '16 at 12:54
  • @Hayt Yes. I moved the *.h to the *src/*-folder and removed the entry on `add_executable`. Then it works. But this destroy the idea behind the separation of headers and sources. – Alex44 Oct 06 '16 at 13:05
  • I cannot test this locally so sorry for having to try my stuff :( can you try `target_include_directories(MyGui PUBLIC inc)` after your `add_executable` call? I believe cmake reads the include dirs from target specific ones. – Hayt Oct 06 '16 at 13:09
  • @Hayt That doesn't work. – Alex44 Oct 06 '16 at 13:14
  • In the end, what is wrong with adding the headers to `add_executable`? They are part of the sources after all, and if someday you use an IDE-based generator this will make the header files visible and editable in the IDE. That is the correct way to declare them, and it works natively with `AUTOMOC`. BTW, always use `target_*`commands, like `target_include_directories`, when possible to avoid global pollution – rocambille Oct 07 '16 at 07:44

0 Answers0