c++ Global Variables Across Multiple Files
You could simply use M_PI from the include (there are other constants too).
Edit: your setup is correct. I got a working minmal example:
globals.h
extern double g_tst;
globals.cpp
#include "globals.h"
double g_tst = 4.0;
main.cpp
#include "globals.h"
#include <stdio.h>
#include <stdlib.h>
int main()
{
fprintf (stderr, "g_tst = %lf \n", g_tst);
return 0;
}
The problem is within your buildsystem
See wikipedia
g_pi musst not be declared extern in one translation unit. You could use a small #define for this
in globals.cpp
#define MY_EXTERN_CPP
in /*globals.h
#ifdef MY_EXTERN_CPP
#define MY_CONFIGURATION_EXTERN
#else
#define MY_CONFIGURATION_EXTERN extern
#endif
MY_CONFIGURATION_EXTERN double g_pi;
so g_pi will be extern in all translation units you include it except globals.cpp
I think the problem is that you've got #include gobals.h instead of #include globals.h. This would give you the undefined references because it isn't inserting globals.h. The C++ precompiler doesn't fail when it can't find a header file. Instead you get an undefined reference message at compilation.
The order of linking might be the problem. Try to link the global object file as the last one.