0

I have a library and example application, driven by CMake. So, there is a class, which I use in library:

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

and example:

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();
}

Then I receive a lot of ld errors:

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

This file (main.cpp) represents example application, which should test core functionality of library.

4
  • Show us sourceeditor.h as well, please. Commented Mar 29, 2013 at 11:14
  • 2
    You're probably missing a Q_OBJECT macro, but since you're not showing the definition of the SourceEditor class, hard to tell. Commented Mar 29, 2013 at 11:15
  • @Mat header was added Commented Mar 29, 2013 at 11:17
  • @AndyProwl private class (some kind of pimpl) Commented Mar 29, 2013 at 11:17

1 Answer 1

1

The problem is most likely due to the fact that you are #includeing a .cpp file:

#include "../src/sourceeditor.cpp"

You shouldn't do that, and you don't need it. Just include the corresponding header sourceeditor.h and, if needed, the novile_debug.h header.

Then, make sure both main.cpp and sourceeditor.cpp are part of your project, so that the compiler will process both these translation units and the linker will eventually merge the corresponding object code into the executable of your program.

Sign up to request clarification or add additional context in comments.

4 Comments

The problem is I don't have hpp file and I need code from .cpp file, too. There are library and example executable (main.cpp)
@IllyaKovalevskyy: Sorry, I meant sourceeditor.h, the header file you have.
Then I have errors because I haven't included implementation, which is stored in .cpp
@IllyaKovalevskyy: What implementation? What errors are you getting? You should never #include a .cpp file

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.