Solved Circular include dependency issue with QOpenGLWindow ( is this a bug? )
-
I'm developing some OpenGL applications and I came through this little annoying issue with QOpenGLWindow. Apparently, the issue is the circular include dependency if I have a class whose name is Rectangle. After some digging and asking in another forum, it seems the name Rectangle is defined somewhere in Windows. The following minimal working example produces the issue.
1- Create simple gui application (i.e.
QMainWindow
).
2- Create class and name it Rectangle.
3- Add#include <rectangle.h>
and#include <QOpenGLWindow>
insidemainwindow.h
.
4- AddRectangle *box;
toclass MainWindow
.
5- Addopengl
to.pro
fileYou will get this annoying unclear error
Rectangle *box; // <-- C2143: syntax error: missing ';' before '*'
In the forum that I've asked the question, one suggests that the Rectangle function is defined in win32 API. Is this a bug or merely a rare coincidence and lucky I was aware of this error but I couldn't image that this function is defined somewhere? How can I avoid this subtle issues in the future?
The compiler is MSVC2015 32bit.
-
@CroCo I cannot reproduce this issue on Windows 7 with MinGW as compiler.
You should not include your own header files with <> do it like this:#include "rectangle.h"
But with MinGW both variants work fine.
Can you post the whole compiler output?
-
The compiler is MSVC2015 32bit. I've tried
#include "rectangle.h"
with no luck. These are the errors I'm getting -
@CroCo Can you post the output from "Compile Output"?
If there is really a name conflict you could put your own Rectangle class in a namespace. -
-
-
Yes, defining a namespace around your Rectangle is a good solution.
This is a problem from the 80's before namespaces were invented, there are many example of Microsoft's headers polluting the global namespace (on the Mac everything is prefixed with 'NS..." from the NextStep days but Microsoft never uses an MS prefix :-(
Anyway, to unpollute the global namespace, you can also apply an 80's solution, just #define NOGDI before #including QOpenGLWindow, like this:
#include <QMainWindow> #define NOGDI #include <QOpenGLWindow> class Rectangle { public: int r; };
(This assumes you're not using any lowlevel Windows GDI functions in your cpp code)