views:

2193

answers:

4
+2  A: 

Are they defined in .cpp file as well? Roughly, it should look like:

struct Format
{
    [...]
    static Format gFmt128;
};
// Format.cpp
Format Format::gFmt128 = { 0, 128, 0 }
yrp
This seems the correct way for a C++ object. Here the static makes the struct member gFmt128 a class variable rather than a global variable with internal linkage.
workmad3
+1  A: 

You need to declare your Format objects as extern not static

Seb Rose
+5  A: 

Don't use the static keyword on global declarations. Here is an article explain the visibility of variables with/without static. The static gives globals internal linkage, that is, only visible in the translation unit they are declared in.

Skizz

Skizz
A: 

Morhveus, I tried this out, too. My linker rather says it has the gFmt128 symbol already defined. This is indeed the behaviour I would expect: the compiler adds the function body to both the library and the client object since it's defined in the include file.

The only way I get unresolved externals is by

  • not adding the static library to the objects-to-be-linked
  • not defining the symbol gFmt128 in the static library's source file

I'm puzzled... How come we see something different? Can you explain what happens?

xtofl
I must have goofed. Skizz's answer seems to be the explanation to what has happened.