0

I have a dll and associated lib file which contains a number of std::vector instances e.g.

In data.h I have:

namespace xyz{
extern DllExport std::vector<double> delays;
...

And in data.cpp I have:

namespace xyz{
std::vector<double> delays{0, 1, 4, 24, 52, 104};
...

data.cpp is compiled and linked into mydata.lib,mydata.dll and delays appears to be visible as an export:

dumpbin /exports ..\bin\debug\mydata.lib | grep delays
?delays@xyz@@3V?$vector@NV?$allocator@N@std@@@std@@A (class std::vector<double,class std::allocator<double> > xyz::delays)

I compile program.cpp, including data.h, which wants to use delays e.g.

for (int i = 0; i < xyz::delays.size(); i++)
    std::cout << xyz::delays[i] << std::endl;

attempting to link with mydata.lib but I get e.g.

cl  -EHsc -std:c++17 -I../include -Zi -MDd ../src/exe/program.cpp -link -DEBUG ../bin/debug/data.lib /OUT:../bin/debug/program.exe

program.obj : error LNK2001: unresolved external symbol "class std::vector<double,class std::allocator<double> > xyz::delays" (?delays@xyz@@3V?$vector@NV?$allocator@N@std@@@std@@A)../bin/debug/program.exe : fatal error LNK1120: 1 unresolved externals

Class definitions in data.lib, dll are fine to use in program but can I incorporate some data e.g. delays into a lib,dll so I can share and access it with whatever programs call on the lib,dll?

In case it isn't obvious using Windows 10 and microsoft compiler (19.16) and linker (14.16).

Thanks...

stegzzz
  • 407
  • 4
  • 9
  • Where and how is `DllExport` defined? – dxiv Aug 16 '20 at 19:42
  • #define DllExport __declspec(dllexport) in data.h – stegzzz Aug 16 '20 at 20:04
  • 1
    That's correct when *building* the DLL, but not for code *using* the DLL. `program.cpp` has to be built with `DllExport` defined as `__declspec(dllimport)`, or not defined at all. See [Why/when is __declspec( dllimport ) not needed?](https://stackoverflow.com/questions/4489441/why-when-is-declspec-dllimport-not-needed) for example. – dxiv Aug 16 '20 at 20:08

1 Answers1

0

The answer and links provided by dxiv helped me to fix things. In my file data.h I now have

#ifdef EXPORTING
#define DllExport __declspec(dllexport)
#else
#define DllExport __declspec(dllimport)
#endif

and I compile mydata.lib, dll with -DEXPORTING

stegzzz
  • 407
  • 4
  • 9