两个第 3 方库中 uint32 的 Typedef 重新定义 (C2371)

Cod*_*lad 4 c++ typedef spidermonkey box2d

在我的应用程序中,我使用Box2DSpidermonkey。这两个库都定义了 uint32 类型,这显然在同一编译单元中使用这两个库时给我带来了编译器错误。

b2settings.h (Box2D): typedef unsigned int uint32;

jsotypes.h (Spidermonkey): typedef unsigned long uint32;

有什么方法可以解决此冲突而不需要更改第 3 方库的标头吗?

我很感谢每一个提示!

ybu*_*ill 5

你可以这样做:

#define uint32 Box2D_uint32
#include "Box2D.h"
#undef uint32
#define uint32 Spider_uint32
#include "Spidermonkey.h"
#undef uint32
Run Code Online (Sandbox Code Playgroud)

由于typedef只是一个别名,因此只要这些标头仅包含声明,就不会导致 ODR 违规。如果存在使用 uint32 的(结构体或内联函数)定义,则会违反ODR。尽管您的编译器可能不够智能,无法检测到这一点,但它仍然可以工作。

但更好的选择是将问题报告给库开发人员,以便他们使用命名空间等方法修复该问题。