Qt应用程序中未定义的引用错误
Undefined reference errors in Qt application
我有一个库和示例应用程序,由CMake驱动。所以,有一个类,我在库中使用:
sourceeditor.h
#ifndef SOURCEEDITOR_H
#define SOURCEEDITOR_H
#include <QWidget>
#include "novile_export.h"
namespace Novile
{
class SourceEditorPrivate;
class NOVILE_EXPORT SourceEditor : public QWidget
{
Q_OBJECT
public:
explicit SourceEditor(QWidget *parent = 0);
~SourceEditor();
private:
SourceEditorPrivate * const d;
};
} // namespace Novile
#endif // SOURCEEDITOR_H
sourceeditor.cpp
#include <QtCore>
#include <QVBoxLayout>
#include <QWebView>
#include "novile_debug.h"
#include "sourceeditor.h"
namespace Novile
{
class SourceEditorPrivate
{
public:
SourceEditorPrivate(SourceEditor *p = 0) :
parent(p),
aceView(new QWebView(p)),
layout(new QVBoxLayout(p))
{
parent->setLayout(layout);
layout->addWidget(aceView);
}
~SourceEditorPrivate()
{
}
void loadAceView()
{
aceView->load(QUrl("qrc:/ace.html"));
}
private:
SourceEditor *parent;
QWebView *aceView;
QVBoxLayout *layout;
};
SourceEditor::SourceEditor(QWidget *parent) :
QWidget(parent),
d(new SourceEditorPrivate(this))
{
mDebug() << "Source Editor has been started";
d->loadAceView();
}
SourceEditor::~SourceEditor()
{
}
} // namespace Novile
和例子:
main.cpp
#include <QApplication>
#include "../src/sourceeditor.cpp"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Novile::SourceEditor editor;
editor.setGeometry(100, 50, 600, 300);
editor.show();
return app.exec();
}
然后我收到很多ld
错误:
CMakeFiles/example.dir/main.cpp.o: In function `Novile::SourceEditor::SourceEditor(QWidget*)':
../src/sourceeditor.cpp:39: undefined reference to `vtable for Novile::SourceEditor'
../src/sourceeditor.cpp:39: undefined reference to `vtable for Novile::SourceEditor'
CMakeFiles/example.dir/main.cpp.o: In function `Novile::SourceEditor::~SourceEditor()':
../src/sourceeditor.cpp:46: undefined reference to `vtable for Novile::SourceEditor'
../src/sourceeditor.cpp:46: undefined reference to `vtable for Novile::SourceEditor'
collect2: error: ld returned 1 exit status
这个文件(main.cpp)是一个示例应用程序,它应该测试库的核心功能。
这个问题很可能是由于你正在#include
一个.cpp
文件:
#include "../src/sourceeditor.cpp"
你不应该那样做,你也不需要。只包括相应的标题sourceeditor.h
,如果需要,novile_debug.h
标题。
然后,确保main.cpp
和sourceeditor.cpp
都是你的项目的一部分,这样编译器会处理这两个翻译单元,链接器最终会将相应的目标代码合并到你的程序的可执行文件中。
相关文章:
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- C++引用错误
- 已定义函数时出现 G++ "未定义的引用"错误
- 链接 cmake 时出现未定义的引用错误
- 只有级联分类器会发出未定义的引用错误
- 2个模板化类的非模板友元函数未定义引用错误
- C/C++ 取消引用错误:在空检查之前取消引用
- 为什么我的C++代码无法编译,出现未定义的引用错误
- 我的代码中C++未定义的引用错误?
- 模板专用化会导致未定义的引用错误
- .cpp文件中未定义的引用错误
- c++ 中目录中文件的未定义引用错误
- Opencv4 使用 pkg-config 给出未定义的引用错误
- 使用Cygwin C++时出现未定义的引用错误,为什么我的文件没有链接?
- 共享指针取消引用错误
- 编译简单的多文件类程序时出现未定义的引用错误
- 模板函数产生未定义的引用错误
- 调用提取重加载器会产生对"运算符"的未定义引用错误>>
- 如何修复"未定义的引用"错误 opencv 和 g++
- 未定义的引用错误,我该怎么办?