32

I have 4 files (2 headers, and 2 code files). FileA.cpp, FileA.h, FileB.cpp, FileB.h

FileA.cpp:

#include "FileA.h"

int main()
{
    hello();
    return 0;
}

void hello()
{
    //code here
}

FileA.h:

#ifndef FILEA_H_
#define FILEA_H_
#include "FileB.h"
void hello();

#endif /* FILEA_H_ */

FileB.cpp:

#include "FileB.h"

void world()
{
    //more code;
}

FileB.h:

#ifndef FILEB_H_
#define FILEB_H_

int wat;
void world();


#endif /* FILEB_H_ */

when I try to compile(with eclipse), I get " multiple definition of `wat' " And I don't know why, it seems as it should work just fine.

Bg1987
  • 1,129
  • 1
  • 11
  • 25
  • Does this answer your question? [How do I use extern to share variables between source files?](//stackoverflow.com/q/1433204/) – outis May 13 '22 at 03:55

5 Answers5

47

I'm not going to include all of the details, but you define a global variable, wat twice in your compilation uint.

To fix, use the following:

FileB.h

extern int wat;

FileB.cpp

int wat = 0;

This (extern) tells the compile that the variable wat exists somewhere, and that it needs to find it on it's own (in this case, it's in FileB.cpp)

Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201
  • 1
    Does it influence the optimization for `const` variables? Is compiler optimization run at compile stage or the link stage? – ar2015 Feb 28 '18 at 23:55
19

Don't declare the variable in the header. #include literally copies and pastes the contents of the file into the other file (that is, any file that does #include "FileB.h" will literally copy the contents of FileB.h into it, which means int wat gets defined in every file that does #include "FileB.h").

If you want to access wat from FileA.cpp, and it's declared in FileB.cpp, you can mark it as extern in FileA.cpp.

Cornstalks
  • 37,137
  • 18
  • 79
  • 144
3

I found the answer now (I guess looking at the files one after another helped) The problem is that the compiler creates a FileB.o which has a definition of wat, and then it tries to compile FilB.o with FileA.cpp, while FileA.h has an include of FileB.h it will now also have a definition of wat.

Bg1987
  • 1,129
  • 1
  • 11
  • 25
  • 1
    I only one single blabla.h file and with include guards (#ifndef xxx_H ...) and a single global variable. At compile time I got the multiple definition of 'myglobal_variable' error. – Kemin Zhou May 07 '17 at 03:05
2

You get multiple definition because wat is declared at file scope and get's visible twice in the 2 source files.

Instead, make the variable declartion extern and define it in exactly one source file.

extern int wat;  // In FileB.h

int wat;   // In FileB.cpp
Mahesh
  • 34,573
  • 20
  • 89
  • 115
0

That wat variable declaration is a definition that should belong in a cpp file. It is as if you would put your function bodies into the header.

You need to put extern before the wat declaration and copy your current declaration without the extern into one of the cpp files.

Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212