Results 1 to 7 of 7

Thread: Understanding how to link dynamic / static library with Qt and MinGW

  1. #1
    Join Date
    Apr 2010
    Location
    Italia
    Posts
    149
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Understanding how to link dynamic / static library with Qt and MinGW

    Hello everyone, I have a bit confused about how to write in the project file .pro for QtCreator to link a dynamic and static library. I use the MinGW compiler (making a Windows).
    Suppose you have the following libraries:
    1 - Dynamic: vettore.dll
    2 - static: matrice.a
    Suppose also that the two libraries are in the directory:
    1 - C :/ dynamic dynamic library vettore.dll
    2 - C :/ static for static library matrice.a

    The project file is called test.pro. How should I write it for him to link the libraries to my program that do the test named main.cpp ?

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Understanding how to link dynamic / static library with Qt and MinGW

    LIBS += -Lc:/dynamic -Lc:/static -lvettore -lmatrice

    http://doc.qt.digia.com/qt/qmake-project-files.html

    for windows you will need a .lib to go with the .dll for linking, I believe
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  3. #3
    Join Date
    Apr 2010
    Location
    Italia
    Posts
    149
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Understanding how to link dynamic / static library with Qt and MinGW

    Thank you. To obtain a dynamic library. Lib and. Dll with QtCreator, how should I do? I can write a very simple example (like Hello World) ?

  4. #4
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Understanding how to link dynamic / static library with Qt and MinGW

    using the creator gui:
    new project -> c++ library -> Type= Shared library

    This gives me:
    pro file
    Qt Code:
    1. QT -= core gui
    2.  
    3. TARGET = helloworld_dll
    4. TEMPLATE = lib
    5.  
    6. DEFINES += HELLOWORLD_DLL_LIBRARY
    7.  
    8. SOURCES += helloworld_dll.cpp
    9.  
    10. HEADERS += helloworld_dll.h\
    11. helloworld_dll_global.h
    To copy to clipboard, switch view to plain text mode 

    header
    Qt Code:
    1. #ifndef HELLOWORLD_DLL_H
    2. #define HELLOWORLD_DLL_H
    3.  
    4. #include "helloworld_dll_global.h"
    5.  
    6. class HELLOWORLD_DLLSHARED_EXPORT Helloworld_dll {
    7. public:
    8. Helloworld_dll();
    9.  
    10. void print();
    11. };
    12.  
    13. #endif // HELLOWORLD_DLL_H
    To copy to clipboard, switch view to plain text mode 

    source
    Qt Code:
    1. #include "helloworld_dll.h"
    2.  
    3. #include <iostream>
    4. Helloworld_dll::Helloworld_dll()
    5. {
    6. }
    7.  
    8. void Helloworld_dll::print()
    9. {
    10. std::cout << "hi from dll\n";
    11. }
    To copy to clipboard, switch view to plain text mode 

    dll defines
    Qt Code:
    1. #ifndef HELLOWORLD_DLL_GLOBAL_H
    2. #define HELLOWORLD_DLL_GLOBAL_H
    3.  
    4. #include <QtCore/qglobal.h>
    5.  
    6. #if defined(HELLOWORLD_DLL_LIBRARY)
    7. # define HELLOWORLD_DLLSHARED_EXPORT Q_DECL_EXPORT
    8. #else
    9. # define HELLOWORLD_DLLSHARED_EXPORT Q_DECL_IMPORT
    10. #endif
    11.  
    12. #endif // HELLOWORLD_DLL_GLOBAL_H
    To copy to clipboard, switch view to plain text mode 
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  5. #5
    Join Date
    Apr 2010
    Location
    Italia
    Posts
    149
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Understanding how to link dynamic / static library with Qt and MinGW

    thanks amleto, for the sample code. I discovered, however, that it is not enough that automatically creates the wizard, you have in the project file (.pro file), add this line:
    Qt Code:
    1. CONFIG += dll
    To copy to clipboard, switch view to plain text mode 
    so the compiler does not find the function main (being a library, there is no main).

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Understanding how to link dynamic / static library with Qt and MinGW

    No you don't. The linker will only expect to find a main() if the template type is app. Amletos' example builds cleanly and correctly with MingW.

    When you build a "lib" project with you will get a shared library by default on Linux and Windows. All that adding "dll" to CONFIG does is request a shared library again.
    If you build the project with the MingW tools there will be a resulting helloworld_dll.dll file and a libhelloworld_dll.a file.
    If you build the same project with the Microsoft compiler you will get helloworld_dll.dll and helloworld_dll.lib.
    The linkers use information from the .a or .lib file to arrange linking to the the .dll at run time. Only the .dll file is required at run time.

  7. #7
    Join Date
    Apr 2010
    Location
    Italia
    Posts
    149
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Understanding how to link dynamic / static library with Qt and MinGW

    Thanks
    ChrisW67

Similar Threads

  1. Replies: 0
    Last Post: 29th March 2012, 19:56
  2. Static library link trouble
    By waynew in forum Qt Programming
    Replies: 2
    Last Post: 18th December 2011, 17:43
  3. Generating a static and dynamic library
    By elcuco in forum Qt Programming
    Replies: 3
    Last Post: 20th August 2011, 11:46
  4. Adding Library Methods - Static Vs Dynamic
    By waynew in forum Qt Programming
    Replies: 1
    Last Post: 15th January 2010, 16:16
  5. How to use a Dynamic Link Library with QT / C++.
    By nivaldonicolau in forum Newbie
    Replies: 5
    Last Post: 29th April 2009, 14:05

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.