I need to define a constant containing an environment variable (Linux, g++). I would prefer to use string
, but std::getenv
needs *char
You are including the header twice. Once from DataLocation.cpp (where it finds HEADER_DATALOCATION_H
not yet defined and thus defines ENV_APPL_ROOT
), and once from Test.cpp (where it agains finds HEADER_DATALOCATION_H
not yet defined and thus defines ENV_APPL_ROOT
again.) The "header protection" only protects a header file being included multiple times in the same compilation unit.
You need:
extern const char* ENV_APPL_ROOT;
in the header file, and
const char* ENV_APPL_ROOT = "APPL_ROOT";
in one .cpp file.