[CMake] How to write CMakeLists.txt for Qt4 when Q_OBJECT in xxx.cpp file

1+1=2 dbzhang800 at gmail.com
Sat Jul 31 09:05:28 EDT 2010


Thanks for your answer.

when use QT4_WRAP_CPP( Generated_MOC_SRCS main.cpp ),  moc_main.cxx
will be generated.
then I use #include "moc_main.cxx" instead of #include "main.moc"

but ${Generated_MOC_SRCS}  can not be added to  ADD_EXECUTABLE.
otherwise moc_main.cxx will be compiled too.

This works. but I want to use main.moc instead of moc_main.cxx. Can
someone help me?

If i have two files: xxx.h xxx.cpp, ther both has an Q_OBJECT (such as
xxxPriveteClass defined in xxx.cpp),  two moc_main.cxx files will be
generated in this case.

for qmake: moc_xxx.cpp generated for xxx.h,  xxx.moc generated for
xxx.cpp, which works very well.

best regards

debao

On Sat, Jul 31, 2010 at 8:21 PM, Michael Jackson
<mike.jackson at bluequartz.net> wrote:
> PROJECT(mytest)
> CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
> FIND_PACKAGE(Qt4 COMPONENTS QtCore QtGui REQUIRED)
> INCLUDE(${QT_USE_FILE})
> SET(pro_SOURCES    main.cpp)
>
> QT4_WRAP_CPP( Generated_MOC_SRCS main.cpp )
>
> INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR})
>
> ADD_EXECUTABLE(mytest  ${pro_SOURCES} ${Generated_MOC_SRCS} )
> TARGET_LINK_LIBRARIES(mytest ${QT_LIBRARIES})
> ___________________________________________________________
> Mike Jackson                      www.bluequartz.net
>
>
> On Jul 31, 2010, at 7:38 AM, 1+1=2 wrote:
>
>> Hi,everyone
>>
>> I am new to cmake. I don't know how to write a CMakeList.txt file if
>> Q_OBJECT in a  "xxx.cpp" source file.
>>
>> for example,
>> A very simple qt4 program like this.
>>
>> // filename main.cpp
>> #include <QtGui/QApplication>
>> #include <QtGui/QWidget>
>> class Widget:public QWidget
>> {
>>   Q_OBJECT
>> public:
>>   Widget(QWidget * parent=0):QWidget(parent) {}
>> };
>> #include "main.moc"
>> int main(int argc, char** argv) {
>>   QApplication app(argc, argv);
>>   Widget w;
>>   w.show();
>>   return app.exec();
>> }
>>
>>
>> if i use qmake, i only needed a simple .pro file like this:
>>
>> TEMPLATE = app
>> SOURCES += main.cpp
>>
>> I try to write a CMakeList.txt, try qt4_wrap_cpp qt4_automoc and
>> qt4_generate_moc, but They did not seem to work
>>
>> PROJECT(mytest)
>> CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
>> FIND_PACKAGE(Qt4 COMPONENTS QtCore QtGui REQUIRED)
>> INCLUDE(${QT_USE_FILE})
>> SET(pro_SOURCES    main.cpp)
>>
>> #QT4_WRAP_CPP(${pro_SOURCES})
>> #QT4_AUTOMOC(${pro_SOURCES})
>> #QT4_GENERATE_MOC(${CMAKE_CURRENT_SOURCE_DIR}/main.cpp
>> ${CMAKE_CURRENT_BINARY_DIR}/main.moc)
>>
>> INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR})
>> ADD_DEFINITIONS(${QT_DEFINITIONS})
>> ADD_EXECUTABLE(mytest  ${pro_SOURCES})
>> TARGET_LINK_LIBRARIES(mytest ${QT_LIBRARIES})
>>
>> Is there any solution to this?
>>
>> thanks
>> _______________________________________________
>> Powered by www.kitware.com
>>
>> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>>
>> Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ
>>
>> Follow this link to subscribe/unsubscribe:
>> http://www.cmake.org/mailman/listinfo/cmake
>
>


More information about the CMake mailing list