#define 宏和枚举使用相同的名称导致的冲突

Conflict caused by #define macro and enum using same name

本文关键字:冲突 枚举 #define      更新时间:2023-10-16

我正在尝试将 NVIDIA 的 PhysX 集成到我的 Linux 代码库中。在其某些头文件中,它定义了以下枚举:

physxvisualdebuggersdk/PvdErrorCodes.h

struct PvdErrorType
{
  enum Enum
  {
    Success = 0,
    NetworkError,
    ArgumentError,
    InternalProblem
  };
};

physxprofilesdk/PxProfileCompileTimeEventFilter.h:

struct EventPriorities
{
  enum Enum
  {
    None,       // the filter setting to kill all events
    Coarse,
    Medium,
    Detail,
    Never       // the priority to set for an event if it should never fire.
  };
};

这会导致以下编译错误:

/usr/local/include/PhysX3/physxvisualdebuggersdk/PvdErrorCodes.h:36:4: error: expected identifier before numeric constant
    Success = 0,
    ^
/usr/local/include/PhysX3/physxvisualdebuggersdk/PvdErrorCodes.h:36:4: error: expected ‘}’ before numeric constant
/usr/local/include/PhysX3/physxvisualdebuggersdk/PvdErrorCodes.h:36:4: error: expected unqualified-id before numeric constant

/usr/local/include/PhysX3/physxprofilesdk/PxProfileCompileTimeEventFilter.h:46:4: error: expected identifier before numeric constant
    None,  // the filter setting to kill all events
    ^
/usr/local/include/PhysX3/physxprofilesdk/PxProfileCompileTimeEventFilter.h:46:4: error: expected ‘}’ before numeric constant
/usr/local/include/PhysX3/physxprofilesdk/PxProfileCompileTimeEventFilter.h:46:4: error: expected unqualified-id before numeric constant

我已经确定这是因为 X11/X.h #defines"无"和"成功"。我已经确认这是问题所在,因为如果我同时 #undef"无"和"成功",我就不再有错误。但是,这显然不是一件可取的事情。

我的问题是:作为必须同时使用这两个标头的开发人员,我应该采取什么正确的行动方案?我应该将其作为错误报告给 NVIDIA 并等待修复,还是我可以自己做些什么来解决问题(除了 #undef)?

谢谢你的时间!

最明智的做法是分离实现,以便在项目中不要在同一源文件中包含两个冲突的标头。一个文件处理 X,一个文件处理 PhysX,然后您的应用程序将两个实现绑定在一起。