-1

I'm getting multiple definition of global variable errors when I try to compile:

/usr/bin/ld: ../grpc/server.o:(.bss+0x0): multiple definition of `TABLET[abi:cxx11]'; backend_server.o:(.bss+0x0): first defined here
/usr/bin/ld: ../grpc/server.o:(.data+0x0): multiple definition of `VB'; backend_server.o:(.data+0x0): first defined here
/usr/bin/ld: file_reader_functions/password_reader.o:(.bss+0x0): multiple definition of `TABLET[abi:cxx11]'; backend_server.o:(.bss+0x0): first defined here
/usr/bin/ld: file_reader_functions/password_reader.o:(.data+0x0): multiple definition of `VB'; backend_server.o:(.data+0x0): first defined here
/usr/bin/ld: file_reader_functions/file_writer.o:(.bss+0x0): multiple definition of `TABLET[abi:cxx11]'; backend_server.o:(.bss+0x0): first defined here
/usr/bin/ld: file_reader_functions/file_writer.o:(.data+0x0): multiple definition of `VB'; backend_server.o:(.data+0x0): first defined here

I define my global variables in backend/globals.h:

#pragma once
#include <unordered_map>
#include <string>

using namespace std;

unordered_map<string, unordered_map<string, char*>> TABLET;
bool VB = true; // Verbose

Then in each file where I use these global variables, I simply include this globals.h file.

I'm 100% certain I'm not redefining the global variables anywhere else, nor am I using extern.

I've also tried with the old school header guards:

#ifndef GLOBALS_H
#define GLOBALS_H
#include <unordered_map>
#include <string>

using namespace std;

unordered_map<string, unordered_map<string, char*>> TABLET;
bool VB = true; // Verbose

#endif

Any reasons why I'm getting this issue?

Mat
  • 195,986
  • 40
  • 382
  • 396
doctopus
  • 4,787
  • 7
  • 39
  • 81

1 Answers1

3

I'm 100% certain I'm not redefining the global variables anywhere else

You're defining them in each source file that is including this header file.

Instead, define them in a single source file, and, instead, extern them in the header file. You want to define them at a single point, but you can declare them several times.

Ami Tavory
  • 71,268
  • 10
  • 134
  • 170