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.
// 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
// 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
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:
// 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
// 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
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.
Bookmarks