PDA

View Full Version : CMake :Undefined reference for a QObject class



Zander87
4th November 2012, 17:45
Hello all
I have an linking issue when compiling a programm under linux 32 bit that is using a dynamic library
My library is a class inheriting QObject and it has the Q_OBEJCT macro inside the classe.

The problem is that it says "undefined reference" to all signals I have for this class (and only the signals)
I suspect something I have to change in one or the other CMake but I can t find what even after googling.
I also suspect it would be due to the "Q_OBJECT" macro..

the cmake lists of the library is the following



cmake_minimum_required(VERSION 2.6)
project(copy_playlist_backend)
set(LIBRARY_OUTPUT_PATH lib)
find_package(Qt4 REQUIRED)
INCLUDE(${QT_USE_FILE})

ADD_DEFINITIONS(${QT_DEFINITIONS}) # I have added those four lines after googling but i am not sure what it does.....
ADD_DEFINITIONS(-DQT_PLUGIN)
ADD_DEFINITIONS(-DQT_NO_DEBUG)
ADD_DEFINITIONS(-DQT_SHARED)

include_directories(${QT_INCLUDES} ${CMAKE_CURRENT_BINARY_DIR} include)
SET (copy_playlist_backend_SRCS src/*)
SET (copy_playlist_backend_HEADERS ../include/*)

file(GLOB_RECURSE source_files ${copy_playlist_backend_SRCS})

qt4_wrap_cpp(copy_playlist_backend_HEADERS_MOC ${copy_playlist_backend_HEADERS})
qt4_automoc(${copy_playlist_backend_SRCS}${copy_pl aylist_backend_HEADERS_MOC})

add_library(copy_playlist_backend SHARED ${source_files} )


the cmake of the executable ist the following



cmake_minimum_required(VERSION 2.6)
enable_language(CXX)
PROJECT(Rainyday)
FIND_PACKAGE(Qt4 REQUIRED)
SET (QT_USE_QTXMLPATTERNS)

SET (project_name_VERSION_MAJOR 0) # The MAJOR version number.
SET (project_name_VERSION_MINOR 1) # The MINOR version number

SET (CPB_INCLUDE /home/max/Documents/C/Library/copy_playlist_backend/include) #directory that contain the .h file of the library i want to link

INCLUDE_DIRECTORIES(${CPB_INCLUDE} ${QT_INCLUDES} ${CMAKE_CURRENT_BINARY_DIR} ${QT_QTXML_INCLUDE_DIR} ${QT_QTXMLPATTERNS_INCLUDE_DIR})

SET(Rainyday_SOURCES main.cpp mainwindow.cpp) # collect the sources of the project and call the collected set "project_name_SRCS"
SET(Rainyday_HEADERS mainwindow.h ui_mainwindow.h)
SET(Rainyday_FORMS mainwindow.ui)
SET(Rainyday_RCCS playlist_ressource.qrc)

QT4_WRAP_CPP(Rainyday_HEADERS_MOC ${Rainyday_HEADERS})
QT4_WRAP_UI(Rainyday_FORMS_HEADERS ${Rainyday_FORMS})
QT4_ADD_RESOURCES(Rainyday_RCC_SRCS ${Rainyday_RCCS})
INCLUDE(${QT_USE_FILE})
ADD_DEFINITIONS(${QT_DEFINITIONS})
LINK_DIRECTORIES(/home/max/Documents/C/Library/lib/) # Directory that contain the .so file

qt4_automoc(${Rainyday_SOURCES} ${Rainyday_HEADERS_MOC} ${Rainyday_RCC_SRCS} ${Rainyday_FORMS_HEADERS} ${Rainyday_FORMS} )
add_executable(Rainyday ${Rainyday_SOURCES} ${Rainyday_HEADERS_MOC} ${Rainyday_RCC_SRCS} ${Rainyday_FORMS_HEADERS} ${Rainyday_FORMS} )
target_link_libraries(Rainyday ${QT_LIBRARIES} ${QT_QTCORE_LIBRARY} ${QT_QTXML_LIBRARY} ${QT_QTXMLPATTERNS_LIBRARY} /home/max/Documents/C/Library/lib/libcopy_playlist_backend.so)

Zander87
7th November 2012, 10:57
Uping ? just in case of..

Zander87
8th November 2012, 14:35
Well I finallly managed to get it thanks to http://qtnode.net/wiki/Qt4_with_cmake

So this is a template of CMakeLists.txt for a classe called "MACLASSE" that would inherit from QObject (which was the problem because you then have to use QT_WRAPP_CPP


cmake_minimum_required(VERSION 2.6)
project(MACLASSE)
set(LIBRARY_OUTPUT_PATH lib)
find_package(Qt4 REQUIRED)
INCLUDE(${QT_USE_FILE})
include_directories(${QT_INCLUDES} ${CMAKE_CURRENT_BINARY_DIR} include)

SET (MACLASSE_SRCS src/MACLASSE.cpp)
SET (MACLASSE_HEADERS include/MACLASSE.h)

MESSAGE( STATUS "sources: " ${MACLASSE_SRCS} )
MESSAGE( STATUS "headers: " ${MACLASSE_HEADERS} )

qt4_wrap_cpp(MACLASSE_HEADERS_MOC ${MACLASSE_HEADERS}) #in there you put the headers and ONLY the headers

add_library(MACLASSE SHARED ${MACLASSE_SRCS} ${MACLASSE_HEADERS_MOC} )
target_link_libraries(MACLASSE ${QT_LIBRARIES})