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

Laszlo Papp lpapp at kde.org
Mon Oct 31 09:38:52 EDT 2011


> 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 ?

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 ...

Something like this might work by using automoc, but is this really
that hard by using cmake ? Please do not take it an offense, but it is
pretty simple by using qmake ;)

[code]
cmake_minimum_required(VERSION 2.8)
find_package(Automoc4 REQUIRED)
include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})
find_package(Qt4)
include_directories(
    ${QT_INCLUDES}
    ${CMAKE_CURRENT_BINARY_DIR}
    ${CMAKE_CURRENT_SOURCE_DIR}
)
set(test_SRCS
    main.cpp
)
automoc4_add_executable(test ${test_SRCS})
target_link_libraries(test ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY})
[/code]

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

I have cmake version 2.8.6. installed.

Best Regards,
Laszlo Papp


More information about the CMake mailing list