C++, multiple definition

后端 未结 2 1828
粉色の甜心
粉色の甜心 2020-12-22 07:47

I need to define a constant containing an environment variable (Linux, g++). I would prefer to use string, but std::getenv needs *char

2条回答
  •  时光说笑
    2020-12-22 08:19

    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.

提交回复
热议问题