Results 1 to 9 of 9

Thread: Plugin compiler error: undefined reference

  1. #1
    Join Date
    Sep 2009
    Location
    Nanjing, China
    Posts
    46
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Plugin compiler error: undefined reference

    Hi, all! I want to put all UI components into one dll so I try to create a plugin. Then my folder tree is:

    / --- test.pro
    |
    ui ---- ui.pro
    | | -- mainwindow.h
    | | -- mainwindow.cpp
    | | -- ivisible.h // this contains the interface that has only two functions: setVisible(bool) and visible()
    |
    app ----- app.pro
    | --- main.cpp
    | --- mainwindow.h // copy from ui/mainwindow.h

    and the code is:

    test.pro
    Qt Code:
    1. TEMPLATE = subdirs
    2. CONFIG += ordered
    3. SUBDIRS += ui app
    To copy to clipboard, switch view to plain text mode 

    app.pro
    Qt Code:
    1. TARGET = test
    2. TEMPLATE = app
    3. CONFIG += debug
    4. SOURCES += main.cpp
    5. HEADERS += mainwindow.h
    6. LIBS += -LE:\documents\test\app\plugins -lui
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include <QPluginLoader>
    3. #include <QMessageBox>
    4. #include <QDir>
    5. #include "mainwindow.h"
    6.  
    7. int main(int argc, char *argv[])
    8. {
    9. QApplication a(argc, argv);
    10. QDir dir(a.applicationDirPath ());
    11. dir.cdUp();
    12. dir.cd("plugins");
    13. QPluginLoader loader(dir.absolutePath() + "/ui.dll");
    14. QObject *plugin = loader.instance();
    15. if(plugin) {
    16. MainWindow *win = qobject_cast<MainWindow *>(plugin);
    17. win->setVisible(true);
    18. } else {
    19. QMessageBox::information(NULL, "load error", loader.errorString());
    20. }
    21. loader.unload();
    22. return a.exec();
    23. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include "ivisible.h"
    6.  
    7. namespace Ui {
    8. class MainWindow;
    9. }
    10.  
    11. class MainWindow : public QMainWindow, public IVisible {
    12. Q_OBJECT
    13. Q_INTERFACES(IVisible)
    14.  
    15. public:
    16. MainWindow(QWidget *parent = 0);
    17. ~MainWindow();
    18.  
    19. void setVisible(bool v) { if(v) this->show(); else this->hide(); }
    20. bool visible() const { return this->visible(); }
    21.  
    22. protected:
    23. void changeEvent(QEvent *e);
    24.  
    25. private:
    26. Ui::MainWindow *ui;
    27. };
    28.  
    29. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp
    Qt Code:
    1. #include <QtPlugin>
    2.  
    3. #include "mainwindow.h"
    4. #include "ui_mainwindow.h"
    5.  
    6. MainWindow::MainWindow(QWidget *parent) :
    7. QMainWindow(parent),
    8. ui(new Ui::MainWindow)
    9. {
    10. ui->setupUi(this);
    11. }
    12.  
    13. MainWindow::~MainWindow()
    14. {
    15. delete ui;
    16. }
    17.  
    18. void MainWindow::changeEvent(QEvent *e)
    19. {
    20. QMainWindow::changeEvent(e);
    21. switch (e->type()) {
    22. case QEvent::LanguageChange:
    23. ui->retranslateUi(this);
    24. break;
    25. default:
    26. break;
    27. }
    28. }
    29.  
    30. Q_EXPORT_PLUGIN2(ui, MainWindow)
    To copy to clipboard, switch view to plain text mode 

    As you can see, main window is the code created by QtCreator and the interface IVisible has only two functions. My problem is there are errors while compiling:

    undefined reference to `MainWindow::~MainWindow()'
    undefined reference to `MainWindow::changeEvent(QEvent*)'

    I think this is because I only add mainwindow.h to app.pro and Qt cannot find the cpp. But I should do this in order to create the main window in main() function. So how can I solve this problem? Must I create main window in application or create it in a function in the plugin and call it in main()?

    Thank you!

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Plugin compiler error: undefined reference

    if you call any methods of the type MainWindow this type's code is needed in your app;
    if you cast plugin to IVisible instead, you might have more luck.

  3. #3
    Join Date
    Sep 2009
    Location
    Nanjing, China
    Posts
    46
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Plugin compiler error: undefined reference

    Quote Originally Posted by caduel View Post
    if you call any methods of the type MainWindow this type's code is needed in your app;
    if you cast plugin to IVisible instead, you might have more luck.
    What I want to try to put all UI components into one dll so MainWindow is also in that dll. Then I should call this
    MainWindow in main() function in order to show it. I checked the class name returned by QPluginLoader::instance()
    and it was a MainWindow. I tried to cast it to IVisible using
    Qt Code:
    1. IVisible * v = (IVisible *)plugin;
    2. v->setVisible(true);
    To copy to clipboard, switch view to plain text mode 
    note that it is not a QObject so I cann't using qobject_cast but it also had the same problem. If I remove HEADERS += mainwindow.h
    in app.pro, it is all right. But there is an runtime error when setVisible() called.

    So how should I do? If I just want to get the MainWindow instance, how should I code? Or I can try IVisible but whatis the
    runtime error?

  4. #4
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Plugin compiler error: undefined reference

    Why you want your UI components to be a plugin? Can it be a regular library? You will get the .dll linked to your app.
    The main idea of plugins is that there is defined one interface (an abstract C++ class) and you communicate with plugins trough that interface (that's why it is called interface...).
    So in your case you should cast objects to IVisible and use only IVisible methods on loaded plugins (otherwise there are some methods in MainWindow declared in mainwindow.h but NOT IMPLEMENTED anywhere in your app...) or just get rid of this IVisible, make your plugin a regular library and link it to your app (like you already did).
    But remember that in dll you need proper dllimport and dllexport -> create a new project "C++ library" in QtCreator to see how to deal with dll import and export (look at *_global.h, .pro file and class declaration).
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  5. #5
    Join Date
    Sep 2009
    Location
    Nanjing, China
    Posts
    46
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Plugin compiler error: undefined reference

    Quote Originally Posted by faldżip View Post
    Why you want your UI components to be a plugin? Can it be a regular library? You will get the .dll linked to your app.
    The main idea of plugins is that there is defined one interface (an abstract C++ class) and you communicate with plugins trough that interface (that's why it is called interface...).
    So in your case you should cast objects to IVisible and use only IVisible methods on loaded plugins (otherwise there are some methods in MainWindow declared in mainwindow.h but NOT IMPLEMENTED anywhere in your app...) or just get rid of this IVisible, make your plugin a regular library and link it to your app (like you already did).
    But remember that in dll you need proper dllimport and dllexport -> create a new project "C++ library" in QtCreator to see how to deal with dll import and export (look at *_global.h, .pro file and class declaration).
    Thank you! I also think I'm creating a shared library not a plugin. But I don't know how to import this library into my project? It says that I should use QLibrary. But the document only shows how to import functions from dll while I just export classes. Could you tell me how should I do if I've got the dll library by creating shared library project?

    I just paste header file and dll file into the same file and add LIBS += -e:/works/test/ui.dll file. Then recompile this project there is no error but when I run it, the exit code is not 0 and no window shown. I don't know why?
    Last edited by FinderCheng; 30th April 2010 at 11:31.

  6. #6
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Plugin compiler error: undefined reference

    This:
    Qt Code:
    1. LIBS += -LE:\documents\test\app\plugins -lui
    To copy to clipboard, switch view to plain text mode 
    looks in E:\documents\test\app\plugins for ui.lib. And instead of copying mainwindow.h just add INCLUDEPATH += path/where/headers/are in app.pro file and remove it from HEADERS.
    And show your ui.pro and mainwindow.h.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  7. #7
    Join Date
    Sep 2009
    Location
    Nanjing, China
    Posts
    46
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Plugin compiler error: undefined reference

    Hi! I tried to create a library instead of a plugin, so I had to modified existing code as following:

    test.pro
    Qt Code:
    1. # there is no any change
    2. TEMPLATE = subdirs
    3. CONFIG += ordered
    4. SUBDIRS += ui app
    To copy to clipboard, switch view to plain text mode 

    app.pro
    Qt Code:
    1. TARGET = test
    2. TEMPLATE = app
    3. CONFIG += debug
    4. SOURCES += main.cpp
    5. LIBS += E:/documents/test/app/lib/ui.dll # I realized this is the correct syntax on Windows
    6. INCLUDEPATH += ../ui
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. // Since this is a library, I think I should use it just like QtGui and so on, so I include necessary head files and create an instance as usual
    2. #include <QtGui/QApplication>
    3. #include "mainwindow.h"
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication a(argc, argv);
    8. MainWindow win;
    9. win.show();
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    ui.pro
    Qt Code:
    1. # also I should edit this file in order to generate a library instead of a plugin
    2. TARGET = ui
    3. TEMPLATE = lib
    4. DEFINES += UI_LIBRARY
    5. CONFIG += debug
    6. SOURCES += mainwindow.cpp
    7. HEADERS += mainwindow.h \
    8. ui_global.h
    9. FORMS += mainwindow.ui
    10. DESTDIR = ../app/lib
    To copy to clipboard, switch view to plain text mode 

    ui_global.h
    Qt Code:
    1. #ifndef UI_GLOBAL_H
    2. #define UI_GLOBAL_H
    3.  
    4. #include <QtCore/qglobal.h>
    5.  
    6. #if defined(UI_LIBRARY)
    7. # define UISHARED_EXPORT Q_DECL_EXPORT
    8. #else
    9. # define UISHARED_EXPORT Q_DECL_IMPORT
    10. #endif
    11.  
    12. #endif // UI_GLOBAL_H
    To copy to clipboard, switch view to plain text mode 

    mainwindow.h
    Qt Code:
    1. // OK, this is almost what QtCreator generated for me, I only added show() function in order to let Qt export this function, am I right?
    2. #ifndef MAINWINDOW_H
    3. #define MAINWINDOW_H
    4.  
    5. #include <QMainWindow>
    6. #include "ui_global.h"
    7.  
    8. namespace Ui {
    9. class MainWindow;
    10. }
    11.  
    12. class UISHARED_EXPORT MainWindow : public QMainWindow {
    13. Q_OBJECT
    14. public:
    15. MainWindow(QWidget *parent = 0);
    16. ~MainWindow();
    17. void show();
    18. protected:
    19. void changeEvent(QEvent *e);
    20. private:
    21. Ui::MainWindow *ui;
    22. };
    23.  
    24. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9. }
    10.  
    11. MainWindow::~MainWindow()
    12. {
    13. delete ui;
    14. }
    15.  
    16. void MainWindow::changeEvent(QEvent *e)
    17. {
    18. QMainWindow::changeEvent(e);
    19. switch (e->type()) {
    20. case QEvent::LanguageChange:
    21. ui->retranslateUi(this);
    22. break;
    23. default:
    24. break;
    25. }
    26. }
    27.  
    28. void MainWindow::show()
    29. {
    30. this->show();
    31. }
    To copy to clipboard, switch view to plain text mode 

    OK, when I recompile this project, there is nothing wrong. But I could not run it. It says that "E:\documents\test\app\debug\test.exe exited with code -1073741819" although I have copied ui.dll to the same directory of test.exe.

  8. #8
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Plugin compiler error: undefined reference

    Quote Originally Posted by FinderCheng View Post
    app.pro
    Qt Code:
    1. TARGET = test
    2. TEMPLATE = app
    3. CONFIG += debug
    4. SOURCES += main.cpp
    5. LIBS += E:/documents/test/app/lib/ui.dll # I realized this is the correct syntax on Windows
    6. INCLUDEPATH += ../ui
    To copy to clipboard, switch view to plain text mode 
    I don't think so. Do you have ui.lib file generated with ui.dll. I'm not sure if DESTDIR works with it so check in debug folder. This ui.lib is the right file to link with while compiling and ui.dll it the right file to link with in runtime. And the previous syntax was okay - I use it on both - Windows and Linux.
    main.cpp
    Qt Code:
    1. // Since this is a library, I think I should use it just like QtGui and so on, so I include necessary head files and create an instance as usual
    2. #include <QtGui/QApplication>
    3. #include "mainwindow.h"
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication a(argc, argv);
    8. MainWindow win;
    9. win.show();
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 
    That's correct.

    ui.pro
    Qt Code:
    1. # also I should edit this file in order to generate a library instead of a plugin
    2. TARGET = ui
    3. TEMPLATE = lib
    4. DEFINES += UI_LIBRARY
    5. CONFIG += debug
    6. SOURCES += mainwindow.cpp
    7. HEADERS += mainwindow.h \
    8. ui_global.h
    9. FORMS += mainwindow.ui
    10. DESTDIR = ../app/lib
    To copy to clipboard, switch view to plain text mode 
    This file looks okay now (for library).
    mainwindow.h
    Qt Code:
    1. // OK, this is almost what QtCreator generated for me, I only added show() function in order to let Qt export this function, am I right?
    To copy to clipboard, switch view to plain text mode 
    No, it is already exported in QtGui.dll as it is part of QWidget class.
    OK, when I recompile this project, there is nothing wrong.
    Are you sure? You are qmaking and building test.pro or app.pro?
    Open VS command prompt, go to the app dir and run:
    Qt Code:
    1. qmake && nmake
    To copy to clipboard, switch view to plain text mode 
    and see what you get.
    But I could not run it. It says that "E:\documents\test\app\debug\test.exe exited with code -1073741819" although I have copied ui.dll to the same directory of test.exe.
    Use Dependency Walker to check library dependencies of your app.exe. And did you try also running it from outside of QtCreator just by double clicking test.exe?
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  9. #9
    Join Date
    Sep 2009
    Location
    Nanjing, China
    Posts
    46
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Plugin compiler error: undefined reference

    Quote Originally Posted by faldżip View Post
    I don't think so. Do you have ui.lib file generated with ui.dll. I'm not sure if DESTDIR works with it so check in debug folder. This ui.lib is the right file to link with while compiling and ui.dll it the right file to link with in runtime. And the previous syntax was okay - I use it on both - Windows and Linux.
    Sorry, I check both DESTDIR and debug folders and there is no such file called ui.lib.

    Quote Originally Posted by faldżip View Post
    Are you sure? You are qmaking and building test.pro or app.pro?
    Yes, I rebuild project with test.pro and app.pro each time and there is no error.

    Quote Originally Posted by faldżip View Post
    Open VS command prompt, go to the app dir and run:
    Qt Code:
    1. qmake && nmake
    To copy to clipboard, switch view to plain text mode 
    and see what you get.
    I'm sorry but I have no VS compiler. So I open app.pro as a separate project and build it, nothing wrong here.

    Quote Originally Posted by faldżip View Post
    Use Dependency Walker to check library dependencies of your app.exe. And did you try also running it from outside of QtCreator just by double clicking test.exe?
    I have a little trouble with Dependency Walker. What results should I looking for? I checked test.exe(not app.exe, TARGET = test in app.pro) and its library contains ui.dll. And I tried to open test.exe outside QtCreator, no window opened...
    Last edited by FinderCheng; 1st May 2010 at 16:54.

Similar Threads

  1. Getting Undefined Reference Error
    By A.H.M. Mahfuzur Rahman in forum Qt Programming
    Replies: 2
    Last Post: 22nd June 2009, 02:12
  2. Error - undefined reference qMain
    By tpf80 in forum Newbie
    Replies: 4
    Last Post: 23rd February 2009, 07:54
  3. Error : undefined reference to `vtable for MyClass'
    By joseph in forum Qt Programming
    Replies: 23
    Last Post: 15th June 2007, 11:21
  4. error undefined reference ...............
    By amit_pansuria in forum Qt Programming
    Replies: 2
    Last Post: 8th June 2007, 14:28
  5. Undefined Reference error!!!
    By Kapil in forum Newbie
    Replies: 25
    Last Post: 28th March 2006, 12:03

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.