Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoiding "already defined in ..." error/warring in c++ none class variables

Tags:

c++

warnings

I have global variables that I define in the Utility namespace. This utility is included in several files and it looks like this:

#ifndef _UT_
#define _UT_
namespace UT {
  std::string PLATFORM_LINUX_NAME = "linux";
  std::string  PLATFORM_MACOSX_NAME = "macosx";
  std::string  PLATFORM_WINDOWS_NAME = "windows";
  #if defined(OS_WIN)
    int PLATFORM = OSTYPE::PLATFORM_WINDOWS;
  #elif defined(OS_LINUX)
    int PLATFORM = PLATFORM_LINUX;
  #elif defined(OS_APPLE)
    int PLATFORM = PLATFORM_MACOSX;
  #endif
};

When I include this file in for example files A.h and B.h and C.h, I'm getting a compiler warning that says:

warning LNK4006: "int UT::PLATFORM" (?PLATFORM@UT@@3HA) already defined in A.obj; second definition ignored
warning LNK4006: "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > UT::PLATFORM_LINUX_NAME" (?PLATFORM_LINUX_NAME@UT@@3V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@A) already defined in A.obj; second definition ignored

What is the best way that does not involve creating a class for solving this? Or is creating a UT class the only way?

like image 559
user63898 Avatar asked Oct 15 '25 20:10

user63898


2 Answers

Define the variables in a single .cpp file and declare them in the .h file. In UT.h:

namespace UT
{
    extern const std::string PLATFORM_LINUX_NAME;
    extern const std::string PLATFORM_MACOS_NAME;
    extern const std::string PLATFORM_WINDOWS_NAME;
    extern const int PLATFORM;
}

in UT.cpp:

namespace UT
{
    const std::string PLATFORM_LINUX_NAME   = "linux";
    const std::string PLATFORM_MACOS_NAME   = "macosx";
    const std::string PLATFORM_WINDOWS_NAME = "windows";

    #if defined(OS_WIN)
    const int PLATFORM = OSTYPE::PLATFORM_WINDOWS;
    #elif defined(OS_LINUX)
    const int PLATFORM = PLATFORM_LINUX;
    #elif defined(OS_APPLE)
    const int PLATFORM = PLATFORM_MACOSX;
    #endif


}

I added const qualifier as these appear to be constant values.

like image 73
hmjd Avatar answered Oct 17 '25 10:10

hmjd


One solution is to make them all static, in which case each object file gets its own copy. Another possibility is to only put the declaration in the header and put the definitions in a separate file.

like image 22
Antimony Avatar answered Oct 17 '25 10:10

Antimony



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!