views:

160

answers:

2

I'm trying to share the same variable between two .cpp files, they include the same .h file.

But I'm getting linking errors, telling me that I have multiple definitions. Which I find awkward, since I'm using include guards

//main.cpp
#include <cstdio>
#include "shared.h"

int main(){
  shared_int = 5;
  printVal();
  return 0;
}


//shared.h
#ifndef INCLUDE_GUARD_H
#define INCLUDE_GUARD_H
#include <cstdio>
#include <cstdlib>
int shared_int;
int printVal();
#endif

//shared.cpp
#include <cstdio>
#include "shared.h"


int printVal(){
  fprintf(stderr,"a: %d\n",shared_int);
  return 0;
}

I'm comping like

g++ shared.cpp -c;g++ main.cpp shared.o
shared.o:(.bss+0x0): multiple definition of `shared_int'
/tmp/cci8w8Am.o:(.bss+0x0): first defined here
collect2: ld returned 1 exit status

thanks

Update: The 'extern' still doesnt work, but now I get a undefined reference These are the updated files

//shared.h
#ifndef INCLUDE_GUARD_H
#define INCLUDE_GUARD_H
#include <cstdio>
#include <cstdlib>
//extern "C" int shared_int;//i've tried both
extern int shared_int;
int printVal();
#endif

//shared.cpp
#include <cstdio>
#include "shared.h"

int printVal(){
  fprintf(stderr,"a: %d\n",shared_int);
  return 0;
}

//main.cpp
#include <cstdio>
#include "shared.h"

int main(){
  int shared_int = 5;
  printVal();
  return 0;
}

This is how I compile

g++ main.cpp shared.o
shared.o: In function `printVal()':
shared.cpp:(.text+0x6): undefined reference to `shared_int'
collect2: ld returned 1 exit status
+6  A: 

The declaration in your header file needs an extern:

extern int shared_int;

Then, you will need an actual instance of the definition in one C++ file (such as in shared.cpp):

int shared_int;

The include guards you're using here are good practice, but they won't have any effect in this situation. The include guard prevents the header file from being included more than once in the same source file. Such a situation normally doesn't happen unless you have header files indirectly included from other header files.

Greg Hewgill
+1 Greg. I also noticed that @monkeyking is mixing C header files .h with C++ compiler. That's not a very good practice, so I'll just point out that is probably better if you force the symbol to be export as a C symbol to avoid name mangling problems. Just define the variable on shared.h as: extern "C" int shared_int;
karlphillip
@karlphillip: There is no such thing as a "C header file" - header files are compiled with whatever compiler #includes them. The `.h` name is perfectly acceptable for C++. Some people might use `.hpp` or `.hxx` but that's just style and has no meaning at all.
Greg Hewgill
A: 

Firstly, globals are not a good idea at all and one must avoid as far as possible. Assuming that in your particular situation, it is mandatorily required for whatever reasons, from a pure OO perspective, I would approach in the following way:

a) Define the global variable in one file say a.cpp (also if the variable can be embedded in a namespace)

b) Write get/set functions in a.cpp

c) Access the global variable in a.cpp using the get/set functions.

This way, you would have a very modular design with well-defined interface.

Chubsdad