Jump to content
  • Advertisement
Sign in to follow this  
avocados

Share global variables between cpp source files?

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

My project is getting too large to scroll around in one file. I think I want to split the functions up into groups like main, file, render, game logic, sound, etc and then minimize interaction between them. What's the proper way to do this? Source files can share functions if they've been prototyped but I don't know how to share global variables. Should I just keep one source file and make the rest header files instead?

Share this post


Link to post
Share on other sites
Advertisement
In the header file declare the globals as extern, then in a single source file define them at namespace level (not inside a class or function). ex:

header:
extern int evil_global;

source file:
int evil_global = 0;

For more details see this article.

Share this post


Link to post
Share on other sites
But how about splitting up my functions that use openGL calls?

If they are in different source files I'd have to include the openGL headers and also initialize all the ARB extensions for each file.

Share this post


Link to post
Share on other sites
Quote:
Original post by avocados
But how about splitting up my functions that use openGL calls?

If they are in different source files I'd have to include the openGL headers and also initialize all the ARB extensions for each file.

You could make a single module (source + header) that interfaces openGL and have your own inline functions call openGL routines. This will also make your life easier if you decide to port over to another API.

In other (most) situations, you usually need to include headers in every source file. That happens quite frequently if you lay it out correctly.

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!