Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Circular include dependency issue with QOpenGLWindow ( is this a bug? )
Forum Updated to NodeBB v4.3 + New Features

Circular include dependency issue with QOpenGLWindow ( is this a bug? )

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 1.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    CroCo
    wrote on last edited by CroCo
    #1

    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> inside mainwindow.h.
    4- Add Rectangle *box; to class MainWindow.
    5- Add opengl to .pro file

    You 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.

    jsulmJ 1 Reply Last reply
    0
    • C CroCo

      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> inside mainwindow.h.
      4- Add Rectangle *box; to class MainWindow.
      5- Add opengl to .pro file

      You 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.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • C Offline
        C Offline
        CroCo
        wrote on last edited by CroCo
        #3

        @jsulm

        The compiler is MSVC2015 32bit. I've tried #include "rectangle.h" with no luck. These are the errors I'm getting

        0_1501054844106_Capture.PNG

        jsulmJ 1 Reply Last reply
        0
        • C CroCo

          @jsulm

          The compiler is MSVC2015 32bit. I've tried #include "rectangle.h" with no luck. These are the errors I'm getting

          0_1501054844106_Capture.PNG

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          C 1 Reply Last reply
          1
          • jsulmJ jsulm

            @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.

            C Offline
            C Offline
            CroCo
            wrote on last edited by
            #5

            @jsulm

            0_1501056083469_Capture.PNG

            D 1 Reply Last reply
            0
            • C CroCo

              @jsulm

              0_1501056083469_Capture.PNG

              D Offline
              D Offline
              Devopia53
              wrote on last edited by
              #6

              @CroCo

              As @jsulm points out, the Rectangle is already defined in wingdi.h.
              Please use the namespace simply. like this:

              #include "rectangle.h"
              
              namespace MyRec {
              class Rectangle;
              }
              
              class MainWindow : public QMainWindow
              {
              [...]
              MyRec::Rectangle *box;
              [...]
              }
              
              1 Reply Last reply
              1
              • hskoglundH Offline
                hskoglundH Offline
                hskoglund
                wrote on last edited by
                #7

                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)

                1 Reply Last reply
                1

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved