Public Group

Archived

This topic is now archived and is closed to further replies.

extern Struct

This topic is 5562 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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?

Share on other sites
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

Share on other sites
You''d better declare your struct in the header file itself. #include that in your other cpp files.

Jappie

Share on other sites
doing that I get a linker error

fatal error LNK1169: one or more multiply defined symbols found

Share on other sites
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.

Share on other sites
sorry for be too heavy, but I have made what miserable tells, and I get linker error unresolved external symbol ''struct Window_s Window''

Share on other sites
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.)

Share on other sites
yes, its all included in the project

1. 1
2. 2
3. 3
4. 4
Rutin
16
5. 5

• 12
• 9
• 12
• 37
• 12
• Forum Statistics

• Total Topics
631419
• Total Posts
2999977
×

Important Information

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!