c++openglglx

How to declare a class whose name is already defined in external library?


I'm working in a multiplatform project using OpenGL and get to the point where I needed to query the current context. In windows I used wglGetCurrentContext() including windows.h that worked fine.

On the other hand, when tried to compile in linux I'm using glXGetCurrentContext() and including glx.h which internally includes Xlib.h and X.h

It happens that in my source code I have a class called Status but there is a macro called the same in Xlib, i.e. #define Status int, Aha ! big problem now since I use my class everywhere.

What would be the best way to overcome this problem? The ideas I have in mind right now are

  1. Rename my class to something else ... but why?
  2. Use #pragma push_macro("Status") followed by a #undef Status
  3. Find a more robust and portable way to query OpenGL's context

If you have any other recommendation let me know, I appreaciate it very much.


Solution

  • at a bare minimum, you may isolate glXGetCurrentContext() into its own translation unit:

    myGlXGetCurrentContext.hpp

    GLUint myGlXGetCurrentContext();
    

    myGlXGetCurrentContext.cpp

    #include<glx.h>
    GLUint myGlXGetCurrentContext(){ return glXGetCurrentContext(); }
    

    whatever.hpp

    #include<myGlXGetCurrentContext.hpp>
    ...