Jump to content
  • Advertisement
Sign in to follow this  
andyb716

Global Variables in C++ 6

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

Hi, I'm trying to make a header that will hold global variables. But when I try to compile it says that its already defined, even though I've tried a #ifdef/#define#endif and a #pragma once. Can someone please help me.

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
If you put globals in a header file that means they will be included in multiple cpp files, this leads to linker errors. To resolve this add 'extern' to all declarations, and declare them without extern in 1 cpp file. As a trick you can use:

// header
#ifndef EXTERN
#define EXTERN extern
#endif
EXTERN int myGlobal;

// 1 cpp file
#define EXTERN
#include "header.hpp"

// other cpp file
#include "header.hpp"

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!