0

Sorry if this is a very basic question, but I'm confused:
I'm using Visual Studio and I'm creating a custom QOpenGLWidget class (by inheriting from the base class), and my problem is as follows:

  • If I add #include <QOpenGLWidget> in my custom class, and also add the #include for glew.h, I can access the things defined in glew.h
  • But If I add #include <QtWidgets> in my custom class (which I though would cover QOpenGLWidget too), then I can't access what is defined in glew.h and I see "identifier not defined" errors (for example, for glGenVertexArrays)

Can someone please tell me what I'm missing here? I'm guessing that including what I only need (#include <QOpenGLWidget>) is better than a whole lot of stuff in <QtWidgets>, but why should it prevent me from accessing what is defined in glew.h?

Any help is appreciated

Farzad
  • 1,770
  • 4
  • 26
  • 48
  • Note that `#include ` implies a `#include `, which in turn does a `#include `, which in turn includes some OpenGL headers. Maybe `glew.h` has some `#ifndef` protection that gets triggered by that. – peppe Feb 29 '16 at 16:19
  • Btw, are you sure you need GLEW? – peppe Feb 29 '16 at 16:19
  • @peppe I think I need GLEW because I'm calling direct OpenGL API functions, and I'm not using the wrappers in Qt. Am I missing something? – Farzad Feb 29 '16 at 16:24
  • Just that you could use the direct wrappers in Qt :-) – peppe Feb 29 '16 at 16:34
  • @peppe: Except he can't, since they don't wrap everything. They provide a common OpenGL ES 2 interface, not full desktop GL. – Nicol Bolas Feb 29 '16 at 16:45
  • ? They *do* provide full desktop GL. You've got per-version resolvers (like [this](https://doc.qt.io/qt-5/qopenglfunctions-3-3-core.html) for 3.3 core) and per-extension (see `QOpenGLExtensions`). – peppe Feb 29 '16 at 17:20

1 Answers1

1

You cannot simultaneously include Qt's OpenGL wrappers and most OpenGL loading library headers. Different .cpp files can include different headers, but the same .cpp cannot. So you're going to have to pick what you want to use: GLEW or Qt's OpenGL wrapper.

The reason this doesn't work is because loaders generally use include guards that prevent you from accidentally including <GL/gl.h> or similar headers. Loading libraries tend to be exclusive, since they are often defining the same symbols that other OpenGL headers would. And if they didn't keep you from doing that, you could get multiply-defined-symbol errors if you try to include both.

Qt's OpenGL wrapper is itself a loading library. So it does the same exclusion trick, defining the include guards that GLEW and other loaders would use. As such, you can't use both in the same .cpp file.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982