cmake链接本地文件时的未定义函数

时间:2013-12-11 16:10:24

标签: c++ qt linker cmake

我正在尝试将一个小的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

2 个答案:

答案 0 :(得分:0)

它无法找到某个东西的vtable - 这意味着它正在寻找一个类型将存在于vtable中的条目(如果它存在),因为它是一个链接器。 (这不是说在运行时缺乏vtable的类型,因为根本没有意义,我甚至不喜欢那句话)

某处你有一个你从未定义的纯虚方法。

该代码真的很难阅读和读取它我甚至在bindable.h中看不到任何虚拟,这意味着在父类中有一个纯虚函数而你永远不会实现它,这就是你的错误。

更具体地说,你永远不会实现其中的3个。

我不好意思说: wxWidgets FTW:P

答案 1 :(得分:0)

我在另一个问题上找到了答案。它不在示例Qt doc中,但我只需要设置另一个环境变量。

设置(CMAKE_AUTOMOC ON)

Why am I getting "undefined reference to vtable..." errors when linking this Qt 5.0 application?