避免 c++ none 类变量中的"already defined in ..."错误/交战
Avoiding "already defined in ..." error/warring in c++ none class variables
我有我在实用程序命名空间中定义的全局变量。此实用程序包含在多个文件中,如下所示:
#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
};
当我将此文件包含在例如文件 A.h 和 B.h 和 C.h 中时,我收到一个编译器警告,上面写着:
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
不涉及创建类来解决此问题的最佳方法是什么?还是创建 UT 类是唯一的方法?
在单个.cpp
文件中定义变量,并在.h
文件中声明它们。在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;
}
在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
}
我添加了const
限定符,因为这些似乎是常量值。
一种解决方案是使它们全部静态,在这种情况下,每个对象文件都会获得自己的副本。另一种可能性是仅将声明放在标头中,并将定义放在单独的文件中。
尝试
#ifndef _INCL_GUARD
#define _INCL_GUARD
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
#endif
相关文章:
- netcat command in c++
- Difference in displaying cv2 Mat
- C++ MFC Libraries in Travis CI
- 如何在OpenSSL中从configuration.h.in获取configuration.h
- 创建具有 new in 函数和"this is nullptr"异常的对象
- IN, OUT, INOUT Parameters
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- 有人安装"IITB Simplecpp in mac"吗?
- 为什么"types may not be defined in parameter types"
- "... already defined in *.obj"但我没有在同一范围内两次定义任何变量
- 是什么导致了代码中出现"error LNK2005: already defined in .obj"错误?
- "inline function need to be DEFINED in all tranlation units"背后的理由是什么?
- 如何修复"defined in discarded section"链接器错误?
- 避免 c++ none 类变量中的"already defined in ..."错误/交战
- 全局指针"already defined in"错误
- 错误 LNK2005:"already defined in SkinHeaderCtrl.obj"
- 异常C++链接器错误 - 'Defined in discarded section'
- "Error: Symbol TMath is not defined in current scope"
- 避免"pointer cannot be null in well-defined C++ code"的投射策略
- decltype - "the only context in which a variable defined as a reference is not treated as a synonym