Skip to main content
GameDev.net gamedev.net
🔒 Locked

static variable export in dll

Started by Sieggy Jun 20, 2002 at 10:26 AM 2 replies 350+ views
Original Post
Sieggy
Sieggy
I have a dll that has a static int (a reference counter) that is being used by another dll. The int is a member of an exported class (via MFC AFX_EXT_CLASS macro). I have the int properly initialized and the dll builds and links fine. The problem occurs when another dll tries to use it. It compiles fine but it will not link claiming to not be able to find the static int. I have tried applying specific exports to the variable init but to no avail. I checked the exports in the first dll and the variable is listed. Any clues? Thanks ahead of time! Sieggy
IndirectX
IndirectX
Can second dll use other exports of the first dll?
---visit #directxdev on afternet <- not just for directx, despite the name
Sieggy
Sieggy
For classes and so forth yes, and it does so heavily. The class in question to which the reference counter was applied is in heavy use by the second dll with no problems.
IndirectX
IndirectX
The reference counter isn''t found because you aren''t importing it.

I''ve built a test project and originally I defined DLLAPI as dllexport for both exe and dll by mistake, and I experienced the same behavior. Then I noticed .exp file for my .exe file, and I was surprised because .exe wasn''t supposed to export anything! There I noticed that I copy-pasted DLLAPI macro without changing it from .dll to .exe.

The problem with AFX_EXT_CLASS is that it''s always defined as dllexport if you''re building your dll. This is not what you want. When you''re building second dll, you want some classes marked with AFX_EXT_CLASS (the ones that reside in first dll) to be dllimported and others (the ones that reside in second dll) to be dllexported. Unfortunately, a single macro won''t cut it. There are more details in the docs, see MFC Technical Notes.

What you need to do is have a per-project (for each dll) define like DLLONE_EXPORTS that is defined when that dll is built and is undefined otherwise. Look at your define list; such define may already be there. Then, you need another define that will control either importing or exporting:

#ifdef DLLONE_EXPORTS
#define DLLONE_API __declspec(dllexport)
#else
#define DLLONE_API __declspec(dllimport)
#endif

Now if you''re building the first dll, DLLONE_EXPORTS will be defined and classes defined in dllone will be exported. However, if you''re building the second dll, DLLTWO_EXPORTS will be defined and DLLONE_EXPORTS will not be defined. As a result, classes defined in dlltwo will be exported and classes defined in dllone will be imported.

I hope this was clear enough. Post if you have more questions.
---visit #directxdev on afternet <- not just for directx, despite the name

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.