Page 1 of 2 12 LastLast
Results 1 to 20 of 30

Thread: Static and Dynamic Libraries

  1. #1
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Static and Dynamic Libraries

    How does Qt differentiate between static and dynamic libraries when both are included in the application pro file? The doc is not clear and the syntax appears to be the same for both. Just by the library file extension (.a Vs .dll on Windows)? I have listed both types in the pro file and the static (<lib>.a) is working but not the dynamic (<lib.dll>) is not and won't load with QLibrary.

  2. #2
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Static and Dynamic Libraries

    The syntax is the same. The .a library basically contains object code similar or equal to the .o files created during compilation. The dll (or shared object) is actually very similar to an executable. When you run your application, the compile time linked shared object will have to be in the search path, which on windows will typically be the application directory. Whether QLibrary can load it or not depends a bit on how you use it. You'll have to elaborate there what you want and what you are doing right now.
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  3. The following user says thank you to franz for this useful post:

    waynew (24th November 2010)

  4. #3
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Static and Dynamic Libraries

    Thanks franz. That helps. I tried linking the static library but it gives me a linker error( a reference error to the library function I call), not recognizing the first function call I make to the library. Yes, I have the headers included. Also, if I comment out the library function call, the build is ok, but the exe stays the same size, which indicates to me the library is not being included.

    So, I tried using the dll. I put it in my application build directory and included the headers. the build is ok, but QLibrary says it can't load the library. Dependency walker only shows a few warnings regarding Microsoft dlls.

    Also, if I pick a random MS dll and put it in my application build directory, QLibrary loads it ok.

    At this point, I am thinking there must be something wrong with my library (I built it from source using Mingw/msys).

  5. #4
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Static and Dynamic Libraries

    Whatever you're building now and trying to link to the library will have to be built using the same tools the library was built with - mingw. It may have to be rebuilt to use similar flags - for example, you can't link a library built in 64-bit mode with an application built in 32-bit mode.

  6. #5
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Static and Dynamic Libraries

    Thanks Six, I built the library on the same 32 bit Windows machine I made my Qt application on.
    So, that should not be a problem, right?

  7. #6
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Static and Dynamic Libraries

    It may not be the problem, at least if they were built with the same compiler. Post the actual error message you're getting.

  8. #7
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Static and Dynamic Libraries

    Well, when I try to link to the static library, the error message is:
    C:\CPP\QT_Projects\HRLogger_Development/lotwupload.cpp:23: undefined reference to `_imp__tqsl_init@0'

    For reference, the pro file entry is:
    Qt Code:
    1. INCLUDEPATH += C:\mingw\msys\1.0\home\wayne\tqsllib\include
    2. LIBS += -L"C:\mingw\msys\1.0\home\wayne\tqsllib\lib" -ltqsllib
    To copy to clipboard, switch view to plain text mode 

    The library .h files are included in my application.

    And the first library call (where it fails) is :
    Qt Code:
    1. int ret = tqsl_init();
    To copy to clipboard, switch view to plain text mode 

    And this function is defined in tqsllib.h which is included in my application.

    So what could I be doing wrong here?

  9. #8
    Join Date
    Aug 2009
    Location
    coimbatore,India
    Posts
    314
    Thanks
    37
    Thanked 47 Times in 43 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Static and Dynamic Libraries

    Hi ,
    Open the Makefile of ur project, see LIBS + line
    if double quotes is missing in pro file,
    Try LIBS += -L$$quote("C:\mingw\msys\1.0\home\wayne\tqsllib\lib ")

    or

    may be u can try by adding the library's path in the PATH variable. u can also try path in QtCreator's projects->Run Settings

    Try run qmake , clean & compile the project entirely

    hope it helps

    Thnks
    Bala
    Last edited by BalaQT; 25th November 2010 at 05:39.

  10. #9
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Static and Dynamic Libraries

    Forward slashes. If possible, always use forward slashes.
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  11. #10
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Static and Dynamic Libraries

    Thanks folks, tried that stuff, no luck. Same problem.
    If I comment out the call to the library function, and do a build, both with and without the LIBS lines in the pro file, the size of the application exe does not change.

    Does anyone else think this is an indicator that the library is not included in the exe? And that would cause the linker error when the library function call is left in?

  12. #11
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Static and Dynamic Libraries

    For whatever reason, the linker is not able to locate the library you're trying to link to. It may be a quoting or backslash issue, as others have suggested, or simply a case of the library not where you're telling the linker to look for it. Check your path, make sure the library is really there and that the path it's on matches the one in your project file.

  13. #12
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Static and Dynamic Libraries

    Ok, I checked all of that and it is ok.

    Here is where part of my newbie confusion comes in. The doc for building the tqsllib says to use the configure command with --disable-shared, which I did. Then it says after running "make install" "This should result in the creation of the tqsllib2.dll" Which it did.

    Wouldn't using --disable-shared mean that you are creating a static library? Like a lib.a file? If so, then why does it create a dll?
    It also creates the files libtqsllib.a libtqsllib.la and tqsllibd.a in the lib directory where I built it. tqsllib2.dll is in the bin directory.

    I have been trying to use the tqsllib.a file as a static library. Is this incorrect?

    Can someone explain this to me? I am really library confused.
    At least I was able to build the qserialdevice static library and include it in my application and it is working fine. But this library is just not the same.

  14. #13
    Join Date
    Sep 2010
    Posts
    145
    Thanks
    1
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Static and Dynamic Libraries

    Looks like your answer lies here:
    Qt Code:
    1. #if defined(__WIN32__) && !defined(TQSL_NODLL)
    2. #ifdef TQSLLIB_DEF
    3. #define DLLEXPORT __stdcall __declspec(dllexport)
    4. #define DLLEXPORTDATA __declspec(dllexport)
    5. #else
    6. #define DLLEXPORT __stdcall __declspec(dllimport)
    7. #define DLLEXPORTDATA __declspec(dllimport)
    8. #endif
    9. #else
    10. #define DLLEXPORT
    11. #define DLLEXPORTDATA
    12. #endif
    To copy to clipboard, switch view to plain text mode 

    You aren't defining any of these symbols yourself are you?
    C:\CPP\QT_Projects\HRLogger_Development/lotwupload.cpp:23: undefined reference to `_imp__tqsl_init@0
    Here, the linker expects to find a function with the stdcall convention. It seems that you build the library one way, and you are trying to link against it in other.

    Examine your defines for building it, and check your defines for linkage.

  15. #14
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Static and Dynamic Libraries

    Thanks Timoteo. I can see that you are trying to tell me something important, but being a C++ newbie, I am having a hard time understanding it.

    Here are my questions/comments:

    1. When I compile the tqsl library and the files tqsllib.a and tqsllib2.dll are created - isn't the tqsllib.a the static and the tqsllib2.dll the dynamic?
    2. If the configure is run with --disable-shared, why does it create the dll?
    3. You said "It seems that you build the library one way, and you are trying to link against it in other." I am just trying to link against the static tqsllib.a I thought that was what I had in my pro file. So what do you mean?
    4. I don't think I am defining any symbols. I am just calling the first function I need in the library that is tqsl_init();
    5. Why does one need the code you posted?
    6. What do you mean by "Examine your defines for building it, and check your defines for linkage"?

    Sorry for all of the newbie questions to your post, I know you are trying to help.
    I am really trying to get this - I have 5 C++ books and 5 Qt books, but none of them really explain how to work with external libraries.
    If I could find a good reference that explains all of this I wouldn't have to waste all of your time with dumb questions.

  16. #15
    Join Date
    Sep 2010
    Posts
    145
    Thanks
    1
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Static and Dynamic Libraries

    The code I posted is from the header of the library in question. It uses a set of defined symbols to determine whether you are importing or exporting symbols. It also determines the calling convention that the linker should expect (__cdecl for static lib, and __stdcall for dynamic lib). From another quick glance it appears that you need to define TQSL_NODLL in order to link statically.
    your .pro
    Qt Code:
    1. DEFINES += TQSL_NODLL
    To copy to clipboard, switch view to plain text mode 

    It also appears that you need to define TQSL_NODLL (not in source, but passed to the compiler) when building the library to achieve static linkage.

  17. The following user says thank you to Timoteo for this useful post:

    waynew (26th November 2010)

  18. #16
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Static and Dynamic Libraries

    Thanks Timoteo, that helps.

    The code in the library is
    Qt Code:
    1. /** Initialize the tQSL library
    2.   *
    3.   * This function should be called prior to calling any other library functions.
    4.   */
    5. DLLEXPORT int tqsl_init();
    To copy to clipboard, switch view to plain text mode 

    I have included the tqsllib.h in my application, and I call the init function like this
    Qt Code:
    1. int ret = tqsl_init();
    To copy to clipboard, switch view to plain text mode 

    And the build error is:
    C:\CPP\QT_Projects\HRLogger_Development/lotwupload.cpp:19: undefined reference to `tqsl_init'

    So what am I missing now?

  19. #17
    Join Date
    Sep 2010
    Posts
    145
    Thanks
    1
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Static and Dynamic Libraries

    Did you rebuild the library as static? Could you upload your project somewhere and send it to me via PM or attach it here?


    Added after 5 minutes:


    Just some text to augment the replies to your previous questions:

    It is important to understand just what a function declaration is. I am going to supply one and break it down while taking the win32 platform as an example.
    Qt Code:
    1. int __stdcall doSomethingAwesome(int coolArg);
    To copy to clipboard, switch view to plain text mode 

    Moving from left to right, we start with the return type. Now everyone knows what this is, but why does the compiler actually need it? The return type in this example tells the complier that the EAX register
    will have a signed integer value in it when the procedure returns. In other words, the return type specifies the post-condition of the EAX register. Most know that compilers can much more easily inline
    a function that returns nothing (void), and now you may know why. That's because the compiler knows that you have no dependency on the post-condition of EAX and it now has an extra register to use
    when inlining.

    Next is the calling convention. The calling convention tells the compiler how the parameters must be passed into the procedure and who is responsible for balancing the stack upon procedure return (caller versus callee). For instance, on win32 the parameters are pushed onto the stack, in win64 the parameters are passed in registers.

    Next is the function identifier. Pretty much self-explanatory. Just note that this can be decorated or undecorated (extern "C").

    Finally, we have the parameter. The most important thing that the compiler has to know about the parameter is the SIZE of the type. The compiler cannot generate correct procedure prologue without knowing this. In other words, the compiler cannot deduce the proper pre-conditions for entry into the procedure and cannot setup a correct stack frame for it. The same applies to procedure epilogue generation.

    Most of this stuff is handled transparently by the compiler and you'd never have to know the compiler's reasons. When you get into mixed language projects, though, it comes in handy to know this stuff.

    How this applies to your problem: The calling convention must be specified the same way when building the library and when linking to it.
    Last edited by Timoteo; 26th November 2010 at 20:20.

  20. #18
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Static and Dynamic Libraries

    I definitely still have a lot to learn about C++, That's what you get with an old hardware engineer!

    I created a small test project but the files were too big to attach and I couldn't see a way to send an attachment on a PM so I put them on my website. Go to k4elo.net and the Download page. 3 files - the test project, the tqsl compiled library, and the config log from the library compile.

    And thanks again for all of your help.

  21. #19
    Join Date
    Sep 2010
    Posts
    145
    Thanks
    1
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Static and Dynamic Libraries

    I had a look at the lib requirements and saw that you need the Windows DDK (WDK) in order to build it. I've been busy so I've not had the chance to setup my environment here at home for a build. When I establish a baseline that will build and link properly, I will post the results back to you (unless you beat me to it).

  22. The following user says thank you to Timoteo for this useful post:

    waynew (28th November 2010)

  23. #20
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Static and Dynamic Libraries

    Many thanks Timoteo I really appreciate all of your efforts, you are a hero.
    By sheer luck and a great deal of perspiration and trial and error, I have gotten the dll to load into my application in a test project test_lib2.
    All I did was to put the includepath in the pro file, include the library header, then put the tqsllib2.dll into the debug directory and used QLibrary to load the library.
    qDebug reports the library is loaded.
    Now I am working on getting the tqsl_init() function resolved. No success yet.
    I don't know if I am on the right path or not, but it looks promising.

    I'll report back later tonight or tomorrow morning with my results.

    I am still confused about library building. That is the library.a vs the library.dll when you build a library with --disable-shared.
    I thought that would create a library.a that you could link statically. Appears not to be the case as I had no luck with linking tqsllib.a statically.
    Just shows you how much I don't know about C++ yet.

    Hoping that someone puts an entry into the wiki explaining external library usage.

Similar Threads

  1. Static or Dynamic !!
    By salmanmanekia in forum Newbie
    Replies: 7
    Last Post: 5th June 2010, 15:24
  2. Resources in dynamic libraries
    By seneca in forum Qt Programming
    Replies: 0
    Last Post: 19th January 2009, 17:26
  3. Dynamic link libraries
    By nareshqt in forum Qt Programming
    Replies: 6
    Last Post: 22nd April 2008, 06:20
  4. having both static and dynamic libraries?
    By gfunk in forum Qt Programming
    Replies: 2
    Last Post: 7th May 2007, 07:33
  5. Hot to define the directory for the dynamic libraries?
    By Dark_Tower in forum Qt Programming
    Replies: 5
    Last Post: 28th December 2006, 22:15

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.