我很抱歉我的愚蠢问题。我正在尝试使用带有 Qt 对象的类中的 cmake 编译 dll。库包含使用其他文件中定义的类的函数。在链接过程中,我收到错误“未定义的引用”到类成员。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})
富.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
窗口.cpp:
#include <QMessageBox>
#include "window.h"
void Window :: Msg()
{
QMessageBox *msg = new QMessageBox();
msg->setText("hello");
msg->exec();
}
窗口.h:
#ifndef WINDOW_H
#define WINDOW_H
#include <QtGui>
class Window
{
Q_OBJECT
public:
void Msg();
};
#endif // WINDOW_H
输出:
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