Reputation: 71
I'm sorry for my stupid question. I'm trying to compile dll with cmake from class with Qt objects. Library contains function which uses class defined in other file. And during link proccess I get error "undefined reference" to class member. Where is the error in CMakeList.txt?
CMakeList.txt:
cmake_minimum_required(VERSION 2.8)
project(foo)
set(SOURCE_LIB foo.cpp window.cpp)
find_package (Qt4 REQUIRED)
include (${QT_USE_FILE})
ADD_DEFINITIONS(-DFOO_LIBRARY)
add_library(foo SHARED ${SOURCE_LIB})
target_link_libraries(foo ${QT_LIBRARIES})
foo.h:
#ifndef FOO_H
#define FOO_H
#include "foo_global.h"
void FOOSHARED_EXPORT hello_world();
#endif // FOO_H
foo.cpp:
#include <QApplication>
#include <QMessageBox>
#include "window.h"
void hello_world()
{
int argc = 0;
char ** argv = (char **) 0;
QApplication a(argc, argv);
Window *w = new Window();
w->Msg();
}
foo_global.h:
#ifndef FOO_GLOBAL_H
#define FOO_GLOBAL_H
#include <QtCore/qglobal.h>
#if defined(FOO_LIBRARY)
# define FOOSHARED_EXPORT Q_DECL_EXPORT
#else
# define FOOSHARED_EXPORT Q_DECL_IMPORT
#endif
#endif // FOO_GLOBAL_H
window.cpp:
#include <QMessageBox>
#include "window.h"
void Window :: Msg()
{
QMessageBox *msg = new QMessageBox();
msg->setText("hello");
msg->exec();
}
window.h:
#ifndef WINDOW_H
#define WINDOW_H
#include <QtGui>
class Window
{
Q_OBJECT
public:
void Msg();
};
#endif // WINDOW_H
Output:
Test\build>c:\MinGW\bin\mingw32-make.exe
Scanning dependencies of target foo
[ 50%] Building CXX object CMakeFiles/foo.dir/foo.cpp.obj
[100%] Building CXX object CMakeFiles/foo.dir/window.cpp.obj
Linking CXX shared library libfoo.dll
Creating library file: libfoo.dll.a
CMakeFiles\foo.dir/objects.a(foo.cpp.obj):foo.cpp:(.text$_ZN6WindowC1Ev[Window::
Window()]+0x8): undefined reference to `vtable for Window'
collect2: ld returned 1 exit status
mingw32-make[2]: *** [libfoo.dll] Error 1
mingw32-make[1]: *** [CMakeFiles/foo.dir/all] Error 2
mingw32-make: *** [all] Error 2
Upvotes: 1
Views: 2973
Reputation: 9853
All headers for classes that contain Q_OBJECT
macro must be processed by the meta-object compiler.
So, your cmake file should look like this:
cmake_minimum_required(VERSION 2.8)
project(foo)
set(LIB_SOURCE foo.cpp window.cpp)
set(LIB_HEADER foo.h)
find_package (Qt4 REQUIRED)
include (${QT_USE_FILE})
ADD_DEFINITIONS(-DFOO_LIBRARY)
QT4_WRAP_CPP(LIB_HEADER_MOC ${LIB_HEADER})
add_library(foo SHARED ${LIB_SOURCE} ${LIB_HEADER_MOC})
target_link_libraries(foo ${QT_LIBRARIES})
Upvotes: 4