Thread: Extern / Globals

  1. #1
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465

    Extern / Globals

    Hey,

    I was trying to do some simple stuff using global variables but my main file was getting huge! I broke it up and I had like

    main.cpp
    Code:
    int global1;
    int global2;
    int global3;
    
    int main()
    {
        return 0;
    }
    and then blah.h
    Code:
    extern "C++"
    {
        int global1;
        int global2;
        int global3;
    }
    and then blah.cpp
    Code:
    #include "blah.h"
    
    void blah() { /* uses globals */ }
    Unfortunately though I get that the variables are already defined as if the extern declaration is a new definition!

    Code:
    1>------ Build started: Project: EmptyProject, Configuration: Debug Win32 ------
    1>Compiling...
    1>Events.cpp
    1>Linking...
    1>Events.obj : error LNK2005: "struct ID3DXMesh * g_pMesh" (?g_pMesh@@3PAUID3DXMesh@@A) already defined in EmptyProject.obj
    ...
    1>Debug\EmptyProject.exe : fatal error LNK1169: one or more multiply defined symbols found
    Is this not the right way to use extern? I thought that's how you refer to global symbols -- with extern?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Tonto
    I was trying to do some simple stuff using global variables but my main file was getting huge!
    Why are you using global variables in the first place? If you absolutely must use global variables, then declare them as extern in the header:
    Code:
    extern int global1;
    extern int global2;
    extern int global3;
    Then define them in exactly one source file.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    I thought I had tried that @_@

    I'll avoid the use of them somehow -- singleton or something --

    Thanks for the quick response

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Tonto View Post
    I thought I had tried that @_@

    I'll avoid the use of them somehow -- singleton or something --

    Thanks for the quick response
    A singleton is really just a clever disguise for a global variable...
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    I know

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Tonto View Post
    I know
    I usually bundle up my "global" variables into a class or struct, and pass this object around by reference to any function which needs access to it. It's painful, but a net gain. If you ever find yourself having to multi-thread your code, it makes life easier.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by Tonto View Post
    I thought I had tried that @_@
    Linkage specification blocks (extern "whatever" {}) do not prevent variable declarations from being definition.
    Curiously enough, a direct linkage specifiation on the variable does.
    Code:
    {}
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by CornedBee View Post
    Linkage specification blocks (extern "whatever" {}) do not prevent variable declarations from being definition.
    Curiously enough, a direct linkage specifiation on the variable does.
    Code:
    {}
    It's got nothing to do with the curly braces. It's related to whether the optional string parameter is given. You would see the same thing if you compared
    Code:
    extern int x;
    and
    Code:
    extern "C" int y;    // equivalent to extern "C" {int y;}
    That string qualifier "C" changes the meaning.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    No, that's my point.
    Code:
    extern "C" int x;
    and
    Code:
    extern "C" { int x; }
    are *not* equivalent. C++ 3.1/2 says so:
    A declaration is a definition unless it declares a function without specifying the function's body, it contains the extern specifier, or a linkage-specification[24] and neither an initializer nor a function-body, ...

    [24] Appearing inside the braced-enclosed declaration-seq in a linkage-specification does not affect whether a declaration is a definition.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    What about static class variables? Act like global variables inside a namespace, without the hassle of extern? You can tell I've never read the specification.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    What about static class variables? Act like global variables inside a namespace, without the hassle of extern?
    You mean you find it more hassle to put extern before a declaration than to put that declaration into a class, which means that every use of the variable must be qualified with the class since using declarations don't extract static members from classes?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. extern classes
    By Swordsman in forum C++ Programming
    Replies: 1
    Last Post: 05-07-2008, 02:07 AM
  2. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  3. Odd Problem with Linking
    By jamez05 in forum C++ Programming
    Replies: 6
    Last Post: 09-21-2005, 01:49 PM
  4. Extern Question, really confused
    By SourceCode in forum C Programming
    Replies: 10
    Last Post: 03-26-2003, 11:11 PM
  5. extern symbols, what do these mean exactly?
    By Shadow12345 in forum C++ Programming
    Replies: 8
    Last Post: 11-02-2002, 03:14 PM