Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DLL build error; exporting static data from DLL

Status
Not open for further replies.

rashmighai

Programmer
Aug 20, 2003
4
US
Hello all ,
I'n new to writing DLLs and the foll line of code in a header file is giving me errors:-
_declspec(dllexport) static int erlStarted = 0
The error is :-
error C2201: 'erlStarted' : must have external linkage in order to be exported/imported

How do I export static data from a DLL?

I'm working on craeting DLL in C and on MS VC++ .
Any suggestions anyone?
 
What's the point of making something static if you're wanting to export it?

Just delete the static keyword from the declaration.
 


Thanks for that Salem , actually i'm trying to convert a static library into a dll and have several issues like:-
1.Converting the variables in the library declared as static.
2.Many of the library files reference the static variables declared in other files of the library.How to tackle this issue of cross referenced static variables?
Is there any web site you'ld like to suggest for this kind of work?
 
> 1.Converting the variables in the library declared as static.
Well if they are already static, then I would think that there is no reason for you to be exporting them.

> 2.Many of the library files reference the static variables declared in other files of the library
Normally, you would have to make them global. But you can keep them all within the DLL by not using the 'dllexport'.

Though why would you need to do this - the same problem would be present whether you were building a static library or a DLL.

Ideally, you should not be exporting any variables at all - only functions. Excessive dependency at the data level is a nightmare for maintenance. Consider writing some access functions to wrap around the data which you want to share with the outside world.
 
Salem is spot on.
You could try using the registry to pass around variables.
I don't really think its worth the fuss though. :)
 
put static variables what you want to share between programs in a data segment like this:
#pragma data_seg("IonFilipski-db2orb-v3-server")
static bool isRunningAlready = false;
HWND hServerWnd = 0;
#pragma data_seg()
and put in the linker command line:
/section:IonFilipski-db2orb-v3-server,rws

The same thing you can do just in the .exe file.

Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
OK people I'm past the issue of static variables.Took your advise Salem , and things worked out.
What's giving me hard time now is this:-
When I build my DLL , it requires a couple of files , say 1.h 2.h ..., to be included.These are listed as External Dependencies.This part is fine and I can build my Dll .
Now when I get to the point of using the DLL in a project , I get errors like :
error LNK2001: unresolved external symbol _htTraceInit
These symbols I can trace to those files which were included as "External Dependencies" while building the DLL.
Why do these errors occur , even though I include the files in question (1.h) in the project that is using the DLL.
Do the functions in files under external dependencies have to be exported too?
 
If the external dependancies are functions in the C libraries, then it is probably something that has been omitted in the linker settings. You see, as well as including files (which give function prototypes, data structures etc.) the appropriate libraries need to be stated for the linker. Most standard include files have their corresponding libraries already included in the link step so usually it is not a problem. Here is an example that uses an *unusual* function:
Code:
#include <windows.h>

int WINAPI WinMain (HINSTANCE,HINSTANCE,LPSTR,int)
{
  mciSendString(&quot;set cdaudio door open&quot;, 0, 0, 0);
  return (0);
}
The program uses mciSnedString to open the CD door. The function is located in the library &quot;winmm.lib&quot;, which is not in the normal link step.
(To include it using MS C++, go to menu 'Project', item 'Settings' and at Link tab type in the library.)
Hopefully it is clear what library to include in the link. Often the library name is the same as the include name.:)
 
Thankz people ,
I had to work a lot on my code to free it of references made to data members from the DLL and indeed I was missing out on files to link.
Now I am getting this execution time error &quot;Memory Access violation in NTDLL.DLL 0xC0000005&quot;.
How best to debug the code at this stage?Running in debug mode gives me a dump and the instruction which is violating , but how do I fix the error?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top