PDA

View Full Version : using QT unit testing with CMake



carpevitam
30th December 2010, 19:36
I am trying to build my QT test unit appl (myTest.cpp) using CMake. The test appl compiles/runs fine if I use qmake.
However, when I use CMake it complains about line at end of file: include "myTest.moc".
I needed this line when I use qmake. So I tried:
a). Removing line (include "myTest.moc")
b). Adding a qt4_wrap_cpp to create myTest.moc.
Now, I get a bunch of moc_myTest.cxx errors.
This unit test file doesn't have a header file. Any ideas what I am missing.

fullmetalcoder
31st December 2010, 12:32
I use the following macros to mimic qmake behavior in cmake and so far it works fine :

# Smarter versions of moc wrapping macros that mimic qmake behavior
function(qt4_wrap_hdrs _moc_srcs)
qt4_get_moc_flags(_moc_incs)
set(_mocs)
foreach(_current_file ${ARGN})
get_filename_component(_abs_file ${_current_file} ABSOLUTE)
if(EXISTS ${_abs_file})
file(READ ${_abs_file} _contents)
get_filename_component(_basename ${_abs_file} NAME_WE)
string(REGEX MATCH "Q_OBJECT" _match "${_contents}")
if(_match)
set(_moc "${CMAKE_CURRENT_BINARY_DIR}/moc_${_basename}.cpp")
qt4_create_moc_command(${_abs_file} ${_moc} "${_moc_incs}" "")
macro_add_file_dependencies(${_abs_file} ${_moc})
list(APPEND _mocs ${_moc})
endif(_match)
endif(EXISTS ${_abs_file})
endforeach (_current_file)
set(${_moc_srcs} ${_mocs} PARENT_SCOPE)
endfunction(qt4_wrap_hdrs)

function(qt4_wrap_srcs)
qt4_get_moc_flags(_moc_incs)
foreach(_current_file ${ARGN})
get_filename_component(_abs_file ${_current_file} ABSOLUTE)
if(EXISTS ${_abs_file})
file(READ ${_abs_file} _contents)
get_filename_component(_abs_path ${_abs_file} PATH)
get_filename_component(_basename ${_abs_file} NAME_WE)
string(REGEX MATCH "# *include +\"${_basename}\\.moc\"" _match "${_contents}")
if(_match)
string(REGEX MATCH "[^ <\"]+\\.moc" _moc "${_match}")
set(_moc "${CMAKE_CURRENT_BINARY_DIR}/${_moc}")
qt4_create_moc_command(${_abs_file} ${_moc} "${_moc_incs}" "")
macro_add_file_dependencies(${_abs_file} ${_moc})
endif(_match)
endif(EXISTS ${_abs_file})
endforeach(_current_file)
endfunction(qt4_wrap_srcs)


Typical usage (assuming _hdrs, _srcs, _forms and _rsrcs variable are defined and hold respectively the HEADERS, SOURCES, FORMS and RESOURCES of your qmake project) :


qt4_wrap_hdrs(_moc_srcs ${_hdrs})
qt4_wrap_srcs(${_srcs})
qt4_wrap_ui(_forms_hdrs ${_forms})
qt4_add_resources(_rsrcs_srcs ${_rsrcs})
add_library(${_name} ${_srcs} ${_moc_srcs} ${_rsrcs_srcs})

carpevitam
3rd January 2011, 00:20
Thank you so much for your help. I don't understand everything your macro does (yet),
but I believe it fixed my building issue. I have much to learn. Thanks a lot.