Originally Posted by
anduril462
If you really need global variables for something (I highly doubt you need them for your program), I recommend looking into a singleton pattern when possible. Make the variable a static global in one .c file, so no other files can see it. Provide a set of functions in that .c for accessing and manipulating that variable, and only list the prototypes for those functions in the header.
Wouldn't that mean that every time a function in one of the other .c files uses one of the variables, that it needs to call a function that creates this variable? That's exactly what I am trying to avoid. These variables are constant from the moment they are created, it makes no sense to reacalculate them every single time they are needed in a function.
Bytheway I tried the #ifndef stuff but I still get this annoying error telling me I already defined phys_MO, I even used a seperate #ifndef for this variable. Why is it still giving me this error? This is my current constants.h:
Code:
#ifndef CONSTANTS_H
#define CONSTANTS_H
#ifndef VAR_PHYS_MO
#define VAR_PHYS_MO
extern double phys_MO = 4.94296E-6 ;
#endif //VAR_PHYS_MO
extern double c_array[6] ;
extern double EOB_M, nu;
extern double v_MECO, v_pole ;
extern double omega0;
#endif //CONSTANTS_H
It shouldn't matter how many times I include this header file right? It shouldn't even be nessecary to use #ifndef anymore around the phys_MO variable. My main.c looks like so:
Code:
#include "constants.h"
#include "constant_calculations.h"
#include "initial_value_calculator.h"
double EOB_M, nu, v_MECO, v_pole, c_array[6], omega0, phys_MO ;
int main(){ etc...