Results 1 to 4 of 4

Thread: Create a C++ DLL with MS VC++ and importing it in Qt

  1. #1
    Join Date
    Feb 2011
    Posts
    31
    Thanks
    23
    Qt products
    Qt4
    Platforms
    Windows

    Question Create a C++ DLL with MS VC++ and importing it in Qt

    hi there
    i want to create a dll with MS VC 2008 which i want to use in a Qt project
    I started with the project wizard and followed the guidelines here http://msdn.microsoft.com/en-us/libr...(v=vs.90).aspx

    when i include it in my qt project i get this linker error Error 48 fatal error LNK1302: only support linking safe .netmodules; unable to link ijw/native .netmodule
    it looks like it is related to the common language runtime compilation http://msdn.microsoft.com/en-us/libr...(v=vs.90).aspx

    since i cannot compile a qt project with the clr:safe option, how can i compile my dll project to make it working with Qt?

    thanks much

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

    Default Re: Create a C++ DLL with MS VC++ and importing it in Qt

    when you biuld the dll make sure /LN isn't used - it pretty much tells you that in your own link!
    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. The following user says thank you to amleto for this useful post:

    marco.stanzani (1st January 2013)

  4. #3
    Join Date
    Feb 2011
    Posts
    31
    Thanks
    23
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Create a C++ DLL with MS VC++ and importing it in Qt

    sigh i cannot find a place in the project property to disable /LN ...
    I have found just an option under 'configuration properties'->'general' where i can disable the 'Common Language Runtime Support (/clr)'
    if i do this the assembly file the wizard put in the project is no compilable and if i comment the assembly file content I got sa linker error
    fatal error LNK1000: Internal error during IncrBuildImage

    plz help, I am really stuck

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,319
    Thanks
    316
    Thanked 870 Times in 857 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Create a C++ DLL with MS VC++ and importing it in Qt

    Forget Microsoft's instructions. As you have found out, they lead you down a path of confusion and trouble.

    If you want to create a Qt-compatible DLL using MSVC 2008, download and install the Qt VS Add-in from the Qt downloads page. (Get the 1.1.11 version for Qt4, and you need to install it when VS 2008 is not running). When you restart VS, you will see that it installed a new set of options when you run the New Project wizard in Visual Studio.

    • Start the new project wizard (File -> New -> Project)
    • Select Qt4 Projects
    • Select Qt Library. Give it a name, choose where you want it built, then click OK. The Qt4 Library Project wizard will appear.
    • On the Qt4 Library Project Wizard, click "Project Settings"
    • Select any additional Qt features you will need (like GUI, for example). You can also change the options to build a static library instead.
    • Click "Generated Class" and change the name if you wish. You can also choose precompiled headers if you want. (I don't use them).
    • Click "Finish" and you now have the skeleton for a Qt4 DLL, built with MSVC 2008


    Note that you will still need to insert the __declspec( dllexport ) / __declspec( dllimport ) macros (or their equivalent) on any classes or methods you want to export from the DLL. I use a header file like this in one of my projects. Change "QTJZY3D" to something unique for your project.

    Qt Code:
    1. // QtJzy3D.h header file
    2.  
    3. #ifndef QTJZY3D_H
    4. #define QTJZY3D_H
    5.  
    6. #include <QtGlobal>
    7.  
    8. #ifndef QTJZY3D_DECL_EXPORT
    9. # if defined(Q_OS_WIN)
    10. # define QTJZY3D_DECL_EXPORT __declspec(dllexport)
    11. # else
    12. # define QTJZY3D_DECL_EXPORT
    13. # endif
    14. #endif
    15.  
    16. #ifndef QTJZY3D_DECL_IMPORT
    17. # if defined(Q_OS_WIN)
    18. # define QTJZY3D_DECL_IMPORT __declspec(dllimport)
    19. # else
    20. # define QTJZY3D_DECL_IMPORT
    21. # endif
    22. #endif
    23.  
    24. #if defined(QTJZY3D_LIB) /* make or use a QtJzy3d static library */
    25. # undef QTJZY3D_MAKEDLL
    26. # undef QTJZY3D_DLL
    27. # define QTJZY3D_EXPORT
    28. #elif defined(QTJZY3D_MAKEDLL) /* create a QtJzy3d DLL library */
    29. # if defined(QTJZY3D_DLL)
    30. # undef QTJZY3D_DLL
    31. # endif
    32. # define QTJZY3D_EXPORT QTJZY3D_DECL_EXPORT
    33. #elif defined(QTJZY3D_DLL) /* use a QtJzy3d DLL library */
    34. # define QTJZY3D_EXPORT QTJZY3D_DECL_IMPORT
    35. #endif
    36.  
    37. #endif // QTJZY3D_H
    To copy to clipboard, switch view to plain text mode 

    In the DLL, when I want to export a class and all its methods, I simply put QTJZY3D_EXPORT in the class declaration:

    Qt Code:
    1. // QtJzy3DApplication.h header file
    2.  
    3. #ifndef QTJZY3DAPPLICATION_H
    4. #define QTJZY3DAPPLICATION_H
    5.  
    6. #include "QtJzy3d.h"
    7.  
    8. #include <QApplication>
    9.  
    10. class QTJZY3D_EXPORT QtJzy3dApplication : public QApplication
    11. {
    12. Q_OBJECT;
    13.  
    14. public:
    15. QtJzy3dApplication( int & argc, char *argv[] );
    16. virtual ~QtJzy3dApplication();
    17. };
    18.  
    19. #endif // QTJZY3DAPPLICATION_H
    To copy to clipboard, switch view to plain text mode 

    When I build the DLL from its source code, I add "QTJZY3D_MAKEDLL" to the C++ Preprocessor Definitions settings (look under File -> Properties -> C/C++ in VS 2008). This tells C++ to compile the exports using the __declspec( dllexport ) option that exposes the classes and methods outside the DLL.

    When I use the DLL in another project, I add "QTJZY3D_DLL" to the preprocessor settings. This will cause the __declspec( dllimport ) version of the macro to be used when compiling in your DLL's header files, and will tell the linker to look for these classes with DLL linkage.

    If I build or use my library as a static library, I define "QTJZY3D_LIB" for both building and using the library. This removes the export and import macros so the compiler and linker create code that uses normal linkage (not DLL linkage).

    And of course, if I am building or using the library on something other than Windows, the header file tells the compiler and linker to completely ignore all that Microsoft idiocy.

    Hope this helps.
    Last edited by d_stranz; 1st January 2013 at 17:45.

  6. The following user says thank you to d_stranz for this useful post:

    marco.stanzani (1st January 2013)

Similar Threads

  1. Importing a sqlite database into another...
    By mtnbiker66 in forum Qt Programming
    Replies: 0
    Last Post: 26th April 2012, 22:00
  2. Importing a QT .pro
    By salmanmanekia in forum Newbie
    Replies: 1
    Last Post: 16th April 2010, 18:17
  3. importing headers
    By peace_comp in forum Qt Programming
    Replies: 1
    Last Post: 29th March 2008, 22:35
  4. importing from CAD model
    By TheKedge in forum Qt Programming
    Replies: 2
    Last Post: 11th April 2007, 19:26
  5. Parsing and importing a dll in Qt4
    By theBrave in forum Qt Programming
    Replies: 3
    Last Post: 12th May 2006, 13:53

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.