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

Thread: Fixed a problem. Help me understand why :)

  1. #1
    Join Date
    Jun 2012
    Posts
    98
    Thanks
    11
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Fixed a problem. Help me understand why :)

    Alright, so I *finally* got all my ducks in a row on getting a plugin to work both in the Qt Designer and not give any linker errors on compilation etc..

    My main issue was that I get receiving linker errors when I had built it as a DLL; the only way to solve those errors was to include the .h and .cpp files directly in with the project, but that's defeating the purpose of encapsulationl! So I went ahead and finally attempted to build the plugin as a *static* library. This replaced the old .lib file and it all works now! (It's worth noting that this was more than one class. The plugin class used another custom class. The dynamic library presumably wouldn't compile my other custom class in with the plugin class and gave me linker errors.)

    I guess my question is:
    -Why didn't the DLL compilation compile both classes into it's .lib file but the standard library compilation did?

    I investigated the issue pretty thoroughly but I have not figured it out yet. It's worth noting that I have not built .lib type files in the past. Help understanding why this fixed my issue would be much appreciated.
    Thanks!

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

    Default Re: Fixed a problem. Help me understand why :)

    what compiler are you using?

    e.g. on microsoft you have to do something like __declspec(dllexport) and have some code in .cpp file for .lib file to be made. otherwise the library is "header only" and you must include the header and not dll/lib.

    Essentially, you got linker error because you either:
    - had nothing in the library to link with -> build/compile problem with the dll.
    - did not properly configure your project to link with the correctly built library
    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
    Jun 2012
    Posts
    98
    Thanks
    11
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Fixed a problem. Help me understand why :)

    Hm, well, it does export the DLL file and additionally has an "Export library file" (exp) that goes with it. I didn't add code to any .cpp files to ensure the other class would be included other than standard #include statements.

    Is there anything dirty about using the static library compared to a dll? Is there a reason I should go figure this out (other than being a learning experience) over just using the Static Library?

    Thanks!

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

    Default Re: Fixed a problem. Help me understand why :)

    I didn't add code to any .cpp files to ensure the other class
    __declspec would go in the header, not the .cpp.

    yes. static library means any change to a library and you need to rebuild your whole app. With dll you just rebuild the dll.


    You should show your dll code and how you were trying to link it.
    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
    Jun 2012
    Posts
    98
    Thanks
    11
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Fixed a problem. Help me understand why :)

    Attached is the full code (plus the vs2010 solution) mostly because I'm not sure I understand what you're asking for and your signature.

    I linked using the following algorithm:
    -I built this in VS2010 using the ".dll" project settings, set to output at <build_location>
    -In a separate program I added the <build_location> to the included libraries and added the built library to it's additional dependencies (this is all under the VS "linker" tab. Right click the project name in the solution explorer->properties->configuration properties->linker)

    From browsing online it looks like I just add:
    __declspec(dllexport) in front of each class I wish to include in the dll? Is Visual Studio automatically doing this at all? It might be worth noting that the .dlls work fine with/for designer, they just don't actually link for the real program :s.

    EDIT: Tried that. Same link issues.
    Attached Files Attached Files
    Last edited by tescrin; 30th July 2012 at 22:22.

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

    Default Re: Fixed a problem. Help me understand why :)

    Is Visual Studio automatically doing this at all?
    No.

    I will have a look at the solution...


    ... You forgot the project files.
    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.

  7. #7
    Join Date
    Jun 2012
    Posts
    98
    Thanks
    11
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Fixed a problem. Help me understand why :)

    I assume you mean the VS Project files; I figured you wouldn't want them (because they'll change user settings and such.) Here ya go.

    EDIT: If no, I'm not using a real .pro file. VS seems to abhor them as even when I write one correctly I have to go manually include paths and a bunch of other hoopla. I wouldn't be using VS if I didn't have to lol.
    Attached Files Attached Files

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

    Default Re: Fixed a problem. Help me understand why :)

    compile settings and linker settings are in the project file not the solution...


    Added after 10 minutes:


    you can see from using depends.exe that your dll is virtually empty - this will be because you are not exporting any of your classes into the dll (__declspec...).
    Last edited by amleto; 30th July 2012 at 22:41.
    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.

  9. #9
    Join Date
    Jun 2012
    Posts
    98
    Thanks
    11
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Fixed a problem. Help me understand why :)

    I hate to be dense; but does this mean you're looking for the project I was trying to link to as well? If so, it's really not necessary if you perform the above steps (add the library location, add the library as a dependency) as I'm just doing it with a simple generated project and adding this class using the designer on the provided form (to test it.)

    Further, the paths probably won't like up with your pathing meaning it's probably more work to do it that way.

    Either way, thanks thus far


    EDIT:After your edit
    With the modified code (just adding __declspec(dllexport) to the classes) the linker still gives me the same errors.
    It's also worth noting that I only get errors regarding "textspinbox" (the class being used by the plugin) and not the plugin class itself (bertspinbox)

    I'll keep looking into the declspec issue.., it looks like you have to import as well (on every function that uses dll stuff?)
    Last edited by tescrin; 30th July 2012 at 22:49.

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

    Default Re: Fixed a problem. Help me understand why :)

    yes, when you include the headers in the project that needs to link against the dll, it needs to be dllimport.

    this 'problem' is normally solved with #defines.
    Qt Code:
    1. #ifdef BUILD_THIS_DLL
    2. #define DECLSPEC __declspec(dllexport)
    3. #else
    4. #define DECLSPEC __declspec(dllimport)
    5. #endif
    To copy to clipboard, switch view to plain text mode 

    where BUILD_THIS_DLL is only defined for the dll project and not the app using the dll.

    See also http://msdn.microsoft.com/en-us/library/8fskxacy.aspx
    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.

  11. The following user says thank you to amleto for this useful post:

    tescrin (31st July 2012)

  12. #11
    Join Date
    Jun 2012
    Posts
    98
    Thanks
    11
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Fixed a problem. Help me understand why :)

    Thanks for the help. I'll have to go find a tut on doing this correctly as right now I'm just breaking my code :s. (it's generating multiple definitions for the same classes and such)

  13. #12
    Join Date
    Jun 2012
    Posts
    98
    Thanks
    11
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Fixed a problem. Help me understand why :)

    I decided to put together a document that clearly explains the *every* step of the way towards doing this correctly in visual studio (in part for myself._ I figured that it might be useful for anyone else who happens upon having troubles getting stuff to work.

    Might be worth pointing to, stickying, or something for future Qt users who have trouble with plugins. The information gathering on getting this all to work is really spread out and for someone just approaching it from the perspective of "this will be quick and easy" it required finding, asking, and experimenting with a lot of different things. Maybe creator does some of these steps for you and I just didn't mess with it enough. At any rate; for visual studio:


    Paths below are based on a standard install of the QT SDK (as of 8/1/2012) with a base directory
    named "Qt" directly located in C:\. Adjust accordingly.
    It also assumes an install of the Qt Visual Studio addin

    QT_PATH => C:\Qt\Desktop\Qt
    QT_DIR => %QT_PATH%\4.8.1\msvc2010
    QT_PLUGINS_DESIGNER => %QT_DIR%\plugins\designer\


    *****FOR DESIGNER******
    //Auto-load qt libraries you'll reference and some other convenient settings
    Create a Qt Designer Plugin project in visual studio (file, new, project, ..)

    //This makes the dll usable by designer
    In the <PLUGIN_PROJECT_NAME> -> Properties -> Configuration Properties -> General ->
    set output directory to QT_PLUGINS_DESIGNER
    set configuration type to "Dynamic Library (.dll)" //if it isn't already
    --optionally you may also build it a second time with another output directory.
    Either way, the below "Your_desired_lib_folder" references the output directory of
    this plugin. If you use multiple directories, be sure to build to each one each time.


    *****FOR USE IN PROJECTS*****
    //This makes the dll usable by other executables/code
    If (the following steps are confusing, refer to "Reference 1"){

    For each class in this <PLUGIN_PROJECT_NAME> you'll need to add __declspec(dllimport)
    (in the header file)
    --E.G. "class foo" becomes "class __declspec(dllimport) foo"

    For each member function of said class NOT DEFINED IN THE HEADER FILE you'll need to
    add __declspec(dllexport) before the function in the source file.
    --E.G. "void bar(){}" becomes "__declspec(dllexport) void bar(){}"
    }

    //Tells the project which library you'll be using (in addition to what is already loaded)
    In the <PLUGIN_USER_PROJECT_NAME> -> Properties -> Configuration Properties -> Linker -> Input
    Add that <Your_Plugin_Name>.lib to your "Additional Dependencies"

    //Allows your project to find the .lib file
    In the <PLUGIN_USER_PROJECT_NAME> -> Properties -> Configuration Properties -> Linker -> General
    Add that <Your_Desired_Lib_Folder> to your "Additional Library Directories"

    //Allows project to find your .dll
    In the <PLUGIN_USER_PROJECT_NAME> -> Properties -> Configuration Properties -> Debugging
    Add your <Your_desired_lib_folder> to your PATH variable. Seperate it via a semicolon:
    --E.G.<CURRPATH> would now be <CURRPATH>;<Your_Desired_Lib_folder>








    References:
    1 - http://www.codeguru.com/cpp/cpp/cpp_...-Beginners.htm

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

    Default Re: Fixed a problem. Help me understand why :)

    "For each member function of said class NOT DEFINED IN THE HEADER FILE"

    How can a member function not be declared in the header?
    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.

  15. #14
    Join Date
    Jun 2012
    Posts
    98
    Thanks
    11
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Fixed a problem. Help me understand why :)

    It says "defined", not declared.

    Qt Code:
    1. //Header_file.h
    2. class foo{
    3. //stuff..
    4. //function that is declared and DEFINED in the header file
    5. void bar() {return 0;}
    6.  
    7. //function that is declared but DEFINED in a cpp file
    8. void derp();
    9. };
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Fixed a problem. Help me understand why :)

    fair point, but your example uses a method that IS defined in the class...

    If you export the class then I don't think you need to export any members explicitly.
    Last edited by amleto; 1st August 2012 at 21:24.
    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.

  17. #16
    Join Date
    Jun 2012
    Posts
    98
    Thanks
    11
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Fixed a problem. Help me understand why :)

    The example function is referring to a function that wasn't in a class. I.E.

    Qt Code:
    1. //Header.h
    2.  
    3. class __declspec(dllimport) foo
    4. {
    5. void glarc() {}; //!!<-this is not what the comment is referring to
    6. void bar()
    7. }
    8.  
    9.  
    10. //source.cpp
    11. __declspec(dllexport) void foo::bar() {} //!!<- this is what the comment is referring to
    To copy to clipboard, switch view to plain text mode 
    Last edited by tescrin; 1st August 2012 at 22:06.

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

    Default Re: Fixed a problem. Help me understand why :)

    In that case, no, that is wrong. You do not need to use the __declspec modifier in the source.
    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.

  19. #18
    Join Date
    Jun 2012
    Posts
    98
    Thanks
    11
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Fixed a problem. Help me understand why :)

    Just like the link in my file:
    http://www.codeguru.com/cpp/cpp/cpp_...-Beginners.htm

    I'm unsure where else you'd but the modifiers. You put one at the top of the class but that's not enough; I've tried doing that. I'm happy to make the document correct, but it seems to be according to the above (and my experience thus far.)

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

    Default Re: Fixed a problem. Help me understand why :)

    In that case, no, that is wrong. You do not need to use the __declspec modifier in the source (cpp) file.

    I think your terminology is confusing. All members are 'in the class' by definition. They may not be implemented in the class definition, though.



    e:
    There're no classes in your link so it doens't really help on this issue...

    And I know you do not need modifiers in the source because I have hundreds of working examples at work


    This would tend to agree with me (or I with it...)
    http://msdn.microsoft.com/en-us/libr...(v=vs.80).aspx
    Last edited by amleto; 1st August 2012 at 23:05.
    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.

  21. #20
    Join Date
    Jun 2012
    Posts
    98
    Thanks
    11
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Fixed a problem. Help me understand why :)

    huh... I swear that didn't work the first time I tried it. I hate to continue being dense, but then what does dllimport do!?

    I have a plugin that works just fine without using "dllimport" at all.. I think. I'm sorry, but I really just don't follow that darn MSDN page (or we probably wouldn't be having this discussion.) Do I dllimport on some class that's in a dll that I'm trying to use? If so, why do I have an example that works without doing that :s

Similar Threads

  1. trying to understand qsettings ...
    By kerim in forum Newbie
    Replies: 7
    Last Post: 28th April 2011, 13:30
  2. Replies: 6
    Last Post: 13th May 2010, 13:54
  3. Something i am not understand about Qt.
    By HeX0R in forum Newbie
    Replies: 8
    Last Post: 12th February 2010, 04:37
  4. need help .not able to understand.......
    By sh123 in forum Qt Programming
    Replies: 1
    Last Post: 20th January 2009, 01:50
  5. PyQt QTimer problem { FIXED }
    By WinchellChung in forum Newbie
    Replies: 0
    Last Post: 1st March 2008, 16:50

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.