[CMake] Transfer Kdevelop-managed project to cmake with .ui files and generated ui_*.h

Andreas Pakulat apaku at gmx.de
Tue Sep 11 04:54:19 EDT 2007


On 11.09.07 10:12:02, junior0007 wrote:
> I have a nice little project running (so far) under Kdevelop that
> contains some .ui-files. So far these ui-files are compiled using uic (i
> guess... - it's autom. done by Kdevelop...) so that the result is an
> ui_*.h file. This ui_*.h File is then included by the classes.

KDevelop doesn't do such things itself. It relies on a buildsystem. In
the case of KDevelop3 there are autotools and qmake as buildsystems that
integrate into the GUI. As you seem to be using Qt4 you probably use
QMake, if so you should have .pro files in all source folders. These
files describe your project sources and how to build an application from
it. More information on QMake can be found on
http://doc.trolltech.com/4.3/qmake-manual.html

You need to somewhat understand the QMake buildsystem to properly
convert it to CMake and its not that hard.

> The Project tree looks like:
>   > project
>     > bin
>     > src
>       > gui
>       > gui_ui
>       > model
> 
> So far i just put a CMakeLists.txt into the project und project/src -
> directory and startet with trial-and error - lots of errors
> 
> ===================================================================
> PROJECT (TOOLBOX)
> 
> SET (CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/Modules )

Thats not needed.

> # Run Qt UIC on a UI file.
> # Arguments:
> #    1-N   Names of .ui files
> MACRO(AIS_UI)
>    FOREACH (it ${ARGN})
>       GET_FILENAME_COMPONENT(outfile ${it} NAME_WE)
>       # changed next line --added ui_
>       SET(outhfile ${CMAKE_CURRENT_BINARY_DIR}/ui_${outfile}.h)
>       SET(outcppfile ${CMAKE_CURRENT_BINARY_DIR}/${outfile}_uic.cpp)
>       ADD_CUSTOM_COMMAND(OUTPUT ${outhfile}
>          COMMAND ${QT_UIC_EXECUTABLE} ARGS -o ${outhfile}
> ${CMAKE_CURRENT_SOURCE_DIR}/${it}
>          WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
>          MAIN_DEPENDENCY ${it}
>          DEPENDS ${it}
>       )
>       ADD_CUSTOM_COMMAND(OUTPUT ${outcppfile}
>          COMMAND ${QT_UIC_EXECUTABLE} ARGS
> ${CMAKE_CURRENT_SOURCE_DIR}/${it} -impl ${outhfile} -o ${outcppfile}
>          WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
>          MAIN_DEPENDENCY ${it}
>          DEPENDS ${it} ${outhfile})
>         SET(CPP_SOURCE ${CPP_SOURCE} ${outcppfile})
>       AIS_MOC(${outhfile})
> 
>       SET(AIS_UIC_SOURCES ${AIS_UIC_SOURCES} ${outcppfile})
>       LIST(APPEND AIS_UIC_HEADERS ${outhfile})
>    ENDFOREACH (it)
> ENDMACRO(AIS_UI)

Uhm, Qt4 CMake module provides a macro for that, its called qt4_wrap_ui()

> # Run Qt MOC on a C++ header file.
> # Arguments:
> #    1-N   Names of headerfiles
> MACRO(AIS_MOC)
>    FOREACH (it ${ARGN})
>       # Make .h filename absolute
>       GET_FILENAME_COMPONENT(infile ${it} ABSOLUTE)
> 
>       # Build x_moc.cpp filename
>       GET_FILENAME_COMPONENT(outfile ${it} NAME_WE)
>       SET(outfile ${CMAKE_CURRENT_BINARY_DIR}/${outfile}_moc.cpp)
> 
>       # Run MOC
>       ADD_CUSTOM_COMMAND(OUTPUT ${outfile}
>          COMMAND ${QT_MOC_EXECUTABLE}
>             ARGS ${infile} -o ${outfile}
>          WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
>          DEPENDS ${infile}
>          COMMENT MOCing ${it})
>       LIST(APPEND CPP_SOURCE ${outfile})
>       LIST(APPEND AIS_MOC_SOURCES ${outfile})
>    ENDFOREACH(it)
> ENDMACRO(AIS_MOC)

Same thing for this, use qt4_automoc() and have a #include "foo.moc" in
foo.cpp 

> # don't know for what but works ;-)
> include(${QT_USE_FILE})

That gives you another macro QT_MODULE_SETUP which you can use to setup
your project for any of the Qt4 modules, like QtGui, QtCore, QtNetwork.
By default it activates QtCore+QtGui.

> TARGET_LINK_LIBRARIES (Toolbox ${QT_LIBRARIES})
> =======================================================================
> 
> Works nice until linking... Linking causes: undefined reference to
> `vtable for [...]

That usually indicates you don't compiler or link the code in the .moc
files (or they're not generated at all).

Andreas

-- 
You will have long and healthy life.


More information about the CMake mailing list