nabroyan
nabroyan

Reputation: 3275

__declspec (dllexport) custom type variable

I'm working on a Visual Studio solution with multiple projects (Visual Studio 2013). One project is for generating a .dll file, other projects use that .dll file and generate .exe files. When I export a standard type variable everything works fine. But in case if I want to use my custom defined type I get an compilation error. Here is an example

// Dll.cpp
#define DllExport __declspec (dllexport)
DllExport int maxPackSize = 20;
// my custom type
struct DllExport Header
{
    int m_data;    // some data
};
DllExport Header qHead = { 100 };


// Exe.cpp
#define DllImport __declspec (dllimport)
DllImport extern int packetSize;    // OK

struct DllImport Header;
DllImport extern Header qHead;      // leads to an error

When I use qHead in my Exe.cpp I get a compilation error on that line. Error is like

error C2027: use of undefined type 'Header'

What am I doing wrong? Any ideas?

Upvotes: 0

Views: 1829

Answers (2)

hama
hama

Reputation: 56

You need to export your custom type in a header, so that your .exe can see the type. For example,

Dll.h

#ifdef EXPORT_SYMBOLS
#define DLL_EXPORT __declspec(dllexport)
#else
#define DLL_EXPORT __declspec(dllimport)
#endif

struct DLL_EXPORT Header
{
   int m_data;
};

Exe.cpp

#include "Dll.h"
Header qHead;

You need to add EXPORT_SYMBOLS to the Dll's preprocessor flags so that the correct __declspec macro is switched.

Upvotes: 1

m.s.
m.s.

Reputation: 16354

The error message you get is a compiler error (not a linker error!). You need to define Header prior to its first use.

You could move struct DllExport Header { ... }; to a separate header file (e.g. Dll.h) and then #include "Dll.h" in Exe.cpp.

Upvotes: 1

Related Questions