views:

21

answers:

1

Hello,

I have a h- and a cpp-file with some calculations used in many of my projects.

Now I tried to put them in a separate dll, so the files should not be included in every project.

When linking I get a LNK2001 (unresolved symbol) error for a struct, however lib and dll are in the right place.

I use the

#ifdef TOOLS_EXPORTS
#define TOOLS_API __declspec(dllexport)
#else
#define TOOLS_API __declspec(dllimport)
#endif

macro, which works fine for a couple of methods.

The struct is defined like that

TOOLS_API typedef  struct  {
char Name[128];
}  uTSystem;

And in the files using this struct from the dll its also defined correctly(?)

extern uTSystem ABC;

The error message is:

error LNK2001: Nichtaufgeloestes externes Symbol "struct uTSystem ABC" (?ABC@@3UuTSystem@@A)

Any hints? Thank you

A: 

Assuming you defined TOOLS_EXPORT when compile the DLL you will export the variable ABC. In your code you define it as extern uTSystem ABC; That's ok for the header file, that you share with the consuming DLL.

While the extern declares that there is a variable ABC you must define it in one of your .cpp file:

uTSystem ABC;

without the extern in front. Your file might look like this:

---- tools.h ----

#ifdef TOOLS_EXPORTS
#define TOOLS_API __declspec(dllexport)
#else
#define TOOLS_API __declspec(dllimport)
#endif

TOOLS_API typedef  struct  {
char Name[128];
}  uTSystem;

extern uTSystem ABC;

---- tools.cpp ----

#include tools.h

uTSystem ABC;
harper