Re: how to export global variable in C dll?



"Cyrfer" wrote:
My DLL's implementation has problems now that I tried to make a
2nd
implementation file in the DLL "touch" the global variables.
Now the
DLL will not link. The problem is probably because I am not
using
'extern', which also causes my DLL not to link. I'm really
confused
about what is happening. I have made a simple MSVC 2005 project
which
shows the problem. You can find the short code sample here:

http://cyrf.com/temp/test_linking.zip

Would someone show that it is possible to export global
variables?

I tried this project and first of all a couple of remarks.

1. The organization of headers is a little bit messy. Everything
includes everything. So, at the beginning I lost myself in the
code.

2. Avoid including CRT/PSDK headers in your DLL headers.
Especially if DLL headers go to the DLL clients. You can safely
assume that user already included system headers before your DLL
headers. Also, bringing "windows.h", "stdio.h" etc. with you may
interfere with user's intents. For example, I may use precompiled
headers for the system stuff or use special defines before CRT
headers. If suddenly one of my files will include "stdio.h" with
different settings it may break my build.

Now in in your project you have multiple definition problem
(LNK2005). It happens because in "DLLGlobals.h" header you
actually defines the globals. Even though all definitions are
inside extern "C" block, they're still definitions. In order to
solve it you need to do following:

1. Put `extern' modifier before each global in "DLLGlobals.h".
Alternatively, put `extern "C"' modifier in each declaration and
remove "#ifdef __cplusplus" altogether.

2. Add "DLLGlobals.cpp" (or "DLLGlobals.c", it doesn't matter)
file to the "cDLL" project. In this file define global variables.
I.e. (notice no `extern' there):

----- DLLGlobals.cpp -----
#include "DLLGlobals.h"

LIB_API PtrFoo foo;
LIB_API int c_dll_var1;
LIB_API HMODULE c_dll_module_dependency;
LIB_API DLLState c_dll_state;
--------------------------

Now these globals are exported without name mangling (as C names)
and can be used both in C and C++ clients. You can open the
resulting DLL file with Depends tool or run DUMPBIN /export
<dllname> in command prompt to see exact names that are exported.

HTH
Alex


.



Relevant Pages

  • Re: VS2008 destroys static objects before global (non-static) objects?
    ... destroyed before globals. ... While the class definitions are in one DLL o is defined in another DLL ... DLLs are loaded p_ in 2.dll is initialized first followed by an initialization of o and again p_ in 1.dll. ... As the destructor of p_ is then called first which frees all memory managed by the memory manager I get an access violation when o is destroyed. ...
    (microsoft.public.vc.language)
  • Re: how to export global variable in C dll?
    ... DLL will not link. ... Avoid including CRT/PSDK headers in your DLL headers. ... inside extern "C" block, they're still definitions. ... Now these globals are exported without name mangling ...
    (microsoft.public.vc.language)
  • Re: Add-in question
    ... > that I need to put my globals in a class, and it should never go out of ... can "emulate" the global variables and you don't have the scope problem. ... > create an instance of the dll when it's loaded and keep this IDispatch ... the pointer was a global variable. ...
    (microsoft.public.office.developer.com.add_ins)
  • Re: Global variables crashing my app
    ... The globals are objects that ... DLL is a pure DLL ... statements (mpBuff ... void DestroyParams; ...
    (microsoft.public.vc.language)
  • Re: LoadLibrary() returns ErrorCode 193
    ... The DLL, instead of hLib = LoadLibrary, pfn = GetProcAddress(hLib, funcname) would instead itself export a function called, e.g. SetFuncs. ... instances of the internal global variables, ... A DLL ... I agree with previous posts that sharing globals doesn't sound like a very ...
    (microsoft.public.windowsce.embedded)