Jump to content
  • Advertisement
Sign in to follow this  
Stompy9999

The extern keyword

This topic is 4837 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

This is something that I haven't yet grasped in C++, but what exactly is the extern keyword used for? I used to program alot in C, and I don't remember ever having to use this. For instance, I once wrote a project in C where I had declared a bunch of global variables in a header file, than used them in source files that included the header file. It worked than fine, but when I did the same thing in C++, I got multiple decrelation errors. I was told to use extern on the globals, so I did, and it worked. However, I never quite understood why or how to use extern. I'm asking this now because I am reorganizing my current project. I want to make sure not to run into any errors when using globals across multiple files. Any help would be appreciated.

Share this post


Link to post
Share on other sites
Advertisement
I have a hard time believing that it worked for you in C. Both C and C++ require that symbols are only defined in a single translation unit (file.c|file.cpp). The extern keyword tells the compiler that a symbol is only a declaration - it is defined somewhere else. If you only ever include a header in a single .c-file, you can get away without the extern. However, if you include it in more than one, the linker will see the same symbol defined in more than one translation unit (one for each .c[pp] file including the header) and rightly flag it as an error.

Share this post


Link to post
Share on other sites
Quote:
Original post by Arild Fines
I have a hard time believing that it worked for you in C. Both C and C++ require that symbols are only defined in a single translation unit (file.c|file.cpp). The extern keyword tells the compiler that a symbol is only a declaration - it is defined somewhere else. If you only ever include a header in a single .c-file, you can get away without the extern. However, if you include it in more than one, the linker will see the same symbol defined in more than one translation unit (one for each .c[pp] file including the header) and rightly flag it as an error.


You're right, it was only one .c file. However, with only one .cpp file, it did not work.

Thanks for the links.

Share this post


Link to post
Share on other sites
C allows what are called tentative declarations. C++ does not. Chances are your header had something like int i; in the header and int i; in the source file. Under C rules as long as two globals are declared with the same type and name they refer to the same variable, the first one is treated as a declartion for the second definition. Under C++ rules, both are treated as definitions and cause a conflict. With extern one becomes a declaration, and the conflict is removed.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!