I am modifying an already working game which contains hundreds of source and header files.

In shareddefs.h, i declared a global variable (bool g_CameraTPOVMode;).

I initialize (or set) the variable to true or false in in_camera.cpp when a certain function is called. (g_CameraTPOVMode = true;)

Compile goes okay, but when it links(which is a subject in itself that is driving me insane!) i get a LNK2005 "...already defined in gamerules.obj"

once from in_camera.obj, once from stdafx.obj, once from both in_joystick.obj and in_mouse.obj.
I havn't even made any changes to in_mouse, in_joystick, or stdafx!

How or why are these three files complaining about the redefinition of the g_CameraTPOVMode?

And why is in_camera.obj complaining about the redifinition of this global variable in the first place? Am i not simply changing the value of the global variable?

I miss my beginner days when a program involved one source file and many header files.
A finger, nudge, or even a military boot kick in the right direction would be greatly appreciated.

There are two similar terms used in C and C++ (and probably many other programming language descriptions): declare and define.

You can declare a thing as many times as you like in C and C++. All this says is that somewhere there is a definition of such a thing.

However you can only define something once - it is called the one definition rule (or ODR). The scope of once depends on the scope of the thing being defined. For class definitions for example once is generally once in the translation unit being compiled (a translation unit is what the compiler compiles after the pre-processor has done its work so includes all included files, all #define macro expansions, all conditionally compiled sections of code that are to be included etc.).

Now in most cases in C and C++ an object (a.k.a. variable) declaration is also its definition. The difference is that a definition reserves space for the object and (if appropriate) initialises it. An object declaration without also being a definition just says that such an object exists somewhere.

Now for global variables that have external linkage (i.e. they can be seen by the linker) - which are those not qualified as static - the scope of once in the one definition rule is the whole linked program (or dynamically linked library).

Hence in your case the declaration of:

   bool g_CameraTPOVMode; // declaration and definition

in shareddefs.h is also its definition. Thus every object file that results from compiling code which includes shareddefs.h defines a new instance of the global and externally visible g_CameraTPOVMode. When the linker links all these object files together it finds these multiple definitions of g_CameraTPOVMode and complains on the second and probably subsequent instances it finds.

To get around this problem C, and thereby C++, use the extern keyword to convert a global variable definition into a declaration only:

   extern bool g_CameraTPOVMode; // declaration only

The problem with just doing this is that now no object file will contain a definition of g_CameraTPOVMode and you will receive undefined symbol errors from the linker instead.

The solution is to pick one and _only_ one implementation file (.cpp file) that defines g_CameraTPOVMode. It does not matter if it is also declared so such a file can also include shareddefs.h. If no such source file is an obvious owner for the g_CameraTPOVMode variable then create a new one - maybe shareddefs.cpp. Include shareddefs.h then if you change the declaration and forget to update the definition (or the other way around) the compiler stands a chance of telling you the two do not match.

On another note I should point out that global objects a not a good design choice as they can lead to maintenance problems as any piece of code can change the value of a global variable so you end up having to understand large portions of the code - a change of a value in one part of the code can affect the behaviour of unanticipated portions of code elsewhere. They are also not thread safe unless access is protected using proper locking such as a mutex. Also in C++ there is no way to predict or control the order of initialisation (or destruction) of global objects in different object files (which is probably not an issue in this particular case). So try to limit the use of such global object to the absolute minimum.

Hope this is of use.  


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.


©2016 About.com. All rights reserved.