Sign in to follow this  

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

This topic is 4684 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Try declaring it with extern keyword everywhere in header files.

Share this post


Link to post
Share on other sites

// In your header
class 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...

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.h
class Libclass
{
public:
static int *X;
void Run(int &num);
};






// this is libclass.cpp
void 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?

Share this post


Link to post
Share on other sites
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.h
extern int *X;

// globals.cpp
int *X = 0;

Share this post


Link to post
Share on other sites

This topic is 4684 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this