Reputation: 3
I have a few arrays that I need to hold in memory for the duration of my program. The arrays are used as look up references by different files so I thought I should make a DLL to hold them in.
The main problem I seem to run into is that the files have to be constructed at the beginning of the program. The arrays hold a few thousand values each and future ones may hold millions, so hard coding the arrays isn't an option.
Here is my best attempt:
First, I made the Dll header file. I read about making static constructors, which is what I am trying to do here to hold the arrays. I put the export only on the NumCalc class (correct?).
// TablesDll.h
#ifndef TABLESDLL_EXPORTS
#define TABLESDLL_EXPORTS
#ifdef TABLESDLL_EXPORTS
#define TABLESDLL_API __declspec(dllexport)
#else
#define TABLESDLL_API __declspec(dllimport)
#endif
namespace tables
{
class Arrays
{
public:
static const int* arr;
private:
static int* SetNums();
};
class TABLESDLL_API NumCalc
{
public:
static Arrays arrays;
};
}
#endif
Now the definitions:
// TablesDll.cpp
#include "stdafx.h"
#include "TablesDll.h"
#include <stdexcept> (<- I don't know why this is here...)
namespace tables
{
const int* Arrays::arr = SetNums();
int* Arrays::SetNums()
{
int* arr= new int[2000];
/* set the numbers*/
return arr;
}
}
It compiles fine. I take the files and stick them into a test program as so:
// TestTablesDll
#include "stdafx.h"
#include "TablesDll.h"
using namespace tables;
int _tmain(int argc, _TCHAR* argv[])
{
for(int i=0; i<299; i++)
printf("arr[%i] = %d/n", i, NumCalc::arrays::arr[i]);
return 0;
}
This doesn't even compile unfortunately.
error C3083: 'arrays': the symbol to the left of a '::' must be a type
My previous attempt didn't use the static constructor. There was no class Arrays. NumCalc was the only class containing
static TABLESDLL_API const int* arr
and the private function
static const int* SetNums().
This yielded a LNK2001 compiler error when run in the TestTablesDll
I'm pretty sure there's an issue with the function not running at compile time, leaving the arr variable undefined.
How can I do this?
Upvotes: 0
Views: 124
Reputation: 4538
In TablesDll.h
you should put TABLESDLL_API
to the Arrays
class too. Otherwise you will not be able to use the parts of NumCalc
that depend on Arrays
.
Also you should have this Arrays NumCalc::arrays;
in TablesDll.cpp
even though Arrays
is an empty class - arrays
has to be defined somewhere (and not just declared in the class definition).
EDIT: There were more problems.
arr
should be accessed like this: NumCalc::arrays.arr
- with .
and not with ::
Also the header always exports symbols beacuse you define TABLESDLL_EXPORTS
and right after that you check if it's defined. This is how it should be:
#ifndef TABLESDLL_HEADER_GUARD
#define TABLESDLL_HEADER_GUARD
#ifdef TABLESDLL_EXPORTS
#define TABLESDLL_API __declspec(dllexport)
#else
#define TABLESDLL_API __declspec(dllimport)
#endif
and in TablesDll.cpp
you should define TABLESDLL_EXPORTS
before including the header - so that only the dll exports the symbols and the executable imports them. Like this:
#define TABLESDLL_EXPORTS
#include "TablesDll.h"
Upvotes: 1