Jump to content
  • Advertisement

Archived

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

Silly_con

extern Struct

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

If you intended to correct an error in the post then please contact us.

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 this post


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

Jappie

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
doing that I get a linker error

fatal error LNK1169: one or more multiply defined symbols found

Share this post


Link to post
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 this post


Link to post
Share on other sites
Guest 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''

Share this post


Link to post
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
yes, its all included in the project

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!