C++, multiple definition

后端 未结 2 1829
粉色の甜心
粉色の甜心 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.

    0 讨论(0)
  • 2020-12-22 08:20

    Use

    extern const char* ENV_APPL_ROOT;
    

    in the header file, and place

    const char* ENV_APPL_ROOT = "APPL_ROOT";
    

    in one particular translation unit (e.g. DataLocation.cpp).

    This should fix it.

    0 讨论(0)
提交回复
热议问题