Results 1 to 8 of 8

Thread: QT Creator - Creating and Using LIBS

  1. #1
    Join Date
    Jul 2009
    Posts
    17
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default QT Creator - Creating and Using LIBS

    Hi all,

    I know there has been related questions to this on the forums. I've searched through them but none of which answer my particular problem.

    Firstly, I'm a nooby, so go easy. I'm trying to learn c++ for myself and have chosen the following configuration to get started;
    OS: XP
    toolkit: QT 4.5.1
    IDE: QT Creator 1.2

    Everything is up and running and I've created some *basic* progs.

    However, I'm stuck on the following problem. I wanted to learn how to use libraries. So I created a shared library (called test2library) using the QT Creator wizard and added some silly functionality. Unfortunately, I'm not able to use the linked library in my main project (called imaginatively mainProject).

    // Please also let me know of any bad practice etc. As I said, I'm trying to learn here so all comments welcome in addition to the solution.//

    So my shared library contains four files; test2library.h .cpp .pro and _global.h

    Here are the changes I made to the header and implementation files.


    test2library.h
    Qt Code:
    1. #ifndef TEST2LIBRARY_H
    2. #define TEST2LIBRARY_H
    3.  
    4. #include "test2Library_global.h"
    5. #include <qDebug>
    6.  
    7. class TEST2LIBRARYSHARED_EXPORT Test2Library {
    8. public:
    9. Test2Library();
    10. void doit();
    11. };
    12.  
    13. #endif // TEST2LIBRARY_H
    To copy to clipboard, switch view to plain text mode 

    test2library.cpp
    Qt Code:
    1. #include "test2library.h"
    2.  
    3.  
    4. Test2Library::Test2Library()
    5. {
    6. }
    7.  
    8. void Test2Library::doit()
    9. {
    10. qDebug() << "In test2Library";
    11. }
    To copy to clipboard, switch view to plain text mode 

    OK, everything compiled OK and seems to be running.

    So I go back to my mainProject which is a GUI frame, also with some silly code in it. I open up my mainProject.pro and add the following lines;

    Qt Code:
    1. <snip>
    2.  
    3. INCLUDEPATH += "..\test2Library\test2library"
    4. win32:LIBS += -L"..\test2Library\test2library\debug" -ltest2Library
    To copy to clipboard, switch view to plain text mode 

    I save everything and rebuild...everything seems fine.

    Then in my mainProject I add the header;

    Qt Code:
    1. #include "../test2Library/test2library/test2library.h"
    2.  
    3. void MainWindow::on_pushButton_clicked()
    4. {
    5. qDebug() << "Button pressed!";
    6. Test2Library *t = new Test2Library();
    7. t->doit();
    8. }
    To copy to clipboard, switch view to plain text mode 

    The program builds fine, but when I run it, I get

    Qt Code:
    1. ..../mainProgram.exe exited with code -1073741515
    To copy to clipboard, switch view to plain text mode 

    Can anyone give me any helpful pointers. I've searched for solutions but can't seem to find anything that works. I've simplified the entire process as much as possible in order to isolate the problem. It seems that there is a problem with linking the libraries?

    Cheers,
    Mick
    Last edited by floyd.pepper; 22nd July 2009 at 05:34.

  2. #2
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QT Creator - Creating and Using LIBS

    To create static lib, just add these lines in your .pro file
    Qt Code:
    1. TEMPLATE = lib
    2. CONFIG += staticlib
    To copy to clipboard, switch view to plain text mode 

    And to use this library in main program. Add these lines in .pro file.
    Qt Code:
    1. LIBS += -Lpath/to/lib/file
    2. LIBS += -lnameoflibrary
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QT Creator - Creating and Using LIBS

    I think your application is fine. Try adding CONFIG+=console to your project file, rerun qmake, rebuild and see if it changes anything.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Jul 2009
    Posts
    17
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QT Creator - Creating and Using LIBS

    Thanks for the replies everyone...

    @ yogeshgokul:
    I've added the lines you recommended. I'm not quite sure of the syntax for the LIBS though...here is what I have right now

    Qt Code:
    1. win32:INCLUDEPATH += ../test2Library/test2library
    2. win32:debug:LIBS += -L../test2Library/test2library/debug
    3. LIBS += -ltest2Library
    To copy to clipboard, switch view to plain text mode 

    ...and it seems to build fine but still not run

    @ wysota: I'm not sure if I add that line to the library pro or the mainProject pro?

    Update

    If I copy the dll file into the same folder as the mainProject.exe then everything runs in the IDE. However, if I remove the dll and leave it in it's build location then I get the error "exited with code -1073741515"

    Can you tell me if there is something else I should set? Does the exe know to look for the dll in location specified by LIBS?
    Last edited by floyd.pepper; 22nd July 2009 at 19:34.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QT Creator - Creating and Using LIBS

    No, the executable is not responsible for knowing where the library is. That's the job of the system's dynamic linker that gets executed before your application launches and links all needed libraries to the process address space. The dll needs to be placed in a directory where the linker will look for it. The directory where the executable resides is one of those directories when we speak about Windows.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. The following user says thank you to wysota for this useful post:

    floyd.pepper (22nd July 2009)

  7. #6
    Join Date
    Jul 2009
    Posts
    17
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QT Creator - Creating and Using LIBS

    Thanks for your support on this wysota.

    So, from my understanding, neither the INCLUDEPATH nor the LIBS variables tell the exe where to look for the dll.

    If that is the case, then I can think of two solutions. Please let me know which one you think is most viable;

    1. Option 1
      I can change the .pro of the library project. Here I change the TARGET variable from
      Qt Code:
      1. TARGET = test2Library
      To copy to clipboard, switch view to plain text mode 
      to
      Qt Code:
      1. debug:TARGET = ../../../mainProject/debug/test2Library
      To copy to clipboard, switch view to plain text mode 

      In this solution, the dll is placed directly into the mainProject folder with the mainProject.exe. This will become a problem if I want to use the same dll with multiple applications. I would need to create a copy of the dll in each of the application folders.
    2. Option 2
      In option two, I can simply add the directory of the dll to my system PATH. However, this will get messy after a while if I keep adding to my PATH for each dll project I create.


    Can you tell me if there is a third solution? Surely I can add the linker paths in QT as I did it quite often in MS Visual Studio.

    Again, thanks again for taking the time to help out a newbie on this.

    All the best,
    Mick

  8. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QT Creator - Creating and Using LIBS

    Quote Originally Posted by floyd.pepper View Post
    So, from my understanding, neither the INCLUDEPATH nor the LIBS variables tell the exe where to look for the dll.
    Yes, that's correct. They are only meaningful during the compilation.

    If that is the case, then I can think of two solutions. Please let me know which one you think is most viable;

    [LIST=1][*]Option 1
    I can change the .pro of the library project. Here I change the TARGET variable from
    Qt Code:
    1. TARGET = test2Library
    To copy to clipboard, switch view to plain text mode 
    to
    Qt Code:
    1. debug:TARGET = ../../../mainProject/debug/test2Library
    To copy to clipboard, switch view to plain text mode 

    In this solution, the dll is placed directly into the mainProject folder with the mainProject.exe.
    How about using the DESTDIR variable?

    This will become a problem if I want to use the same dll with multiple applications. I would need to create a copy of the dll in each of the application folders.
    That's basically the unfortunate choice on Windows. You either copy the dll to the system folder or you have to copy it around to every binary it uses the library.

    Can you tell me if there is a third solution? Surely I can add the linker paths in QT as I did it quite often in MS Visual Studio.
    Linker paths are related to compilation, not execution. That's exactly the same what -L does for GCC/MinGW.

    I think there is a way to do what you want through manifest files but unfortunately I know nothing about them so I can't be of any help.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #8
    Join Date
    Jul 2009
    Posts
    17
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QT Creator - Creating and Using LIBS

    Cheers wysota,

    I'm using DESTDIR now to place the dll with the binary of my mainProject. I will look into manifest files another day I think.

    Again thanks for the support.

    /Mick

Tags for this Thread

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.