kde4_automoc change (need help)
David Faure
faure at kde.org
Wed May 10 15:00:40 CEST 2006
I want to implement "Q_OBJECT in .cpp file and no .h file at all, and .cpp includes .moc" support
in kde4_automoc, for unit tests. Would be much more convenient than having to write a .h file
for each, and by having the "no header file" condition there we avoid grepping both .h and .cpp
for Q_OBJECT in all cases, which might slow things down.
But my current implementation doesn't work - no moc file, and the binary is being linked
without any source file as input... I'm stumped.
--- KDE4Macros.cmake (revision 538918)
+++ KDE4Macros.cmake (working copy)
@@ -240,6 +240,19 @@
get_filename_component(_basename ${_current_MOC} NAME_WE)
# set(_header ${CMAKE_CURRENT_SOURCE_DIR}/${_basename}.h)
set(_header ${_abs_PATH}/${_basename}.h)
+
+ # Check if the header exists
+ if (NOT EXISTS ${_header})
+ # Otherwise check if the cpp file has Q_OBJECT
+ string(REGEX MATCH "Q_OBJECT" _match "${_contents}")
+ if (_match)
+ # Yes -> moc the cpp file, not the non-existing header file.
+ set(_header ${_abs_FILE})
+ else (_match)
+ message(FATAL_ERROR "${_abs_FILE} includes ${_moc_INCS} but there's no ${_header} file and no Q_OBJECT in ${_abs_FILE} either")
+ endif (_match)
+ endif (NOT EXISTS ${_header})
+
set(_moc ${CMAKE_CURRENT_BINARY_DIR}/${_current_MOC})
#set(_moc ${_abs_PATH}/${_current_MOC})
add_custom_command(OUTPUT ${_moc}
COMMAND ${QT_MOC_EXECUTABLE}
ARGS ${_moc_INCS} ${_header} -o ${_moc}
MAIN_DEPENDENCY ${_header}
)
The testcase:
set(kovariabletest_SRCS kovariabletest.cpp)
kde4_automoc(${kovariabletest_SRCS})
kde4_add_executable(kovariabletest RUN_UNINSTALLED ${kovariabletest_SRCS})
target_link_libraries(kovariabletest kotext ${QT_QTTEST_LIBRARY})
with kovariabletest.cpp defining a QObject-derived class, with Q_OBJECT macro and #include "kovariabletest.moc"
What happens:
Linking CXX executable kovariabletest
/usr/lib/gcc/x86_64-linux-gnu/4.0.3/../../../../lib64/crt1.o: In function `_start':../sysdeps/x86_64/elf/start.S:109: undefined reference to `main'
Indeed in CMakeFiles/kovariabletest.dir/build.make, kovariabletest_OBJECTS is empty!
How could this happen, given that I checked and ${kovariabletest_SRCS} is definitely
not empty just before the call to kde4_add_executable...
--
David Faure, faure at kde.org, sponsored by Trolltech to work on KDE,
Konqueror (http://www.konqueror.org), and KOffice (http://www.koffice.org).
More information about the Kde-buildsystem
mailing list