PDA

View Full Version : Create a C++ DLL with MS VC++ and importing it in Qt



marco.stanzani
31st December 2012, 21:54
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/library/ms235636(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/library/9x035d1s(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

amleto
1st January 2013, 02:58
when you biuld the dll make sure /LN isn't used - it pretty much tells you that in your own link!

marco.stanzani
1st January 2013, 11:56
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

d_stranz
1st January 2013, 17:58
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 (http://qt-project.org/downloads#qt-other). (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.



// QtJzy3D.h header file

#ifndef QTJZY3D_H
#define QTJZY3D_H

#include <QtGlobal>

#ifndef QTJZY3D_DECL_EXPORT
# if defined(Q_OS_WIN)
# define QTJZY3D_DECL_EXPORT __declspec(dllexport)
# else
# define QTJZY3D_DECL_EXPORT
# endif
#endif

#ifndef QTJZY3D_DECL_IMPORT
# if defined(Q_OS_WIN)
# define QTJZY3D_DECL_IMPORT __declspec(dllimport)
# else
# define QTJZY3D_DECL_IMPORT
# endif
#endif

#if defined(QTJZY3D_LIB) /* make or use a QtJzy3d static library */
# undef QTJZY3D_MAKEDLL
# undef QTJZY3D_DLL
# define QTJZY3D_EXPORT
#elif defined(QTJZY3D_MAKEDLL) /* create a QtJzy3d DLL library */
# if defined(QTJZY3D_DLL)
# undef QTJZY3D_DLL
# endif
# define QTJZY3D_EXPORT QTJZY3D_DECL_EXPORT
#elif defined(QTJZY3D_DLL) /* use a QtJzy3d DLL library */
# define QTJZY3D_EXPORT QTJZY3D_DECL_IMPORT
#endif

#endif // QTJZY3D_H


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



// QtJzy3DApplication.h header file

#ifndef QTJZY3DAPPLICATION_H
#define QTJZY3DAPPLICATION_H

#include "QtJzy3d.h"

#include <QApplication>

class QTJZY3D_EXPORT QtJzy3dApplication : public QApplication
{
Q_OBJECT;

public:
QtJzy3dApplication( int & argc, char *argv[] );
virtual ~QtJzy3dApplication();
};

#endif // QTJZY3DAPPLICATION_H


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.