# Exporting static class variables (C++)

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

## 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 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.hppclass 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.ca 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 on other sites
Man... all I can say is, sweeeeeeeeeeeet :D

Worked like a charm!
Huge thanks

##### Share on other sites
No problem. I kept editing in better explainations because I was bored [lol].

1. 1
Rutin
34
2. 2
3. 3
4. 4
5. 5

• 12
• 14
• 9
• 9
• 9
• ### Forum Statistics

• Total Topics
633334
• Total Posts
3011407
• ### Who's Online (See full list)

There are no registered users currently online

×