Results 1 to 20 of 30

Thread: Static and Dynamic Libraries

Hybrid View

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

    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.

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

    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.

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

    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.

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

    waynew (26th November 2010)

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

    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?

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

    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.

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

    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.

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

    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).

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

    waynew (28th November 2010)

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

    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.

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

    Default Re: Static and Dynamic Libraries

    This library definitely is different (in the bad way). For example, I cannot get it to build cleanly on Linux due to it being bound to an older OpenSSL (if I hack around on it, I can get it to build, but it is nasty). Btw, what I said about it requiring the Windows DDK - total brain spasm. Dunno where that came from really. Also the configuration for the library appears to be totally borked on Windows (VS compiler).

    The Trusted QSL software does come with a prebuilt library with it, but
    Qt Code:
    1. dumpbin /exports tqsllib2.dll
    To copy to clipboard, switch view to plain text mode 
    causes the linker to crash so I cannot even inspect the symbols that it exports.

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

    Default Re: Static and Dynamic Libraries

    None of that surprises me. Wysota said something in an earlier post that indicated it wasn't too great.

    I exchanged emails with the author and he said he really only provided the pre-built dll for Windows developers using Visual Basic and I know there are several Windows programs out there that are successfully using that approach.

    When I built the library on Windows using Mingw, I used the current versions of openssl, expat, and zlib and it built ok.

    I have gotten the tqsl_init() function to return success as follows:
    Qt Code:
    1. QLibrary tqsllib2;
    2. tqsllib2.setFileName("c:/cpp/qt_projects/lib_test2/debug/tqsllib2.dll");
    3. tqsllib2.load();
    4.  
    5. qDebug() << "is tqsllib2 loaded ? " << tqsllib2.isLoaded();
    6. qDebug() << "lib load error is " << tqsllib2.errorString();
    7.  
    8. int ret;
    9. typedef int (*MyPrototype) ();
    10.  
    11. MyPrototype tqsl_init = (MyPrototype) tqsllib2.resolve("tqsl_init");
    12. if (tqsl_init)
    13. {
    14. ret = tqsl_init();
    15. }
    16. qDebug() << "ret is now " << ret;
    To copy to clipboard, switch view to plain text mode 
    Now I am working on getting the tqsl_getStationLocation function to work.

    However, I want to be able to offer my application on Linux and Mac as well as Windows and if I'm not going to be able to compile the library for the other O/S and won't be able to use the library I built on Windows, then I think I am going to give up on adding the features to my application that the library offers.

    Am I correct in assuming I won't be able to use the dll I built under Windows to create versions of my application for Linux and Mac? If that is the case, then I'm giving up on it unless they re-write the library.

    Thanks again for all of your help, you have gone above and beyond what one would expect on the forum and I truly appreciate it. I'm sorry it turned out to be a waste of your time.

  13. #11
    Join Date
    Feb 2008
    Posts
    491
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11
    Thanks
    12
    Thanked 142 Times in 135 Posts

    Default Re: Static and Dynamic Libraries

    Waynew,

    I have been following your travails with tsqllib. Not being a windows guy, I can't help you with your dll problem but I wanted to let you know that the Debian repositories have a package (tqsllib-dev) that is being maintained with the most recent patches being about a year old. One of the patches addresses the problem that Timoteo had:
    Qt Code:
    1. -#elif (OPENSSL_VERSION_NUMBER & 0xfffff000) ==0x00907000
    2. +#elif ((OPENSSL_VERSION_NUMBER & 0xfffff000) == 0x00907000 || (OPENSSL_VERSION_NUMBER & 0xfffff000) == 0x00908000)
    To copy to clipboard, switch view to plain text mode 

    I compiled the lib from source and ran your test program successfully.

    Norm

  14. The following user says thank you to norobro for this useful post:

    Timoteo (30th November 2010)

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

    Default Re: Static and Dynamic Libraries

    Gaw, just my luck. Well, I'm a fairly stubborn guy so I would have probably ignored the package anyway, but that is great to know.

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
  •  
Qt is a trademark of The Qt Company.