Re: CRITICAL_SECTION not initialized globally in DllMain

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance




As you have discovered, declaring variables in header files is a bad idea. In this case, the same header file being included in multiple compilation units (.c or .cpp source files) causes multiple declarations of the same variable; unless, as when you specified static, the declarations are local scope (internal linkage) to the compilation unit. To find out more, read the C or C++ spec or some of the commentary in MSDN.

"Fabian" <Fabian@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message news:7C4EE0DA-B153-43D9-B8F3-B8BF56E894D9@xxxxxxxxxxxxxxxx
Actually I solved it. I forgot to define it:

declaration in header file:

extern "C" CRITICAL_SECTION csCalibrate;

definition in cpp file:

CRITICAL_SECTION csCalibrate;


"Fabian" wrote:

Hi Roger,

"rogero" wrote:

> On Jul 6, 3:00 pm, <dan...@xxxxxxxxxxxxxxxx> wrote:
> > What is the point of declaring a static variable if it's already > > global.
> > Does static global not mean it's valid only in the context of the > > same
> > source file ?
>
> It means each source file gets it's *own* copy of the variable.
> This, I believe, explains the AV - the variable that gets initialised
> is in one source file and is a *different* variable to the one in the
> FindRotationCenter.
>
> Roger.
>

If I only declare it global in the header file the linker strikes:

Error 1 error LNK2005: "struct _RTL_CRITICAL_SECTION csCalibrate"
(?csCalibrate@@3U_RTL_CRITICAL_SECTION@@A) already defined in
dllmain.obj FindRotationCenter.obj BeadAlignment

How else do I solve this?

Thx,

Fabian

.



Relevant Pages

  • Re: static function declaration in header file
    ... >> Is there any reason for declaring functions as static in a header file ... >> called in the source file. ... >> The functions are declared static as an optimization. ... What overhead is saved by declaring the functions static? ...
    (comp.lang.c)
  • Re: static function declaration in header file
    ... > Is there any reason for declaring functions as static in a header file ... > compiler throws a warning for every such function declared but not ... What overhead is saved by declaring the functions static? ...
    (comp.lang.c)
  • Re: static function declaration in header file
    ... >Is there any reason for declaring functions as static in a header file ... What overhead is saved by declaring the functions static? ... I may still be sleepy this morning, but I can't think of any reason to ... Balmer Consulting ...
    (comp.lang.c)
  • static function declaration in header file
    ... Is there any reason for declaring functions as static in a header file ... argument is not a good solution since extern functions have additional ... What overhead is saved by declaring the functions static? ...
    (comp.lang.c)
  • Re: Link Error 2019
    ... It sounds like you are *declaring* some GUIDs for various things, ... define a preprocessor symbol to tell the header file that declares them to ... This might be INITGUID. ...
    (microsoft.public.windowsce.embedded.vc)