Skip to main content
gamedev.net
🔒 Locked

Share global variables between cpp source files?

Started by avocados Aug 26, 2005 at 4:38 PM 4 replies 750+ views
Original Post
avocados
avocados
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?
SiCrane
SiCrane
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.
avocados
avocados
Ah, perfect, thanks.
avocados
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.
Kest
Kest
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.