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

Andreas Pakulat apaku at gmx.de
Mon Oct 31 18:27:53 EDT 2011


On 31.10.11 21:07:45, Alexander Neundorf wrote:
> On Monday 31 October 2011, Laszlo Papp wrote:
> > Hi,
> > 
> > I would like to achieve something like this by using cmake:
> > http://doc.qt.nokia.com/4.8/activeqt-comapp.html
> > 
> > I would like to have a QObject subclass in my main.cpp file since it
> > has just one method for instance, and thus it would be a convenient
> > approach for me.
> > 
> > This is my current CMakeLists.txt file:
> > 
> > [code]
> > cmake_minimum_required(VERSION 2.8)
> > 
> > include_directories(${CMAKE_CURRENT_SOURCE_DIR}
> > ${CMAKE_CURRENT_BINARY_DIR})
> > 
> > find_package(Qt4)
> > 
> > #set(CMAKE_AUTOMOC ON)
> > 
> > set(test_SRCS
> >     main.cpp
> > )
> > 
> > qt4_automoc(${test_SRCS})
> 
> I'd advice against using this.
> Among others it only rechecks at cmake time, which is not enforced by simply 
> editing the source files.
> 
> The straightforward way should be
> 
> qt4_wrap_cpp(mocFiles main.cpp)
> add_executable(test ${test_SRCS} ${mocFiles})
> 
> and then _not_ include the moc file in main.cpp.

As explained elsewhere in the thread, this does not work because the
file generated by moc for the classes in the .cpp file will not compile
standalone. It requires the declarations from the .cpp file, hence
#include'ing the moc-generated file is the only option. Adding it to the
executable as source is hence not going to work and that means one needs
to manually add dependencies to get cmake to generate it in the first
place.

Andreas
 


More information about the CMake mailing list