Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add include directories to AUTOMOC

I have a ROS package that includes QT4 GUIs. My code is in the folder Project_name/src/test/*.cpp and my includes in Project_name/include/test/*.h

Some qt4 mocs must be created as some header files contain Q_OBJECT in their classes.

I tried the set(CMAKE_AUTOMOC ON) in the cmake file but as it seems it does not search the /include/test/ folder. AUTOMOC states that works either bu searching the source files for moc_**.cpp files or by examining the header files for Q_OBJECT.

I also tried to include a moc_***.cpp in a source file (for example /src/test/a.cpp). So it searched for a.h but could not find it in include/test/a.h.

I must note that if I remove the Q_OBJECT from the classes the compilation succeeds, as the include/ folder is added like this: include_directories( include ${catkin_INCLUDE_DIRS} )

Finally I've tried to use QT4_WRAP_CPP but for some reason it couldn't find the mocs as well and the link failed (although in another project with the same parameters in the cmake file works :/)

Edit : Found a solution. In added in the cpp file:

#include "../../include/test/moc_a.cpp"

and found the .h in include/test.

Though something tells me that it is not the correct way :P

like image 327
Manos Tsardoulias Avatar asked Nov 06 '13 19:11

Manos Tsardoulias


1 Answers

#include "../../include/test/moc_a.cpp" in cpp file works but not well for libraries that may be built sometimes as static libraries within bigger project and sometimes by themselves. The problem is that include directory can be created in a not-suitable location, which pollutes code, causes problems with VCS.

qt_wrap_cpp works best for me. It supports both qt4 and qt5, does not require including moc in cpp file. The syntax:

include_directories(${CMAKE_CURRENT_BINARY_DIR}) # including binary dir is
# necessary only if there are classes with Q_OBJECT macro declared in cpp
# files (these cpp files should also contain `# include "x.moc"` at the end).
set(CMAKE_AUTOMOC ON)
include_directories(${Include_Directories})
set(Sources ${Sources_Path}/a.cpp ${Sources_Path}/b.cpp
     ... ${Sources_Path_z}/z.cpp)
qt_wrap_cpp(${Target_Name} Sources ${Headers_Path}/header1.hpp
            ${Headers_Path_2}/header2.hpp ... ${Headers_Path_N}/headerN.hpp)
add_library(${Target_Name} STATIC ${Sources})
# OR add_executable(${Target_Name} ${Sources})

Naturally, only headers that contain Q_OBJECT macro and are not in the same directory as corresponding sources must be passed to qt_wrap_cpp.

like image 171
vedg Avatar answered Sep 25 '22 20:09

vedg