Search code examples
c++structglobal-variablesextern

Extern unnamed struct object definition


I got a global object of type "unnamed-struct" and i'm trying to define it. I don't want to pollute my global namespace with such useless type (it will be used only once).

Global.h

extern struct {

    int x;

} A;

Is there any correct way to define such object?
I was trying this:

Global.cpp

struct {

    int x;

} A = { 0 };

But VS2012 throws "error C2371: 'A' : redefinition; different basic types". Thanks.


Solution

  • One possible solution: create another file Global_A.cpp that does not include Global.h, and define A there. By the equivalent-definition rule this will be valid, as long as the anonymous struct definitions are equivalent.

    This is still a bad idea, and most compilers will warn about it e.g. (gcc): warning: non-local variable `<anonymous struct> A' uses anonymous type.