[CMake] Building a main.cpp containing a QObject subclass

Andreas Pakulat apaku at gmx.de
Mon Oct 31 10:07:33 EDT 2011


On 31.10.11 15:38:52, Laszlo Papp wrote:
> > You're not supposed to add the mocfiles variable contents you receive
> > from qt4_wrap_cpp to the list of sources. In particular not because you
> > already #include that same file in the main.cpp. If you look at the
> > generated file you'll notice that it requires all the declarations from
> > the main.cpp, i.e. its not a standalone C++ source and cannot be
> > compiled on its own.
> 
> You are right. That sounds logical. Unfortunately, the moc file is not
> generated if I do it that way. If I generate it manually, the build
> works. Is there a way, similar to add_dependencies, to tell moc in the
> CMakeLists.txt file to generate the moc_main.cxx before the main.cpp ?

Yes, add_custom_target can be used:

add_custom_target( foo DEPENDS mocfiles );
add_dependencies( exe foo );

> I think those macros are not enough alone since
> qt4_generate_moc(main.cpp moc_main.cxx) did not work either. I would
> expect that qt4_generate_moc can work for source files. Apparently,
> qt4_wrap_cpp can work only for header files ...

No, its just the missing dependency.

> Something like this might work by using automoc, but is this really
> that hard by using cmake ?

Its not, but someone has to do it. As I said I don't know how the
automoc in cmake 2.8.6 behaves. What I do know is that KDE's automoc
does support all this with 2 macros (a pre-target and a post-target
one), but it requires a separate C++ app.

> Please do not take it an offense, but it is
> pretty simple by using qmake ;)

Yes, but qmake can build Qt projects for quite some more time than CMake
can ;) 

> Also,"set(CMAKE_AUTOMOC ON)" aborts the build:
> terminate called after throwing an instance of 'std::logic_error'
>   what():  basic_string::_S_construct null not valid
> Aborted

That looks like a cmake bug that should be reported.

Andreas



More information about the CMake mailing list