MrCravon MrCravon - 1 year ago 105
C++ Question

#define conflict with variable in .dll header file

I am using Unreal Engine 4 with some external .dll libraries. I have encountered a problem where "PI" is defined in unreal engine core code as "3.141592..." like this:

#define PI (3.1415926535897932f)

However, in the header file supplied with the .dll library I am using, "PI" is the name of a protected member variable of a class:

SomeDataType PI;

I cant edit the define because it is used in the core files of the Unreal Engine. I believe I can not edit the header file as well as it would no longer match the underlying .dll.

Is there a good solution for this? Can I undefine "PI" locally or something?

Answer Source

This is a problem with macros, and is the reason why modern C++ programmers try to avoid them as much as possible.

One solution is for your code to not include the Unreal header file directly, but make up something like:

// my_unreal.h
#include "unreal.h"
#undef PI

And then in the rest of your code only do #include "my_unreal.h", and not the actual unreal header. So that the macro is gone by the time any other code happens.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download