3

I have declared a global variable in header.h and included that header in source.cpp and main.cpp but linker is giving error

Source.obj : error LNK2005: "int globalVariable" (?globalVariable@@3HA) already defined in     Main.obj
GlobalVariableAndLinkageIssue.exe fatal error LNK1169: one or more multiply defined symbols found

header.h

int globalVariable;

source.cpp

#include "header.h"

main.cpp

#include"header.h"

void main() {}
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
Maddyfire
  • 61
  • 5
  • possible duplicate of http://stackoverflow.com/questions/1433204/how-do-i-share-a-variable-between-source-files-in-c-with-extern-but-how – WhozCraig Oct 11 '14 at 04:55

5 Answers5

3
  1. Move the declaration to a .cpp file. You can use a declaration in a header file by using:

    extern int globalVariable; // declaration in header - can have as many as you need

    But the .cpp file should have the definition:

    int globalVariable; // definition in .cpp - only need one across all your files

    C and C++ use textual pre-processor to include headers, this is basically a text insertion, not a smart module system as in some languages. By including it as you were, you are creating multiple definitions, one per .cpp file.

  2. As a matter of good practice, you need to get used to using include guards to protect against multiple nested includes (though it would not solve your current issue). If using Visual C++, you can use #pragma once or to use a portable solution wrap your header code in:

    #ifndef _INCLUDE_FOO_H_

    #endif

codenheim
  • 20,467
  • 1
  • 59
  • 80
1

To create a global variable, you should do the following. Note that in the header, we mark the variable as extern, and we actually create the object in a cpp file.

header.h

extern int globalVariable;

header.cpp

#include "header.h"
int globalVariable;

main.cpp

#include "header.h"

int main() {}
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
1

Put global variable in some .c or .cpp file, so that it can be defined only once and refer in header file using extern for example,

header.h

extern int globalVariable;

header.cpp

int globalVariable = 0;

source.cpp

#include "header.h"

main.cpp

#include"header.h"

int main() {
  return 0;
}
Dr. Debasish Jana
  • 6,980
  • 4
  • 30
  • 69
0

Because BOTH sources #include your header, and thus it is DEFINED twice.

Ron Pinkas
  • 357
  • 2
  • 10
-2

In such situation,it is common to use some #define as follows:

//header.h
#ifdef DEFINE_VARS
   #define DEFINE_OR_DECLARE 
#else
   #define DEFINE_OR_DECLARE extern
#endif

DEFINE_OR_DECLARE int globalVariable;


//main.cpp
#define DEFINE_VARS
#include "header.h"
...

//header.cpp
#include "header.h"
...
Hamzahfrq
  • 696
  • 1
  • 6
  • 25
Ron Pinkas
  • 357
  • 2
  • 10
  • I would never recommend this. – Bill Lynch Oct 11 '14 at 05:12
  • Could you kindly explain why? – Ron Pinkas Oct 11 '14 at 05:18
  • Let's say I want to have `globalVariable` have a default value of 4. So I do `DEFINE_OR_DECLARE int globalVariable = 4;`. That will cause warnings that look like: `a.c:1: warning: ‘x’ initialized and declared ‘extern’ `. – Bill Lynch Oct 11 '14 at 05:20
  • But you would NEVER specify an assignment in such use. This technique is very productive in making sure header files never miss newly introduced var[s]. One then uses an initialization routine in the definition source, to avoid the default value scenario you described. – Ron Pinkas Oct 11 '14 at 05:28