This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Problem with "find_package(KDE4 REQUIRED)" in shared object

Tags: None
(comma "," separated)
Tobias Renger
Registered Member
Posts
5
Karma
0
Hello,

I want to include some KDE classes in a library. So I put the find_package(KDE4 REQUIRED) statement into the CMakeLists.txt of the library. The compilation and installation of it (as shared object) works without errors. But when I link an application against the library I get "undefined reference to" errors. The errors disappear when I remove the find_package(KDE4 REQUIRED) statement from the CMakeLists.txt of the library.

Is there anything I if have to consider when I want to use KDE classes in a library?

I attached some files which demonstrate the problem on a minimalistic application:

The error message I get when I try to link against the library is
Code: Select all
main.cpp:18: undefined reference to `LibClass::sayHello()'


Folder structure

app
|-build
|-src
|--main.cpp
|--CMakeLists.txt

lib
|-build
|-src
|--CMakeLists.txt
|--libclass.cpp
|--libclass.h

app CMakeLists.txt
Code: Select all
project(app)

find_package(Qt4 REQUIRED)
find_package(KDE4 REQUIRED)

include_directories(
    ${KDE4_INCLUDES}
    ${LIB_PATH}/src
)

set(app_SRCS
    main.cpp
)

kde4_add_executable(app ${app_SRCS})

target_link_libraries(app ${KDE4_KDEUI_LIBS} libclass.so)

install(TARGETS app DESTINATION ${BIN_INSTALL_DIR})

app main.cpp
Code: Select all
#include <QDebug>

#include <KAboutData>
#include <KApplication>
#include <KCmdLineArgs>

#include "libclass.h"

int main (int argc, char *argv[])
{
    KAboutData aboutData("app",
                         0,
                         ki18n("app"),
                         "0.1");
    KCmdLineArgs::init(argc, argv, &aboutData);
    KApplication app;
   
    qDebug() << LibClass::sayHello();
   
    return app.exec();
}


lib CMakeLists.txt
Code: Select all
project(libclass)

find_package(Qt4 REQUIRED)
find_package(KDE4 REQUIRED)

include(${QT_USE_FILE})

set(libclass_SRCS
    libclass.cpp
)

add_library(class SHARED ${libclass_SRCS})

target_link_libraries(class ${QT_LIBRARIES})

install(TARGETS class LIBRARY DESTINATION ${LIB_INSTALL_DIR})

lib libclass.cpp
Code: Select all
#include "libclass.h"

const QString LibClass::sayHello()
{
    return "Hello World!";
}

lib libclass.h
Code: Select all
#ifndef LIBCLASS_H
#define LIBCLASS_H

#include <QString>

class LibClass
{
   
public:
    static const QString sayHello();
   
};

#endif
User avatar
anda_skoa
KDE Developer
Posts
783
Karma
4
OS
The undefined reference error is reporting it can't find your class's method.

My guess would be that this is because there is no EXPORT macro.

Add this header:
Code: Select all
#ifndef LIBCLASS_EXPORT_H
#define LIBCLASS_EXPORT_H

/* needed for KDE_EXPORT and KDE_IMPORT macros */
#include <kdemacros.h>

#ifndef LIBCLASS_EXPORT
# if defined(MAKE_LIBCLASS_LIB)
   /* We are building this library */
#  define LIBCLASS_EXPORT KDE_EXPORT
# else
   /* We are using this library */
#  define LIBCLASS_EXPORT KDE_IMPORT
# endif
#endif

#endif


and in your libclass.h you add this between "class" and the class name

Code: Select all
#ifndef LIBCLASS_H
#define LIBCLASS_H

#include "libclass-export.h"

#include <QString>

class LIBCLASS_EXDPORT LibClass


Cheers,
_


anda_skoa, proud to be a member of KDE forums since 2008-Oct.
Tobias Renger
Registered Member
Posts
5
Karma
0
Thanks a lot. That solved the problem.


Bookmarks



Who is online

Registered users: Bing [Bot], blue_bullet, Google [Bot], rockscient, Yahoo [Bot]