Results 1 to 11 of 11

Thread: to use a dll (.NET)

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default to use a dll (.NET)

    hi,
    I write a class File (file.h and file.cpp) and compile it as file.dll.
    TheN I'd like use it in other project myApp; so I add in myApp:
    Qt Code:
    1. #include "file.h"
    2. using namespace std;
    3. int main (int argc, char** argv) {
    4. File f;
    5. f.setFileName("myfile.txt");
    6. if (!f.open_file(0)) {
    7. cerr << "unable to open that file\n";
    8. return -1;
    9. }
    10. . ..........................................
    11. return 0;
    To copy to clipboard, switch view to plain text mode 
    and I put file.dll in myApp directory;
    the errorors are these:
    file error LNK2019: unresolved external symbol "public: __thiscall File::~File(void)" (??1File@@QAE@XZ) referenced in function _main
    file error LNK2019: unresolved external symbol "public: __thiscall File::File(void)" (??0File@@QAE@XZ) referenced in function _main
    file error LNK2019: unresolved external symbol "public: class std::basic_fstream<char,struct std::char_traits<char> > & __thiscall File::open_file(int)" (?open_file@File@@QAEAAV?$basic_fstream@DU?$char_t raits@D@std@@@std@@H@Z) referenced in function _main
    file fatal error LNK1120: 3 unresolved externals
    I think this is simple, but this is my first time...anyone could suggest me? thanks
    Last edited by jacek; 13th October 2006 at 21:19. Reason: changed [code] to [quote] to allow wrapping
    Regards

  2. #2
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: to use a dll (.NET)

    With the .dll file you would have got .lib file also. Add the path to this lib file in the linker options of myApp.

    project->properties->linker->Additional Library directories.

  3. #3
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: to use a dll (.NET)

    sorry I haven't .lib file; probabily I'm doing a wrong crating .dll; I did: new project-> win32consoleProject then I cjecked dll and 'empty project' in application settings; then I add file.h and file.cpp to the project; compiling is successful...but it create .dll .obj....the usual files of a .net application (eccept .exe)....How to costruct a .dll? What am I doing wrong? thanks
    Regards

  4. #4
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: to use a dll (.NET)

    Hi, could anyone suggest me a link to read hoe create a .dll and use it step by step with .net2003 (I found for VC6 but I have a crisis...) thanks
    Regards

  5. #5
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: to use a dll (.NET)

    hi, I'm trying to buil a dll from a class and its implementation....I've this problem (I already find on google but doens't help me...). Here below you can see the linking errors; I need to include iostream to declare string file_name (and for other things...) Any suggets? Thanks...
    Qt Code:
    1. // mgfile.h
    2. #pragma once
    3. #include <use_ansi.h>
    4. #include <iostream> // error LNK2020: unresolved token (0A000006) _CxxThrowException
    5.  
    6.  
    7. //#include <fstream> // error LNK2020: unresolved token(0A000006) _CxxThrowException
    8.  
    9. //using namespace std; //it seems necessary includes <iostream>
    10.  
    11.  
    12. using namespace System;
    13.  
    14. namespace mgfile {
    15. public __gc class File {
    16. private:
    17. //string file_name;
    18. //std::fstream file;
    19. public:
    20. File();
    21. ~File();
    22. //inline void setFileName (std::string s) {file_name = s;}
    23. //inline std::string getFileName () {return file_name;}
    24. };
    25. }
    To copy to clipboard, switch view to plain text mode 
    Regards

  6. #6
    Join Date
    Feb 2006
    Location
    US
    Posts
    173
    Thanks
    16
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: to use a dll (.NET)

    Make sure you are exporting the functions you want public. If there are non exported, then Visual Studio (or MinGW, etc.) won't create the associated .lib.
    Last edited by brcain; 20th October 2006 at 16:42.

  7. #7
    Join Date
    Feb 2006
    Location
    US
    Posts
    173
    Thanks
    16
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: to use a dll (.NET)

    Here's a portable, common way to handle WIN32 exports. It will generate .lib on Windows using Visual Studio or other compiler (e.g. MinGW).

    In the project that creates the library, define the preprocessor directive SOMECLASS_LIBRARY ... resulting in usage of the dllexport rule.

    In the project that includes the library, it will automatically use the dllimport rule.

    In header file someClassExport.h
    Qt Code:
    1. #ifndef _SOMECLASS_EXPORT_
    2. #define _SOMECLASS_EXPORT_
    3.  
    4. #if defined(WIN32) || defined(_MSC_VER) || defined(__CYGWIN__) || defined(__MINGW32__) || defined( __BCPLUSPLUS__) || defined( __MWERKS__)
    5. #ifdef SOMECLASS_LIBRARY
    6. #define SOMECLASS_EXPORT __declspec(dllexport)
    7. #else
    8. #define SOMECLASS_EXPORT __declspec(dllimport)
    9. #endif
    10. #else
    11. #define SOMECLASS_EXPORT
    12. #endif
    13.  
    14. #endif // _SOMECLASS_EXPORT_
    To copy to clipboard, switch view to plain text mode 


    In header file someClass.h
    Qt Code:
    1. #ifndef _SOMECLASS_
    2. #define _SOMECLASS_
    3.  
    4. #include "someClassExport.h"
    5.  
    6. // If wanting to export whole class ...
    7. class SOMECLASS_EXPORT cSomeClass
    8. {
    9. public:
    10. int foo(void);
    11. }
    12.  
    13. // Or, if wanting to export only specified methods of class ...
    14.  
    15. class cSomeClass
    16. {
    17. public:
    18. SOMECLASS_EXPORT int foo(void);
    19. };
    20.  
    21. #endif // _SOMECLASS_
    To copy to clipboard, switch view to plain text mode 
    Last edited by brcain; 20th October 2006 at 16:47.

  8. #8
    Join Date
    Mar 2006
    Posts
    140
    Thanks
    8
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: to use a dll (.NET)

    You don't need the .lib
    You do need to specify an additional include path to where the header file of your library is located, and #include that file.

    The trick with Visual Studio .NET 2k3 and 2k5 is to add a 'Reference' to the dll.
    Go to the 'Solution Explorer' and right click the client project (the one that needs access to the dll), then select 'Add Reference'.
    A dialog will be shown listing all the .NET, .COM and Project references.

    The dialog allows you to select the dll you need.
    If you have a solution open and you dll and client project within that solution, then click on the 'Projects' tab. Your dll project will be listed here. Select it from the list and click the 'Select' button (to the right of the tab control), then click 'Ok' (bottom of the dialog).

    If you are referring to a dll that isn't from a project in your solution, then you can 'Browse...' to the dll explicitly.

    Each project in the solution explorer has a 'References' item in the TreeView.
    Your new dll reference will be listed there. Now Visual Studio knows where to find the body of the classes you're calling and you shouldn't get the 'unresolved synbol' errors.


    I hope this helps,

    Steve York

  9. #9
    Join Date
    Feb 2006
    Location
    US
    Posts
    173
    Thanks
    16
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: to use a dll (.NET)

    So does visual studio no longer need the dllexport and dllimport rules. That would be convenient!

  10. #10
    Join Date
    Mar 2006
    Posts
    140
    Thanks
    8
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: to use a dll (.NET)

    You do still need that, but Qt wraps them up into a <libname>_global.h and you include that in each class you want to export / import.

    Here's an example:

    Qt Code:
    1. #ifndef IMAGECACHE_GLOBAL_H
    2. #define IMAGECACHE_GLOBAL_H
    3.  
    4. #include <Qt/qglobal.h>
    5.  
    6.  
    7. #ifdef IMAGECACHE_LIB
    8. # define IMAGECACHE_EXPORT Q_DECL_EXPORT
    9. #else
    10. # define IMAGECACHE_EXPORT Q_DECL_IMPORT
    11. #endif
    12.  
    13.  
    14. #endif // IMAGECACHE_GLOBAL_H
    To copy to clipboard, switch view to plain text mode 


    You basically need to stick IMAGECACHE_EXPORT in front of the class name:
    Qt Code:
    1. #ifndef IMAGECACHE_H
    2. #define IMAGECACHE_H
    3.  
    4. #include "imagecache_global.h"
    5. #include <QObject>
    6.  
    7. class IMAGECACHE_EXPORT ImageCache : public QObject
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. ImageCache();
    13. ~ImageCache();
    To copy to clipboard, switch view to plain text mode 
    You could write a dll without these Export and Import macros but you'd have runtime linking issues and would have to define explicit load instructions for each function you want access to. You also can't add a reference to a dll from Visual Studio unless these macros are defined.

    But, as I said originally, to allow dynamic linking in the above example, I'd stick #include "imagecache.h" in my client application and add a project reference to imagecache.dll
    Then the client application will be able to compile without having to worry about having a .lib or .exp file in your build path.



    Steve York

  11. #11
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: to use a dll (.NET)

    Quote Originally Posted by stevey View Post
    You don't need the .lib
    The trick with Visual Studio .NET 2k3 and 2k5 is to add a 'Reference' to the dll.
    Go to the 'Solution Explorer' and right click the client project (the one that needs access to the dll), then select 'Add Reference'.
    A dialog will be shown listing all the .NET, .COM and Project references.

    The dialog allows you to select the dll you need.
    If you have a solution open and you dll and client project within that solution, then click on the 'Projects' tab. Your dll project will be listed here. Select it from the list and click the 'Select' button (to the right of the tab control), then click 'Ok' (bottom of the dialog).

    If you are referring to a dll that isn't from a project in your solution, then you can 'Browse...' to the dll explicitly.
    Steve York
    sorry,
    I'm trying to follow your instructions but this errror occured; I've got a dll create from project A; after I created projetc B and I try to import dll within B project; I try to add this reference from all tabs (.net, com, project); the error is the same; why that?
    thanks.
    Attached Images Attached Images
    Regards

Similar Threads

  1. Add custom QTreeView in VS .NET
    By wind in forum Newbie
    Replies: 4
    Last Post: 5th October 2006, 17:04
  2. problem with linking
    By mickey in forum Qt Programming
    Replies: 49
    Last Post: 12th August 2006, 21:41
  3. Qt and .NET
    By Brandybuck in forum Qt Programming
    Replies: 6
    Last Post: 6th July 2006, 01:01
  4. Can't configure for Win32 MSVC .NET
    By saber850 in forum Installation and Deployment
    Replies: 8
    Last Post: 28th February 2006, 16:42

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.