Jump to content
  • Advertisement


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


Variable scope to other files

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

How do you make variables accesible from other .cpp files? Example: Hello.cpp int Hello = 10; //Initiating Hello in Hello.cpp Goodday.cpp int Goodday = Hello; //Initiating Gooday in Goodday.cpp sHaKaZeD

Share this post

Link to post
Share on other sites
By defining the variable in only one implementation file, and putting a declaration in each file that needs access to it.


int x = 0;

Is a declaration and definition, whereas this:

extern int x;

Is a declaration only.

So, in short, write "extern int x" in each file that requires access to the variable, and the linker will sort it out.

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!