How to make a static var in a Lib?(fixed)

Started by
6 comments, last by MetaKnight 19 years, 2 months ago
Hi I was wondering how to make a static var in a Lib? I try to builda a lib with a static var but it ends up getting declared twice. I could take it out of the main build , and leave it in the lib build, but then wouldnt that cause undesired results? If there is no way out of this, how would I be able to share a rendering context with parts of the libary that would need it, like mesh containors [Edited by - MetaKnight on February 17, 2005 10:05:44 AM]
Advertisement
Try declaring it with extern keyword everywhere in header files.
// In your headerclass CFoo{public:   static RenderContext s_Render;};// In a source file in your .lib project:RenderContext CFoo::s_Render;// To access the variable from the main project (which links to your .lib):DoSomething(CFoo::s_Render);

I think that's right anyway...
In general you can get into real trouble if you use static or global vars in libraries. If your just building a single lib into an exe you should be ok but imagine that you build a lib with a global or static in it. Then you build 2 dlls' each of which links in the lib. Then you build an exe which uses both dll's.

Now your exe has 2 of the same variable. How do you access it?? What should the linker do?

Cheers
Chris
CheersChris
Thank you for the replys, but both methods dont seem to work for me.

For putting it in a class, the compiler still declares it twice.

And When I try to extern it ,once I get outside the function that uses it, the compiler complains that it's not there anymore.

Should switching to DLL solve my problem? Isnt there a speed drop for dlls?
Okay, Ill clairify a bit more on the problem, Suppose you have your lib and you have a class like this in the lib:
// this is libclass.hclass Libclass{public:    static int *X;    void Run(int &num);};


// this is libclass.cppvoid Run(int &num){   X = num ;//whatever here}



// this is main.cpp for the problem using the new created lib#include "libclass.h"Libclass Moo;


What happens is i get this error:

error LNK2001: unresolved external symbol "class Libclass * X"

What I think is happening is this:
static var X is showing up in the lib and in the cpp of main as well, but how can I use it without declaring it?


I have tried using extern, extern "C", everything, doesnt seem to work so far, so can anyone help?
Static class member variables need to be initialized somewhere. Regular member variables are initialized when an object is instantiated. Static variables are initialized at startup, so you need to add a definition and initialization to your source file. This is true whether you are building a library or an executable.

// libclass.cpp// this is mandatory!int Libclass::*X = 0;void Libclass::Run(int &num){   X = num ;//whatever here}


Just to make sure you are clear on a couple of points - the only difference between building a library and building an executable is that all of the object files are linked together at the end of the executable build. Linking to library is effectively the same as taking that library's source files and adding them to the executable build.

And the extern keyword that was mentioned only works on global variables - not member variables. You would still need to declare the variable in a source module somewhere:

// globals.hextern int *X;// globals.cppint *X = 0;
Sweet jesus! thanks Aldacron, that's just what I was looking for.

This topic is closed to new replies.

Advertisement