c++ - #ifndef works only for "declaration" parts? -


i have such iglobal.h file:

#ifndef _iglobals_ #define _iglobals_  #define _use_math_defines  #define plug_num_channels 2 #define plug_num_presets 1 #define plug_fps 100 #define plug_ppq 96 #define knob_num_frames 128 #define pianoroll_min_notes 2 #define pianoroll_max_notes 8 #define pianoroll_min_velocity 40 #define pianoroll_max_velocity 127 #define pianoroll_pattern_length plug_ppq * 4 #define lfo_num_samples 882 #define waveplayer_width 441 #define waveplayer_height 136 #define waveplayer_path_length 256 #define envelope_min_value 0.0001  #include <algorithm> #include <random> #include <chrono> #include <math.h> #include <cmath> #include "iplug_include_in_plug_hdr.h"  using namespace std;  // bitmaps ibitmap gbitmapknobgeneral;  #endif // !_iglobals_  

which include .h/.cpp file within project. ibitmap struct.

when build (compile), says:

lnk1169 1 or more multiply defined symbols found lnk2005 "struct ibitmap gbitmapknobgeneral" ?gbitmapknobgeneral@@3uibitmap@@a) defined in icustomcontrols.obj 

and in fact have each time include iglobal.h. shouldn't #ifndef discard problem? or compiler automatically declarations , not definitions?

to answer question #ifndef works entire file.

the problem when write ibitmap gbitmapknobgeneral; create instance in every source file includes header. assume trying create single global instance. in case want write.

  extern ibitmap gbitmapknobgeneral; 

in iglobal.h file , create instance in 1 of source files instance iglobal.cpp with

 ibitmap gbitmapknobgeneral; 

Comments