Build error when declaring global - Programmer

This is a discussion on Build error when declaring global - Programmer ; When hitting Build (F7) within ImageProperties.cpp, with #include "globals.h", which contains BITMAP *gsmybmp; Linking... ImageProperties.obj : error LNK2005: "struct tagBITMAP * gsmybmp" (?gsmybmp@@3PAUtagBITMAP@@A) already defined in ChildView.obj Creating browse info file... Benchmark.exe - 1 error(s), 0 warning(s) The same is ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: Build error when declaring global

  1. Build error when declaring global

    When hitting Build (F7) within ImageProperties.cpp, with #include
    "globals.h", which contains BITMAP *gsmybmp;

    Linking...
    ImageProperties.obj : error LNK2005: "struct tagBITMAP * gsmybmp"
    (?gsmybmp@@3PAUtagBITMAP@@A) already defined in ChildView.obj
    Creating browse info file...

    Benchmark.exe - 1 error(s), 0 warning(s)

    The same is applicable for Build from ChildView.cpp.

    If I use another file, Benchmark.cpp, which is my mainapp file, and does not
    include the "globals.h", I don't get any errors and linking is completed
    successfully.

    Now, once linking is completed successfully, I can recompile and build from
    ImageProperties.cpp without any errors occurring.

    Why is this??? Is this not the correct way to declare global variables,
    pointers etc.???
    If I don't #include "globals.h" into ChildView.cpp or ImageProperties.cpp,
    then there are no errors, but I can't use the pointer.

    As I've written this, I've just introduced errors, compiled, corrected the
    errors, and recompiled, to be certain that my claims are correct. Now the
    program doesn't want to build correctly, regardless from which file I build.
    Anybody know what's happening here???

    Thanks



  2. Re: Build error when declaring global


    "Basil" wrote in message
    news:bfgkfr$aq2$1@ctb-nnrp2.saix.net...
    > When hitting Build (F7) within ImageProperties.cpp, with #include
    > "globals.h", which contains BITMAP *gsmybmp;
    >
    > Linking...
    > ImageProperties.obj : error LNK2005: "struct tagBITMAP * gsmybmp"
    > (?gsmybmp@@3PAUtagBITMAP@@A) already defined in ChildView.obj
    > Creating browse info file...
    >
    > Benchmark.exe - 1 error(s), 0 warning(s)
    >
    > The same is applicable for Build from ChildView.cpp.
    >
    > If I use another file, Benchmark.cpp, which is my mainapp file, and does

    not
    > include the "globals.h", I don't get any errors and linking is completed
    > successfully.
    >
    > Now, once linking is completed successfully, I can recompile and build

    from
    > ImageProperties.cpp without any errors occurring.
    >
    > Why is this??? Is this not the correct way to declare global variables,
    > pointers etc.???
    > If I don't #include "globals.h" into ChildView.cpp or ImageProperties.cpp,
    > then there are no errors, but I can't use the pointer.
    >
    > As I've written this, I've just introduced errors, compiled, corrected the
    > errors, and recompiled, to be certain that my claims are correct. Now the
    > program doesn't want to build correctly, regardless from which file I

    build.
    > Anybody know what's happening here???
    >
    > Thanks
    >
    >

    OK. So I have it working finally. I've declared 'extern BITMAP *gsmybmp' in
    each of the files that require it.
    I found the clue under 'Storage-Class Specifiers for External-Level
    Declarations' within the MSDN Library. I still don't understand why it is
    not considered global when one simply declares the variable outside of any
    function or class, and within only one file. Why does one need to re-declare
    the variable in each file????



  3. Re: Build error when declaring global

    Basil wrote:
    > OK. So I have it working finally. I've declared 'extern BITMAP
    > *gsmybmp' in each of the files that require it.
    > I found the clue under 'Storage-Class Specifiers for External-Level
    > Declarations' within the MSDN Library. I still don't understand why
    > it is not considered global when one simply declares the variable
    > outside of any function or class, and within only one file. Why does
    > one need to re-declare the variable in each file????


    You have to redeclare a global var so that the compiler knows how to
    access it in each file it is used. This way you can compile the source
    files in any order you want.

    OTOH, adding 'extern' is a hint not to allocate space for the specific
    var. If you omit it, each .obj will have its own memory area reserved
    for the global var and the linker will get confused, because it doesn't
    know which one to use.

    The proper way to use globals is to declare them 'extern' in a header,
    include the header in any source files needed, and finally define the
    global (without 'extern') into *one* source file.


+ Reply to Thread