extern Struct

Started by
6 comments, last by Silly_con 21 years ago
I have a struct:
  
struct Window_s {
 ...
} Window;
  
created in one cpp file, and I want to use it in other file h/cpp, but if I do extern Window_s Window; I get: error C2146: syntax error : missing '';'' before identifier ''Window''. How to declare extern a struct?
Advertisement
put the definition of your structure in a header file. then all you need to do is #include that header file in any cpp files that use that structure.

-Arek the Absolute
-Arek the Absolute"The full quartet is pirates, ninjas, zombies, and robots. Create a game which involves all four, and you risk being blinded by the sheer level of coolness involved." - Superpig
You''d better declare your struct in the header file itself. #include that in your other cpp files.

Jappie
----------------------------- JappieBabJap Productions"There's no such things as bugs; they're just unintentional extra features"
doing that I get a linker error

fatal error LNK1169: one or more multiply defined symbols found
Note that the definition of the struct (that is, the type, not the particular instance) must be known in each unit where it is known. Note also that the particular instance must be defined only once. Hence, in the header file, define the struct itself and declare the extern instance; define the instance in one source file.

window_s.h

  struct Window_s {    // ...};extern struct Window_s Window;  


window_s.c

  #include "window_s.h"struct Window_s Window;  

... Now every other source file that #includes window_s.h will also include an extern reference to Window, which is declared exactly once.

This is really no different from declaring, say, an extern int -- it''s just that since the type is user defined, its definition must be included in every compilation unit that uses it.
sorry for be too heavy, but I have made what miserable tells, and I get linker error unresolved external symbol ''struct Window_s Window''
quote:Original post by Anonymous Poster
sorry for be too heavy, but I have made what miserable tells, and I get linker error unresolved external symbol ''struct Window_s Window''

... That''s odd. Are you sure you linked the source file with the actual global variable declaration (window_s.c)? (In IDE''s such as VC++, for those who don''t already know this, this would probably just mean to include the file in the project.)
yes, its all included in the project

This topic is closed to new replies.

Advertisement