Sign in to follow this  
Syranide

Exporting static class variables (C++)

Recommended Posts

Ok, I'm going nuts about this, I've tried and tried and it no longer makes any sense. The basic situation that is the problem is this: I have one class, which has one static function and one static variable (want to make ugly but easy logging), the static function is of course using the static variable. And everything works great, but as fast as I move it outside the current project, to a project of its own, it complains about an unresolved link (the static variable)... I have the exact same class structure (same header), I included the lib, and it won't work. Is there something I've missed on how to export static class variables? As, exporting classes is as easy as nothing, just move it to a separate project and include the lib and header, and it all works, but the static variable makes a fuzz (however, having a static variable but not using works). I could paste some code, but as it is fairly trivial and a basic thought I recon it isn't needed as those who know something about it likely know what I'm talking about. Thanks in advance.

Share this post


Link to post
Share on other sites
It sounds like you're making a library out of your logging class?

With a class-static variable, you have to later define it a second time, to allocate the memory for it like so:

//MyLibrary.hpp
class logger {
static unsigned int log_level;
public:
//...
};

//MyLibrary.cpp (compiled into .dll or .lib or similar)
#include "MyLibrary.hpp"

unsigned int logger::log_level;

//Main.cpp (your .exe that uses the library, needs to link against your .dll or .lib)
int main () {
logger my_logger;
//...
}





When you define a class with only normal variables, you're not actually allocating memory until you declare a variable of that class's type:
struct a {
int b;
int c;
};

a first_a; //allocates first_a.b & first_a.c
a second_a; //allocates second_a.b & second_a.c

When you use static, then these variable declerations don't allocate memory, because there's only to be one version:
struct a {
int b;
static int c;
};

a first_a; //allocates first_a.b - if we allocated first_a.c we'd run into problems bellow:
a second_a; //allocates second_a.b - if we allocated .c each time, we'd now have two .c variables, which isn't what we want.

The line:
unsigned int logger::log_level;

Allocates the single version of the variable. This shouldn't be in a header file, while it may appear to work at first, the moment you use the header in two source files you'll be getting multiple definition errors, the same as when you do:
int i;
int i; //error: int i allready defined


Also, for future reference, allways include the exact error message. Copy and paste. I mean it. Although this one was trivial to fix, most of the time the copy and paste of the exact contents will at worst be un-needed, at best allow us to point out some obscure thing that appears to have no relevance, but does.

HTH

-Mike

Share this post


Link to post
Share on other sites

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