【发布时间】:2013-12-11 16:10:24
【问题描述】:
我正在尝试将一个小型 Qt 示例编译为可执行文件(test.cpp 包含 main),然后添加对将非 test.cpp 代码编译到其自己的库的支持。我正在使用 qmake,但现在我正在尝试按照示例 here 使用 cmake。不幸的是,我遇到了链接错误。看起来在链接时找不到我在正在编译的 cpp 文件中定义的构造函数。
$ make
Linking CXX executable test
CMakeFiles/test.dir/attribute_editor.cpp.o: In function `AttributeEditor::AttributeEditor(QWidget*)':
attribute_editor.cpp:(.text+0x2a): undefined reference to `vtable for AttributeEditor'
CMakeFiles/test.dir/bindable.cpp.o: In function `Bindable::Bindable(QObject*)':
bindable.cpp:(.text+0x50): undefined reference to `vtable for Bindable'
CMakeFiles/test.dir/bindable.cpp.o: In function `AttributeObject::AttributeObject()':
bindable.cpp:(.text._ZN15AttributeObjectC2Ev[_ZN15AttributeObjectC5Ev]+0x24): undefined reference to `vtable for AttributeObject'
collect2: error: ld returned 1 exit status
make[2]: *** [test] Error 1
make[1]: *** [CMakeFiles/test.dir/all] Error 2
make: *** [all] Error 2
这是我的 cmake 文件...
cmake_minimum_required(VERSION 2.8)
# http://qt-project.org/quarterly/view/using_cmake_to_build_qt_projects
PROJECT(qattrs)
FIND_PACKAGE(Qt4 REQUIRED)
SET(QT_USE_QTSCRIPT TRUE)
SET(qattrs_SOURCES test.cpp attribute_editor.cpp bindable.cpp)
SET(qattrs_HEADERS bindable.h attribute_editor.h)
QT4_WRAP_CPP(qattrs_HEADERS_MOC ${qattrs_HEADERS})
INCLUDE(${QT_USE_FILE})
ADD_DEFINITIONS(${QT_DEFINITIONS})
ADD_EXECUTABLE(test ${qattrs_SOURCES} ${qattrs_HEADER_MOC})
TARGET_LINK_LIBRARIES(test ${QT_LIBRARIES})
我对 cmake 知之甚少,但我认为它不包括链接期间的 .o 文件。
整个代码(五个或六个文件)位于github。
【问题讨论】: