[CMake] Forcibly run 'moc' on Qt files that are NOT part of the build

Michael Jackson mike.jackson at bluequartz.net
Wed Mar 7 12:13:56 EST 2012


On Mar 7, 2012, at 12:05 PM, Michael Wild wrote:

> On 03/07/2012 04:10 PM, Michael Jackson wrote:
>> In an effort to speed up the build of a project that uses Qt (and moc) I tried an alternate approach with the moc files. Normally I use the basic idea of gathering the headers that need to be "moc'ed" and feed those to moc with this type of CMake Code:
>> 
>> QT4_WRAP_CPP( FilterWidgets_Generated_MOC_SRCS ${QFilterWidget_HDRS} ${FilterWidget_GEN_HDRS}) 
>> 
>> The in the Add_Executable(...) call include the ${FilterWidgets_Generated_MOC_SRCS} variable to the list of sources. In my project I have at least 30 auto-generated files which all get moc'ed. That gives me an additional 60 compiled files. So I tried the idea of #include "moc_[some_file.cxx]" in each of the auto-generated .cpp files for each Widget. This would cut the number of files compiled in half. The issue is that since they are being #include'ed in the .cpp files then they do NOT need to be compiled themselves so I took the ${FilterWidgets_Generated_MOC_SRCS} out of the list of sources in the add_executable() call. What happened is that CMake did NOT run moc on those headers because there were now NOT included in the build.
>> 
>> So for that version of the cmake code I have something like this:
>> 
>> QT4_WRAP_CPP( FilterWidgets_Generated_MOC_SRCS ${FilterWidget_GEN_HDRS}) 
>> QT4_WRAP_CPP( FilterWidgets_MOC_SRCS ${QFilterWidget_HDRS} )
>> 
>> Is there a way to forcibly run the moc step even if the resulting source files are NOT directly included in the add_executable? Custom_Command? Add_Depends?
>> 
>> Thanks
> 
> You could still add them to the add_executable() call, but set their
> HEADER_FILE_ONLY source file property to TRUE to prevent them from being
> compiled. This way they would still show up in the proper place in
> IDE's, and no need for fiddling around with add_custom_target() and
> add_dependencies(). Of course, you might still want to introduce the
> custom target for convenience so you can trigger moc'ing manually (e.g.
> "make moc")...
> 
> Michael
> 
> --


Thanks to everyone (Michael and Andreas) for the suggestions. Both suggestions have their pros and cons. I am leaning towards the "Use the new automoc function in CMake 2.8.7" that Andreas suggested. I don't have to worry about the CMake version since there are only 2 developers. We can upgrade to the latest CMake without a problem.
  Actually NOT having them listed in the IDE is BETTER for me since I tread those files as "black boxes", ie, I don't really want to know what is in them. Or at least on a very rare occasion at which point I can just search for the file in the build directory.

Thanks again Everyone. I'll post in the next few days what I ended up doing.
--
Mike J.


More information about the CMake mailing list