include - using a globals struct in several .cpp files, and initializing it in the constructor -


i know similar questions have been asked, none address issue. want create globals struct , initialize default values. implemented below, project won't build.

i've tried can think of, notably moving "extern" declaration of *gxg in , out of header guard , changing struct class, same results: project won't build because of duplicate symbols globals constructor. builds if don't use in more 1 .cpp file, or if don't include constructor or destructor in struct's implementation file.

//  globals.hpp  #ifndef globals_hpp #define globals_hpp  struct gxglobals{     double radius;     bool easement;      gxglobals(); // constructor  } ;  extern "c" gxglobals *gxg;  #endif /* globals_hpp */  —————————————  //  globals.cpp  #include "globals.hpp"  gxglobals::gxglobals():     radius(24),     easement(false)     {};  ———————————  //  main_file.cpp  #include "globals.hpp"  gxglobals *gxg = new gxglobals();  ———————————  //  other_file.cpp  #include "globals.hpp"  // error: duplicate symbol gxglobals::gxglobals() 

i can include globals.h in 1 file, not in 2 or more. works if remove self-initialization in .cpp file.

there many members in actual struct make initializer list practical, last option function runs on startup plugs of default values in. mistaken should work?


Comments