Hi there,

since it's been a while since my last post - hello to everyone!

In a library code I noticed that the moc-generated cpp files are included at the bottom of the cpp file that matches the respective header used to generate the moc_xxx.cpp. I understand that this speeds up compilation. But also, inside the cpp file there are private class declarations that are needed for the moc-generated code to compile.

Building with Qt-creator is no problem at all - it generates the moc_xxx.cpp files, they are included in the cpp-files, compiled and done. Also, qmake does not attempt to compile the moc_xxx.cpp files themselves!

When running the system through CMake using qt5_wrap_cpp() I get a problem. Example cmake-code:


# collect a list of all header files (to be used in MOC compiler)
file( GLOB LIB_HDRS ${PROJECT_SOURCE_DIR}/../../src/*.h )

# collect a list of all source files, these are also run through the moc compiler
file( GLOB LIB_SRCS ${PROJECT_SOURCE_DIR}/../../src/*.cpp )


# generate mocs from headers
qt5_wrap_cpp( LIB_MOC_SRCS ${LIB_HDRS})

# generate moc-cpps from sources
qt5_wrap_cpp( LIB_MOCCPP_SRCS ${LIB_SRCS})


# add build target for library and use all generated files as dependencies
add_library( ${PROJECT_NAME} STATIC
${LIB_SRCS}
${LIB_MOC_SRCS}
${LIB_MOCCPP_SRCS}
)


CMake will now generate build rules for all generated cpps, resulting in some code being compile twice (giving linker problems) and some moc_xxx.cpp files not being able to compile because the private declarations inside the cpp-files are not available.

Question: what's the best way to get tell cmake that the moc-generated cpps are already included in other cpps and do not need to be compiled?

The variant with handcrafting a custom build target for the mocs alone and adding this "custom target" as dependency to the lib appears to work, but is quite complex. Sure there is a simpler solution, or not?

Thanks,
Andreas